-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathhandlers.go
118 lines (94 loc) · 2.49 KB
/
handlers.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package xlog
import (
"context"
"flag"
"html/template"
"log/slog"
"os"
"runtime"
"time"
"github.com/gorilla/csrf"
"gitlab.com/greyxor/slogor"
)
// Define the catch all HTTP routes, parse CLI flags and take actions like
// building the static pages and exit, or start the HTTP server
func Start(ctx context.Context) {
runtime.GOMAXPROCS(runtime.NumCPU() * 2)
flag.Parse()
// Setup logger
level := slogor.SetLevel(slog.LevelDebug)
timeFmt := slogor.SetTimeFormat(time.TimeOnly)
handler := slogor.NewHandler(os.Stderr, level, timeFmt)
logger := slog.New(handler)
slog.SetDefault(logger)
// if a static site is going to be built then lets also turn on read only
// mode
if len(Config.Build) > 0 {
Config.Readonly = true
}
if !Config.Readonly {
Listen(PageChanged, clearPagesCache)
Listen(PageDeleted, clearPagesCache)
}
if err := os.Chdir(Config.Source); err != nil {
slog.Error("Failed to change dir to source", "error", err, "source", Config.Source)
os.Exit(1)
}
initExtensions()
Get("/{$}", rootHandler)
Get("/{page...}", getPageHandler)
if len(Config.Build) > 0 {
if err := build(Config.Build); err != nil {
slog.Error("Failed to build static pages", "error", err)
os.Exit(1)
}
return
}
srv := server()
slog.Info("Starting server", "address", Config.BindAddress)
go func() {
<-ctx.Done()
srv.Close()
}()
srv.ListenAndServe()
}
// Redirect to `/index` to render the index page.
func rootHandler(r Request) Output {
return Redirect("/" + Config.Index)
}
// Shows a page. the page name is the path itself. if the page doesn't exist it
// redirect to edit page otherwise will render it to HTML
func getPageHandler(r Request) Output {
page := NewPage(r.PathValue("page"))
if page == nil {
return NoContent()
}
if !page.Exists() {
// if it's a directory get back to home page
if s, err := os.Stat(page.Name()); err == nil && s.IsDir() {
return Redirect(Config.Index)
}
// if it's a static file serve it
if output, err := staticHandler(r); err == nil {
return output
}
// if it's readonly mode quit now
if Config.Readonly {
return NotFound("can't find page")
}
// Allow extensions to handle this page if it's not readonly mode like
// opening an editor or something
Trigger(PageNotFound, page)
page = DynamicPage{
NameVal: page.Name(),
RenderFn: func() template.HTML {
str := "Page doesn't exist"
return template.HTML(str)
},
}
}
return Render("page", Locals{
"page": page,
"csrf": csrf.Token(r),
})
}