-
Notifications
You must be signed in to change notification settings - Fork 1
/
hooks.go
47 lines (41 loc) · 1.23 KB
/
hooks.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
package apikit
import (
"github.com/revel/revel"
"encoding/json"
"net/http"
)
type GETHooker interface {
RESTController
PreGETHook(id uint64, authUser User) revel.Result
PostGETHook(model RESTObject, authUser User) revel.Result
}
type POSTHooker interface {
RESTController
PrePOSTHook(model RESTObject, authUser User) revel.Result
PostPOSTHook(model RESTObject, authUser User, err error) revel.Result
}
type PUTHooker interface {
RESTController
PrePUTHook(newInstance, existingInstance RESTObject, authUser User) revel.Result
PostPUTHook(newInstance, existingInstance RESTObject, authUser User, err error) revel.Result
}
type DELETEHooker interface {
RESTController
PreDELETEHook(model RESTObject, authUser User) revel.Result
PostDELETEHook(model RESTObject, authUser User, err error) revel.Result
}
// Result that can be returned from a hook
type HookJsonResult struct {
Body interface{}
}
func (result HookJsonResult) Apply(req *revel.Request, resp *revel.Response) {
if body, err := json.Marshal(result.Body); err != nil {
ApiMessage{
StatusCode: http.StatusInternalServerError,
Message: "Something went wrong",
}.Apply(req, resp)
} else {
resp.WriteHeader(http.StatusOK, "application/json")
resp.Out.Write(body)
}
}