forked from influxdata/chronograf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprotoboards.go
76 lines (61 loc) · 1.78 KB
/
protoboards.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 server
import (
"fmt"
"net/http"
"github.com/bouk/httprouter"
"github.com/influxdata/chronograf"
)
type protoboardLinks struct {
Self string `json:"self"`
}
type protoboardResponse struct {
chronograf.Protoboard
Links protoboardLinks `json:"links"`
}
func newProtoboardResponse(protoboard chronograf.Protoboard) protoboardResponse {
httpAPIProtoboards := "/chronograf/v1/protoboards"
selfLink := fmt.Sprintf("%s/%s", httpAPIProtoboards, protoboard.ID)
return protoboardResponse{
Protoboard: protoboard,
Links: protoboardLinks{
Self: selfLink,
},
}
}
type getProtoboardsResponse struct {
Protoboards []protoboardResponse `json:"protoboards"`
}
// Protoboards retrieves all protoboards from store
func (s *Service) Protoboards(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
protoboards, err := s.Store.Protoboards(ctx).All(ctx)
if err != nil {
Error(w, http.StatusInternalServerError, "Error loading protoboards", s.Logger)
return
}
res := getProtoboardsResponse{
Protoboards: []protoboardResponse{},
}
seen := make(map[string]bool)
for _, protoboard := range protoboards {
// remove duplicates
if seen[protoboard.ID] {
continue
}
seen[protoboard.ID] = true
res.Protoboards = append(res.Protoboards, newProtoboardResponse(protoboard))
}
encodeJSON(w, http.StatusOK, res, s.Logger)
}
// ProtoboardsID retrieves protoboard with ID from store
func (s *Service) ProtoboardsID(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
id := httprouter.GetParamFromContext(ctx, "id")
protoboard, err := s.Store.Protoboards(ctx).Get(ctx, id)
if err != nil {
Error(w, http.StatusNotFound, fmt.Sprintf("ID %s not found", id), s.Logger)
return
}
res := newProtoboardResponse(protoboard)
encodeJSON(w, http.StatusOK, res, s.Logger)
}