/
sources.go
67 lines (58 loc) · 1.73 KB
/
sources.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 sources struct {
*topologies
src core.SourceNode
}
func setUpSourcesRouter(prefix string, router *web.Router) {
root := router.Subrouter(sources{}, "/:topologyName/sources")
root.Middleware((*sources).fetchSource)
root.Get("/", (*sources).Index)
root.Get("/:sourceName", (*sources).Show)
}
func (sc *sources) fetchSource(rw web.ResponseWriter, req *web.Request, next web.NextMiddlewareFunc) {
tb := sc.fetchTopology()
if tb == nil {
return
}
if srcName := sc.PathParams().String("sourceName", ""); srcName != "" {
src, err := tb.Topology().Source(srcName)
if err != nil {
sc.ErrLog(err).Error("Cannot find the source")
sc.RenderError(jasco.NewError(requestResourceNotFoundErrorCode,
"The source was not found", http.StatusNotFound, err))
return
}
sc.src = src
sc.AddLogField("node_type", core.NTSource.String())
sc.AddLogField("node_name", src.Name())
}
next(rw, req)
}
func (sc *sources) Index(rw web.ResponseWriter, req *web.Request) {
// TODO: support pagination
srcs := sc.topology.Topology().Sources()
res := make([]*response.Source, 0, len(srcs))
for _, s := range srcs {
res = append(res, response.NewSource(s, false))
}
sc.Render(map[string]interface{}{
"topology": sc.topologyName,
"count": len(res),
"sources": res,
})
}
func (sc *sources) Show(rw web.ResponseWriter, req *web.Request) {
sc.Render(map[string]interface{}{
"topology": sc.topologyName,
"source": response.NewSource(sc.src, true),
})
}
// TODO: Support Update(e.g. pause/resume) and Destroy if necessary. They can be
// done by queries.