-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandlers.go
48 lines (40 loc) · 1.09 KB
/
handlers.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
package main
import (
"encoding/json"
"golang.org/x/exp/slog"
"net/http"
"strings"
)
func (rs *RequestStore) rootHandler(w http.ResponseWriter, r *http.Request) {
b := rs.Add(r)
if b == nil {
slog.Error("failed to store request", "url", r.URL.String())
w.WriteHeader(http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
_, err := w.Write(b)
if err != nil {
slog.Error("failed to write response", "error", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
}
func (rs *RequestStore) logHandler(w http.ResponseWriter, r *http.Request) {
url := r.URL.String()
url = strings.TrimPrefix(url, "/logs")
// we store root requests under the / url so we need to make sure we ge those correctly as well
if url == "" {
url = "/"
}
rs.Print(w, url)
}
func (rs *RequestStore) logAllHandler(w http.ResponseWriter, r *http.Request) {
err := json.NewEncoder(w).Encode(rs.Requests)
if err != nil {
slog.Error("failed to encode requests", "error", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
}