/
sinks.go
67 lines (58 loc) · 1.7 KB
/
sinks.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
package server
import (
"github.com/gocraft/web"
"gopkg.in/pfnet/jasco.v1"
"gopkg.in/sensorbee/sensorbee.v0/core"
"gopkg.in/sensorbee/sensorbee.v0/server/response"
"net/http"
)
type sinks struct {
*topologies
sink core.SinkNode
}
func setUpSinksRouter(prefix string, router *web.Router) {
root := router.Subrouter(sinks{}, "/:topologyName/sinks")
root.Middleware((*sinks).fetchSink)
root.Get("/", (*sinks).Index)
root.Get("/:sinkName", (*sinks).Show)
}
func (sc *sinks) fetchSink(rw web.ResponseWriter, req *web.Request, next web.NextMiddlewareFunc) {
tb := sc.fetchTopology()
if tb == nil {
return
}
if sinkName := sc.PathParams().String("sinkName", ""); sinkName != "" {
sink, err := tb.Topology().Sink(sinkName)
if err != nil {
sc.ErrLog(err).Error("Cannot find the sink")
sc.RenderError(jasco.NewError(requestResourceNotFoundErrorCode,
"The sink was not found", http.StatusNotFound, err))
return
}
sc.sink = sink
sc.AddLogField("node_type", core.NTSink.String())
sc.AddLogField("node_name", sink.Name())
}
next(rw, req)
}
func (sc *sinks) Index(rw web.ResponseWriter, req *web.Request) {
// TODO: support pagination
sinks := sc.topology.Topology().Sinks()
res := make([]*response.Sink, 0, len(sinks))
for _, s := range sinks {
res = append(res, response.NewSink(s, false))
}
sc.Render(map[string]interface{}{
"topology": sc.topologyName,
"count": len(res),
"sinks": res,
})
}
func (sc *sinks) Show(rw web.ResponseWriter, req *web.Request) {
sc.Render(map[string]interface{}{
"topology": sc.topologyName,
"sink": response.NewSink(sc.sink, true),
})
}
// TODO: Support Update(e.g. pause/resume) and Destroy if necessary. They can be
// done by queries.