-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.go
135 lines (126 loc) · 3.04 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package handlers
import (
"errors"
"fmt"
"html/template"
"io/fs"
"net/http"
"path"
"regexp"
"strings"
"github.com/Masterminds/sprig/v3"
"github.com/ryodocx/private-endpoint-proxy/pkg/dao"
"github.com/ryodocx/private-endpoint-proxy/pkg/upstream"
)
type server struct {
mux *http.ServeMux
files http.FileSystem
templates map[string]*template.Template
consolePrefix string
proxyPathRegex *regexp.Regexp
upstream upstream.UpstreamProvider
dao dao.Dao
}
func (s server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
s.mux.ServeHTTP(w, r)
}
func New(files fs.FS, upstream upstream.UpstreamProvider, d dao.Dao) (http.Handler, error) {
s := &server{
mux: http.NewServeMux(),
files: http.FS(files),
templates: map[string]*template.Template{},
consolePrefix: "/console/",
proxyPathRegex: regexp.MustCompile(`^/([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})(/|$|\?)`),
upstream: upstream,
dao: d,
}
// load templates
var dirwalk func(f fs.FS, dir string) (paths []string, err error)
dirwalk = func(f fs.FS, dir string) (paths []string, err error) {
entries, err := fs.ReadDir(f, dir)
if err != nil {
return nil, err
}
for _, entry := range entries {
if entry.IsDir() {
nestedPaths, err := dirwalk(f, path.Join(dir, entry.Name()))
if err != nil {
return nil, err
}
paths = append(paths, nestedPaths...)
continue
}
paths = append(paths, path.Join(dir, entry.Name()))
}
return paths, nil
}
paths, err := dirwalk(files, ".")
if err != nil {
return nil, errors.Join(err, fmt.Errorf("failed at call dirwalk()"))
}
for _, p := range paths {
if !strings.HasSuffix(p, ".html") {
continue
}
t, err := template.New(p).Funcs(sprig.FuncMap()).ParseFS(files, p)
if err != nil {
return nil, errors.Join(err, fmt.Errorf("failed at call dirwalk()"))
}
s.templates[p] = t
}
// setup handlers
handleFunc := func(pattern string, handers ...http.HandlerFunc) {
// TODO: support sentry.io
s.mux.HandleFunc(
pattern,
func(handlers ...http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
dw := &doneWriter{
pattern: pattern,
s: s,
r: r,
ResponseWriter: w,
}
defer func() {
recover()
if !dw.writeDone {
dw.WriteHeader(http.StatusInternalServerError)
}
}()
for _, h := range handlers {
h.ServeHTTP(dw, r)
if dw.writeDone {
return
}
}
}
}(handers...),
)
}
handleFunc("/livez",
method(http.MethodGet, http.MethodHead),
s.live,
)
handleFunc("/readyz",
method(http.MethodGet, http.MethodHead),
s.ready,
)
handleFunc(s.consolePrefix,
method(http.MethodGet),
s.console,
)
handleFunc("/api/token/add",
method(http.MethodPost),
antiCSRF(),
s.addToken,
)
handleFunc("/api/token/delete",
method(http.MethodPost),
antiCSRF(),
s.deleteToken,
)
handleFunc("/",
s.proxy,
)
return s, nil
}