forked from SpectoLabs/hoverfly
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cache_handler.go
61 lines (49 loc) · 1.49 KB
/
cache_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
49
50
51
52
53
54
55
56
57
58
59
60
61
package v2
import (
"encoding/json"
"net/http"
"github.com/SpectoLabs/hoverfly/core/handlers"
"github.com/codegangsta/negroni"
"github.com/go-zoo/bone"
)
type HoverflyCache interface {
GetCache() (CacheView, error)
FlushCache() error
}
type CacheHandler struct {
Hoverfly HoverflyCache
}
func (this *CacheHandler) RegisterRoutes(mux *bone.Mux, am *handlers.AuthHandler) {
mux.Get("/api/v2/cache", negroni.New(
negroni.HandlerFunc(am.RequireTokenAuthentication),
negroni.HandlerFunc(this.Get),
))
mux.Delete("/api/v2/cache", negroni.New(
negroni.HandlerFunc(am.RequireTokenAuthentication),
negroni.HandlerFunc(this.Delete),
))
mux.Options("/api/v2/cache", negroni.New(
negroni.HandlerFunc(this.Options),
))
}
func (this *CacheHandler) Get(w http.ResponseWriter, req *http.Request, next http.HandlerFunc) {
cacheViews, err := this.Hoverfly.GetCache()
if err != nil {
handlers.WriteErrorResponse(w, err.Error(), http.StatusInternalServerError)
return
}
bytes, _ := json.Marshal(cacheViews)
handlers.WriteResponse(w, bytes)
}
func (this *CacheHandler) Delete(w http.ResponseWriter, req *http.Request, next http.HandlerFunc) {
err := this.Hoverfly.FlushCache()
if err != nil {
handlers.WriteErrorResponse(w, err.Error(), http.StatusInternalServerError)
return
}
this.Get(w, req, next)
}
func (this *CacheHandler) Options(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
w.Header().Add("Allow", "OPTIONS, GET, DELETE")
handlers.WriteResponse(w, []byte(""))
}