-
Notifications
You must be signed in to change notification settings - Fork 204
/
hoverfly_postserveactiondetails_handler.go
76 lines (63 loc) · 2.69 KB
/
hoverfly_postserveactiondetails_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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package v2
import (
"encoding/json"
"net/http"
"github.com/SpectoLabs/hoverfly/core/handlers"
"github.com/codegangsta/negroni"
"github.com/go-zoo/bone"
)
type HoverflyPostServeActionDetails interface {
GetAllPostServeActions() PostServeActionDetailsView
SetLocalPostServeAction(string, string, string, int) error
SetRemotePostServeAction(string, string, int) error
DeletePostServeAction(string) error
}
type HoverflyPostServeActionDetailsHandler struct {
Hoverfly HoverflyPostServeActionDetails
}
func (postServeActionDetailsHandler *HoverflyPostServeActionDetailsHandler) RegisterRoutes(mux *bone.Mux, am *handlers.AuthHandler) {
mux.Get("/api/v2/hoverfly/post-serve-action", negroni.New(
negroni.HandlerFunc(am.RequireTokenAuthentication),
negroni.HandlerFunc(postServeActionDetailsHandler.Get),
))
mux.Put("/api/v2/hoverfly/post-serve-action", negroni.New(
negroni.HandlerFunc(am.RequireTokenAuthentication),
negroni.HandlerFunc(postServeActionDetailsHandler.Put),
))
mux.Delete("/api/v2/hoverfly/post-serve-action/:actionName", negroni.New(
negroni.HandlerFunc(am.RequireTokenAuthentication),
negroni.HandlerFunc(postServeActionDetailsHandler.Delete),
))
}
func (postServeActionDetailsHandler *HoverflyPostServeActionDetailsHandler) Get(w http.ResponseWriter, req *http.Request, next http.HandlerFunc) {
postServeActionsDetailsView := postServeActionDetailsHandler.Hoverfly.GetAllPostServeActions()
bytes, _ := json.Marshal(postServeActionsDetailsView)
handlers.WriteResponse(w, bytes)
}
func (postServeActionDetailsHandler *HoverflyPostServeActionDetailsHandler) Put(w http.ResponseWriter, req *http.Request, next http.HandlerFunc) {
var actionRequest ActionView
err := handlers.ReadFromRequest(req, &actionRequest)
if err != nil {
handlers.WriteErrorResponse(w, err.Error(), 400)
return
}
if actionRequest.Remote != "" {
err = postServeActionDetailsHandler.Hoverfly.SetRemotePostServeAction(actionRequest.ActionName, actionRequest.Remote, actionRequest.DelayInMs)
} else {
err = postServeActionDetailsHandler.Hoverfly.SetLocalPostServeAction(actionRequest.ActionName, actionRequest.Binary, actionRequest.ScriptContent, actionRequest.DelayInMs)
}
if err != nil {
handlers.WriteErrorResponse(w, err.Error(), 400)
return
}
postServeActionDetailsHandler.Get(w, req, next)
}
func (postServeActionDetailsHandler *HoverflyPostServeActionDetailsHandler) Delete(w http.ResponseWriter, req *http.Request, next http.HandlerFunc) {
actionName := bone.GetValue(req, "actionName")
err := postServeActionDetailsHandler.Hoverfly.DeletePostServeAction(actionName)
if err != nil {
handlers.WriteErrorResponse(w, err.Error(), 400)
return
}
postServeActionDetailsHandler.Get(w, req, next)
}