/
handler.go
107 lines (91 loc) · 3.15 KB
/
handler.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
package uos
import (
"embed"
"net/http"
)
// AppRequestHandler represents a application specific HTTP handler function.
type AppRequestHandler func(http.ResponseWriter, *http.Request)
// AppRequestHandlerOptions specify meta information about request handling
type AppRequestHandlerOptions struct {
// do not expect valid CSRF token on POST/PUT/DELETE
NoCSRFcheck bool
// do not include the page in sitemap
NoSitemap bool
// redirect to login page if not authenticated
IsAuthRequired bool
// login page (target for "authentication required" pages)
IsAuthPage bool
}
// AppRequestHandlerMapping represents a path pattern and a corresponding handler.
type AppRequestHandlerMapping struct {
// URL route
Route string
// request handler for specified route
Handler AppRequestHandler
// options
Options AppRequestHandlerOptions
}
// Internal indicates, that the given request handler requires authentication.
func (hm AppRequestHandlerMapping) Internal() AppRequestHandlerMapping {
hm.Options.IsAuthRequired = true
return hm
}
// AuthPage indicates, that the given request handler provides the authentication page.
func (hm AppRequestHandlerMapping) AuthPage() AppRequestHandlerMapping {
hm.Options.IsAuthPage = true
return hm
}
func (hm AppRequestHandlerMapping) NoSitemap() AppRequestHandlerMapping {
hm.Options.NoSitemap = true
return hm
}
// RegisterAppRequestHandler registers the given handler for a specific URL pattern/path.
func RegisterAppRequestHandler(pattern string, handler AppRequestHandler, options AppRequestHandlerOptions) {
Log.DebugContext("register request handler", LogContext{"pattern": pattern})
if options.IsAuthPage {
if authenticationPageURL != "" {
Log.PanicContext(
"multiple auth pages specified",
LogContext{"first": authenticationPageURL, "second": pattern},
)
panic("multiple auth pages specified")
}
authenticationPageURL = pattern
Log.InfoContext("registered auth page", LogContext{"url": authenticationPageURL})
}
sitemap.addToSitemap(pattern, !options.NoSitemap)
appMux.Handle(pattern, mwWrapF(handler, options))
}
// RegisterAppRequesHandlers registers a list of handler.
func RegisterAppRequestHandlers(handlerList ...AppRequestHandlerMapping) {
for _, mapping := range handlerList {
RegisterAppRequestHandler(
mapping.Route,
mapping.Handler,
mapping.Options,
)
}
}
// RegisterAppRequestHandlersList registers a slice of handler.
func RegisterAppRequestHandlersList(list []AppRequestHandlerMapping) {
RegisterAppRequestHandlers(list...)
}
// RegisterStaticAssets provides the given content file system at the specified location.
func RegisterStaticAssets(route string, content embed.FS) {
Log.DebugContext(
"register static assets server",
LogContext{"route": route},
)
appMux.Handle(route, http.FileServer(http.FS(content)))
}
// RegisterDynamicAssets provides the configured dynamic asset directory at the specified location.
func RegisterDynamicAssets(route string) {
Log.DebugContext(
"register dynamic assets server",
LogContext{"route": route, "base": Config.Assets.Dynamic},
)
appMux.Handle(
route,
http.StripPrefix(route, http.FileServer(http.Dir(Config.Assets.Dynamic))),
)
}