/
api.go
73 lines (60 loc) · 1.55 KB
/
api.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
package server
import (
"github.com/jonasi/mohttp"
"github.com/jonasi/mohttp/hateoas"
"github.com/jonasi/mohttp/middleware"
"github.com/jonasi/project/server/api"
"golang.org/x/net/context"
)
var setSrv, getSrv = mohttp.ContextValueAccessors("github.com/jonasi/project/server.Server")
func getServer(c context.Context) *Server {
return getSrv(c).(*Server)
}
var apiService = hateoas.NewService(
hateoas.AddResource(root, version, status, plugins),
hateoas.ServiceUse(api.JSON, api.AddLinkHeaders),
)
var root = hateoas.NewResource(
hateoas.Path("/"),
hateoas.AddLink("version", version),
hateoas.AddLink("status", status),
hateoas.AddLink("plugins", plugins),
hateoas.HEAD(mohttp.EmptyBodyHandler),
)
var version = hateoas.NewResource(
hateoas.Path("/version"),
hateoas.GET(mohttp.StaticDataHandler(Version)),
)
var status = hateoas.NewResource(
hateoas.Path("/status"),
hateoas.PATCH(mohttp.HandlerFunc(func(c context.Context) {
var body struct {
Status string `json:"status"`
}
if err := middleware.JSONBodyDecode(c, &body); err != nil {
mohttp.GetResponseWriter(c).WriteHeader(500)
return
}
if body.Status == "inactive" {
getServer(c).Close()
}
})),
)
var plugins = hateoas.NewResource(
hateoas.Path("/plugins"),
hateoas.GET(mohttp.DataHandlerFunc(func(c context.Context) (interface{}, error) {
var (
plugins = getServer(c).plugins
resp = make([]pl, len(plugins))
)
i := 0
for _, p := range plugins {
resp[i].Name = p.name
i++
}
return resp, nil
})),
)
type pl struct {
Name string `json:"name"`
}