forked from wish/eventmaster
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.go
176 lines (144 loc) · 5.22 KB
/
server.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package eventmaster
import (
"encoding/json"
"net/http"
"path/filepath"
"time"
assetfs "github.com/elazarl/go-bindata-assetfs"
"github.com/julienschmidt/httprouter"
"github.com/prometheus/client_golang/prometheus/promhttp"
log "github.com/sirupsen/logrus"
"github.com/wish/eventmaster/jh"
"github.com/wish/eventmaster/metrics"
tmpl "github.com/wish/eventmaster/templates"
"github.com/wish/eventmaster/ui"
)
// Server implements http.Handler for the eventmaster http server.
type Server struct {
store *EventStore
handler http.Handler
ui http.FileSystem
templates TemplateGetter
}
// ServeHTTP dispatches to the underlying router.
func (srv *Server) ServeHTTP(w http.ResponseWriter, req *http.Request) {
srv.handler.ServeHTTP(w, req)
}
// NewServer returns a ready-to-use Server that uses store, and the appropriate
// static and templates facilities.
//
// If static or templates are non-empty then files are served from those
// locations (useful for development). Otherwise the server uses embedded
// static assets.
func NewServer(store *EventStore, static, templates string) *Server {
// Handle static files either embedded (empty static) or off the filesystem (during dev work)
var fs http.FileSystem
switch static {
case "":
fs = &assetfs.AssetFS{
Asset: ui.Asset,
AssetDir: ui.AssetDir,
AssetInfo: ui.AssetInfo,
}
default:
if p, d := filepath.Split(static); d == "ui" {
static = p
}
fs = http.Dir(static)
}
var t TemplateGetter
switch templates {
case "":
t = NewAssetTemplate(tmpl.Asset)
default:
t = Disk{Root: templates}
}
srv := &Server{
store: store,
ui: fs,
templates: t,
}
srv.handler = registerRoutes(srv)
return srv
}
func registerRoutes(srv *Server) http.Handler {
r := httprouter.New()
// API endpoints
r.POST("/v1/event", latency("/v1/event", jh.Adapter(srv.addEvent)))
r.GET("/v1/event", latency("/v1/event", jh.Adapter(srv.getEvent)))
r.GET("/v1/event/:id", latency("/v1/event", jh.Adapter(srv.getEventByID)))
r.POST("/v1/topic", latency("/v1/topic", jh.Adapter(srv.addTopic)))
r.PUT("/v1/topic/:name", latency("/v1/topic", jh.Adapter(srv.updateTopic)))
r.GET("/v1/topic", latency("/v1/topic", jh.Adapter(srv.getTopic)))
r.DELETE("/v1/topic/:name", latency("/v1/topic", jh.Adapter(srv.deleteTopic)))
r.POST("/v1/dc", latency("/v1/dc", jh.Adapter(srv.addDC)))
r.PUT("/v1/dc/:name", latency("/v1/dc", jh.Adapter(srv.updateDC)))
r.GET("/v1/dc", latency("/v1/dc", jh.Adapter(srv.getDC)))
r.GET("/v1/health", latency("/v1/health", jh.Adapter(srv.healthCheck)))
// GitHub webhook endpoint
r.POST("/v1/github_event", latency("/v1/github_event", jh.Adapter(srv.gitHubEvent)))
// UI endpoints
r.GET("/", latency("/", srv.HandleMainPage))
r.GET("/add_event", latency("/add_event", srv.HandleCreatePage))
r.GET("/topic", latency("/topic", srv.HandleTopicPage))
r.GET("/dc", latency("/dc", srv.HandleDCPage))
r.GET("/event", latency("/event", srv.HandleGetEventPage))
// grafana datasource endpoints
r.GET("/grafana", latency("/grafana", cors(srv.grafanaOK)))
r.GET("/grafana/", latency("/grafana/", cors(srv.grafanaOK)))
r.OPTIONS("/grafana/:route", latency("/grafana", cors(srv.grafanaOK)))
r.POST("/grafana/annotations", latency("/grafana/annotations", cors(srv.grafanaAnnotations)))
r.POST("/grafana/search", latency("/grafana/search", cors(srv.grafanaSearch)))
r.Handler("GET", "/metrics", promhttp.Handler())
r.Handler("GET", "/ui/*filepath", http.FileServer(srv.ui))
r.GET("/version/", latency("/version/", srv.version))
r.PanicHandler = func(w http.ResponseWriter, req *http.Request, i interface{}) {
metrics.HTTPStatus("panic", http.StatusInternalServerError)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusInternalServerError)
e := struct {
E string `json:"error"`
}{"server panicked"}
if err := json.NewEncoder(w).Encode(&e); err != nil {
log.Printf("json encode: %v", err)
}
}
r.NotFound = func(w http.ResponseWriter, req *http.Request) {
log.Printf("not found: %+v", req.URL.Path)
metrics.HTTPStatus(http.StatusText(http.StatusNotFound), http.StatusNotFound)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusNotFound)
e := struct {
E string `json:"error"`
}{"not found"}
if err := json.NewEncoder(w).Encode(&e); err != nil {
log.Printf("json encode: %v", err)
}
}
r.MethodNotAllowed = func(w http.ResponseWriter, req *http.Request) {
metrics.HTTPStatus(http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusMethodNotAllowed)
e := struct {
E string `json:"error"`
}{"method not allowed"}
if err := json.NewEncoder(w).Encode(&e); err != nil {
log.Printf("json encode: %v", err)
}
}
return r
}
func latency(prefix string, h httprouter.Handle) httprouter.Handle {
return func(w http.ResponseWriter, req *http.Request, ps httprouter.Params) {
start := time.Now()
defer func() {
metrics.HTTPLatency(prefix, start)
}()
lw := NewStatusRecorder(w)
h(lw, req, ps)
metrics.HTTPStatus(prefix, lw.Status())
}
}
func (srv *Server) healthCheck(w http.ResponseWriter, r *http.Request, _ httprouter.Params) (interface{}, error) {
return nil, nil
}