forked from carl-mastrangelo/pixur
-
Notifications
You must be signed in to change notification settings - Fork 1
/
root.go
91 lines (77 loc) · 1.64 KB
/
root.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
package handlers
import (
"net/http"
"path"
"strings"
"pixur.org/pixur/fe/server"
)
type rootHandler struct {
pt *paths
indexHandler http.Handler
viewerHandler http.Handler
pixHandler http.Handler
errHandler http.Handler
}
func (h *rootHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
relpath := strings.TrimPrefix(r.URL.Path, h.pt.Root().Path)
if relpath == "" {
h.indexHandler.ServeHTTP(w, r)
return
}
if base := path.Base(relpath); base == relpath {
if strings.Contains(base, ".") {
h.pixHandler.ServeHTTP(w, r)
} else {
h.viewerHandler.ServeHTTP(w, r)
}
return
}
h.errHandler.ServeHTTP(w, r)
}
type rootErrorHandler struct{}
func (h *rootErrorHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
httpReadError(r.Context(), w, &HTTPErr{
Message: "Not found",
Code: http.StatusNotFound,
})
}
func init() {
register(func(s *server.Server) error {
pt := &paths{r: s.HTTPRoot}
rw := readWrapper(s)
ih := rw(http.HandlerFunc((&indexHandler{
c: s.Client,
pt: pt,
}).static))
ihh := compressHtmlHandler(&methodHandler{
Get: ih,
})
vh := rw(http.HandlerFunc((&viewerHandler{
c: s.Client,
pt: pt,
}).static))
vhh := compressHtmlHandler(&methodHandler{
Get: vh,
})
ph := &pixHandler{
c: s.Client,
}
phh := &methodHandler{
Get: ph,
}
eh := rw(&rootErrorHandler{})
ehh := compressHtmlHandler(&methodHandler{
Get: eh,
Post: eh,
})
rh := &rootHandler{
pt: pt,
indexHandler: ihh,
viewerHandler: vhh,
pixHandler: phh,
errHandler: ehh,
}
s.HTTPMux.Handle(pt.Root().Path, rh)
return nil
})
}