-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathroutes.go
More file actions
52 lines (43 loc) · 1.32 KB
/
routes.go
File metadata and controls
52 lines (43 loc) · 1.32 KB
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
package ukuleleweb
import (
"embed"
"net/http"
"github.com/peterbourgon/diskv/v3"
)
//go:embed static/*
var staticFiles embed.FS
type Config struct {
MainPage string
Store *diskv.Diskv
}
func NewServer(cfg *Config) http.Handler {
if cfg.MainPage == "" {
cfg.MainPage = "MainPage"
}
mux := http.NewServeMux()
addRoutes(mux, cfg)
return noReferrer(mux)
}
// noReferrer wraps a handler to set a no-referrer Referrer-Policy header.
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Referrer-Policy
func noReferrer(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Referrer-Policy", "no-referrer")
h.ServeHTTP(w, r)
})
}
// addRoutes adds the Ukuleleweb routes to the given ServeMux.
func addRoutes(mux *http.ServeMux, cfg *Config) {
mux.Handle("GET /static/", http.FileServer(http.FS(staticFiles)))
mux.HandleFunc("POST /preview", previewHandler)
handler := &PageHandler{
MainPage: cfg.MainPage,
D: cfg.Store,
}
mux.HandleFunc("GET /{$}", func(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "/"+cfg.MainPage, http.StatusMovedPermanently)
})
mux.HandleFunc("GET /edit/{pageName}", handler.serveEdit)
mux.HandleFunc("POST /edit/{pageName}", handler.serveSave)
mux.HandleFunc("GET /{pageName}", handler.serveView)
}