forked from facesea/banshee
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
76 lines (70 loc) · 2.38 KB
/
server.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
// Copyright 2015 Eleme Inc. All rights reserved.
package webapp
import (
"fmt"
"net/http"
"github.com/eleme/banshee/config"
"github.com/eleme/banshee/filter"
"github.com/eleme/banshee/storage"
"github.com/eleme/banshee/util/log"
"github.com/julienschmidt/httprouter"
)
// Globals
var (
// Config
cfg *config.Config
// Storage
db *storage.DB
// Filter
flt *filter.Filter
)
// Init globals.
func Init(c *config.Config, d *storage.DB) {
cfg = c
db = d
}
// Start http server.
func Start(c *config.Config, d *storage.DB, f *filter.Filter) {
// Init globals.
cfg = c
db = d
flt = f
// Auth
auth := newAuthHandler(cfg.Webapp.Auth[0], cfg.Webapp.Auth[1])
// Routes
router := httprouter.New()
// Api
router.GET("/api/config", auth.handler(getConfig))
router.GET("/api/interval", getInterval)
router.GET("/api/notice", getNotice)
router.GET("/api/language", getLanguage)
router.GET("/api/projects", getProjects)
router.GET("/api/project/:id", getProject)
router.POST("/api/project", auth.handler(createProject))
router.PATCH("/api/project/:id", auth.handler(updateProject))
router.DELETE("/api/project/:id", auth.handler(deleteProject))
router.GET("/api/project/:id/rules", auth.handler(getProjectRules))
router.GET("/api/project/:id/users", auth.handler(getProjectUsers))
router.POST("/api/project/:id/user", auth.handler(addProjectUser))
router.DELETE("/api/project/:id/user/:user_id", auth.handler(deleteProjectUser))
router.GET("/api/users", auth.handler(getUsers))
router.GET("/api/user/:id", auth.handler(getUser))
router.POST("/api/user", auth.handler(createUser))
router.DELETE("/api/user/:id", auth.handler(deleteUser))
router.PATCH("/api/user/:id", auth.handler(updateUser))
router.GET("/api/user/:id/projects", auth.handler(getUserProjects))
router.POST("/api/project/:id/rule", auth.handler(createRule))
router.DELETE("/api/rule/:id", auth.handler(deleteRule))
router.POST("/api/rule/:id", auth.handler(editRule))
router.GET("/api/metric/rules/:name", getMetricRules)
router.GET("/api/metric/indexes", getMetricIndexes)
router.GET("/api/metric/data", getMetrics)
router.GET("/api/info", getInfo)
router.GET("/api/version", getVersion)
// Static
router.NotFound = newStaticHandler(http.Dir(cfg.Webapp.Static), auth)
// Serve
addr := fmt.Sprintf("0.0.0.0:%d", cfg.Webapp.Port)
log.Info("webapp is listening and serving on %s..", addr)
http.ListenAndServe(addr, router)
}