-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathweb_ui.go
153 lines (131 loc) · 3.49 KB
/
web_ui.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package chef
import (
"bytes"
"embed"
"html/template"
"io"
"io/fs"
"log"
"log/slog"
"net/http"
"os"
"runtime/debug"
"strconv"
"time"
"github.com/sauerbraten/chef/db"
)
var (
//go:embed templates css
embedded embed.FS
templates fs.FS = func() fs.FS { f, _ := fs.Sub(embedded, "templates"); return f }()
css fs.FS = func() fs.FS { f, _ := fs.Sub(embedded, "css"); return f }()
// for status page
gitRevision = func() string {
info, ok := debug.ReadBuildInfo()
if ok {
for _, bs := range info.Settings {
if bs.Key == "vcs.revision" {
return bs.Value[:min(len(bs.Value), 8)]
}
}
}
return "unknown"
}()
)
type WebUI struct {
http.Handler
db *db.Database
}
func NewWebUI(db *db.Database) *WebUI {
wui := &WebUI{
db: db,
}
r := http.NewServeMux()
r.HandleFunc("GET /", wui.frontPage())
r.HandleFunc("GET /info", wui.infoPage())
r.HandleFunc("GET /status", wui.statusPage())
r.HandleFunc("GET /lookup", wui.lookup())
r.Handle("GET /css/", http.StripPrefix("/css/", http.FileServer(http.FS(css))))
wui.Handler = r
return wui
}
func (wui *WebUI) staticPageFromTemplates(files ...string) http.HandlerFunc {
buf := new(bytes.Buffer)
err := template.
Must(template.ParseFS(templates, files...)).
Execute(buf, nil)
if err != nil {
slog.Error("build static page from template files", "files", files, "erorr", err)
os.Exit(1)
}
buildTime, page := time.Now(), bytes.NewReader(buf.Bytes())
return func(resp http.ResponseWriter, req *http.Request) {
http.ServeContent(resp, req, "dummy.html", buildTime, page)
page.Seek(0, io.SeekStart)
}
}
func (wui *WebUI) frontPage() http.HandlerFunc {
return wui.staticPageFromTemplates("base.tmpl", "search_form.tmpl", "front.tmpl")
}
func (wui *WebUI) infoPage() http.HandlerFunc {
return wui.staticPageFromTemplates("base.tmpl", "info.tmpl")
}
func (wui *WebUI) statusPage() http.HandlerFunc {
tmpl := template.
New("base.tmpl"). // must be the base template (entry point) so templates are associated correctly by ParseFS()
Option("missingkey=error").
Funcs(template.FuncMap{
"formatInt": func(i int) string {
s := strconv.Itoa(i)
if i < 1000 {
return s
}
f, s := s[len(s)-3:], s[:len(s)-3]
for len(s) > 3 {
f = s[len(s)-3:] + "," + f
s = s[:len(s)-3]
}
return s + "," + f
},
})
tmpl, err := tmpl.ParseFS(templates, "base.tmpl", "status.tmpl")
if err != nil {
log.Fatalln(err)
}
return func(resp http.ResponseWriter, req *http.Request) {
status := struct {
db.Status
Revision string
}{
Status: wui.db.Status(),
Revision: gitRevision,
}
err := tmpl.Execute(resp, status)
if err != nil {
log.Println(err)
}
}
}
func (wui *WebUI) lookup() http.HandlerFunc {
tmpl := template.
New("base.tmpl"). // must be the base template (entry point) so templates are associated correctly by ParseFS()
Option("missingkey=error").
Funcs(template.FuncMap{
"timestring": func(timestamp int64) string { return time.Unix(timestamp, 0).UTC().Format("2006-01-02 15:04:05") },
})
tmpl, err := tmpl.ParseFS(templates, "base.tmpl", "search_form.tmpl", "results.tmpl")
if err != nil {
log.Fatalln(err)
}
return func(resp http.ResponseWriter, req *http.Request) {
nameOrIP, sorting, last90DaysOnly, directLookupForced, redirected := parseLookupRequest(resp, req)
if redirected {
return
}
results := wui.db.Lookup(nameOrIP, sorting, last90DaysOnly, directLookupForced)
err := tmpl.Execute(resp, results)
if err != nil {
log.Println(err)
}
}
}