-
Notifications
You must be signed in to change notification settings - Fork 2
/
http.go
151 lines (129 loc) · 3.07 KB
/
http.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
package sqlog
import (
"embed"
"encoding/json"
"io/fs"
"net/http"
"net/url"
"os"
"path"
"strconv"
"strings"
)
var (
// running "/demo/main.go"
devDemo = false
//go:embed web/*
webFiles embed.FS
)
func (l *sqlog) HttpHandler() http.Handler {
var wfs http.FileSystem
if devDemo {
pwd, _ := os.Getwd()
pwd = strings.TrimSuffix(pwd, "demo") + "web"
wfs = http.Dir(pwd)
} else {
subfs, _ := fs.Sub(webFiles, "web")
wfs = http.FS(subfs)
}
staticHandler := http.FileServer(wfs)
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
p := r.URL.Path
if idx := strings.LastIndex(p, "/api/"); idx >= 0 {
p = p[idx+len("/api/"):]
switch p {
case "ticks":
l.ServeHTTPTicks(w, r)
case "entries":
l.ServeHTTPEntries(w, r)
case "result":
l.ServeHTTPResult(w, r)
}
} else {
switch path.Ext(p) {
case ".html", ".css", ".js":
p = path.Base(p)
default:
p = ""
}
r.URL.Path = p
staticHandler.ServeHTTP(w, r)
}
})
}
// ServeHTTPTicks tick api
func (l *sqlog) ServeHTTPTicks(w http.ResponseWriter, r *http.Request) {
var (
q = r.URL.Query()
levels []string
)
if level := q.Get("level"); level != "" {
levels = strings.Split(level, ",")
}
list, err := l.Ticks(&TicksInput{
Expr: q.Get("expr"),
Level: levels,
EpochEnd: getInt64(q, "epoch"),
IntervalSec: getInt(q, "interval"),
MaxResult: getInt(q, "limit"),
})
sendJson(w, list, err)
}
// ServeHTTPEntries entries api. (seek method or keyset pagination {before, after})
func (l *sqlog) ServeHTTPEntries(w http.ResponseWriter, r *http.Request) {
var (
q = r.URL.Query()
levels []string
)
if level := q.Get("level"); level != "" {
levels = strings.Split(level, ",")
}
entries, err := l.Entries(&EntriesInput{
Expr: q.Get("expr"),
Level: levels,
Direction: q.Get("dir"), // before, after
EpochStart: getInt64(q, "epoch"),
NanosStart: getInt(q, "nanos"),
MaxResult: getInt(q, "limit"),
})
sendJson(w, entries, err)
}
// ServeHTTPResult scheduled result api.
func (l *sqlog) ServeHTTPResult(w http.ResponseWriter, r *http.Request) {
var q = r.URL.Query()
result, err := l.Result(getInt32(q, "id"))
sendJson(w, result, err)
}
// ServeHTTPResult scheduled result api.
func (l *sqlog) ServeHTTPCancel(w http.ResponseWriter, r *http.Request) {
var q = r.URL.Query()
err := l.Cancel(getInt32(q, "id"))
sendJson(w, nil, err)
}
func sendJson(w http.ResponseWriter, data any, err error) {
w.Header().Set("Content-Type", "application/json")
if err != nil {
w.WriteHeader(http.StatusBadRequest)
json.NewEncoder(w).Encode(map[string]any{
"error": err.Error(),
})
} else {
if data != nil {
json.NewEncoder(w).Encode(data)
} else {
w.WriteHeader(http.StatusNoContent)
}
}
}
func getInt(q url.Values, key string) int {
v, _ := strconv.Atoi(q.Get(key))
return v
}
func getInt64(q url.Values, key string) int64 {
v, _ := strconv.ParseInt(q.Get(key), 10, 64)
return v
}
func getInt32(q url.Values, key string) int32 {
v, _ := strconv.ParseInt(q.Get(key), 10, 32)
return int32(v)
}