forked from brimdata/super
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler.go
48 lines (41 loc) · 1.38 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
package zqd
import (
"encoding/json"
"net/http"
"github.com/gorilla/mux"
"go.uber.org/zap"
)
type handlerFunc func(root *Core, w http.ResponseWriter, r *http.Request)
type handler struct {
*mux.Router
core *Core
}
func (h *handler) Handle(path string, f handlerFunc) *mux.Route {
return h.Router.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
f(h.core, w, r)
})
}
func NewHandler(root *Core) http.Handler {
return NewHandlerWithLogger(root, root.logger)
}
func NewHandlerWithLogger(core *Core, logger *zap.Logger) http.Handler {
h := handler{Router: mux.NewRouter(), core: core}
h.Use(requestIDMiddleware())
h.Use(accessLogMiddleware(logger))
h.Use(panicCatchMiddleware(logger))
h.Handle("/space", handleSpaceList).Methods("GET")
h.Handle("/space", handleSpacePost).Methods("POST")
h.Handle("/space/{space}", handleSpaceGet).Methods("GET")
h.Handle("/space/{space}", handleSpaceDelete).Methods("DELETE")
h.Handle("/space/{space}/packet", handlePacketSearch).Methods("GET")
h.Handle("/space/{space}/packet", handlePacketPost).Methods("POST")
h.Handle("/search", handleSearch).Methods("POST")
h.HandleFunc("/version", func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Type", "application/json")
json.NewEncoder(w).Encode(&Version)
})
h.HandleFunc("/status", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("ok"))
})
return h
}