forked from SpectoLabs/hoverfly
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hoverfly_usage_handler.go
43 lines (34 loc) · 1.08 KB
/
hoverfly_usage_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
package v2
import (
"encoding/json"
"net/http"
"github.com/SpectoLabs/hoverfly/core/handlers"
"github.com/SpectoLabs/hoverfly/core/metrics"
"github.com/codegangsta/negroni"
"github.com/go-zoo/bone"
)
type HoverflyUsage interface {
GetStats() metrics.Stats
}
type HoverflyUsageHandler struct {
Hoverfly HoverflyUsage
}
func (this *HoverflyUsageHandler) RegisterRoutes(mux *bone.Mux, am *handlers.AuthHandler) {
mux.Get("/api/v2/hoverfly/usage", negroni.New(
negroni.HandlerFunc(am.RequireTokenAuthentication),
negroni.HandlerFunc(this.Get),
))
mux.Options("/api/v2/hoverfly/usage", negroni.New(
negroni.HandlerFunc(this.Options),
))
}
func (this *HoverflyUsageHandler) Get(w http.ResponseWriter, req *http.Request, next http.HandlerFunc) {
var metricsView UsageView
metricsView.Usage = this.Hoverfly.GetStats()
bytes, _ := json.Marshal(metricsView)
handlers.WriteResponse(w, bytes)
}
func (this *HoverflyUsageHandler) Options(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
w.Header().Add("Allow", "OPTIONS, GET")
handlers.WriteResponse(w, []byte(""))
}