forked from thbkrkr/toctoc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
101 lines (84 loc) · 2.21 KB
/
main.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package main
import (
"errors"
"flag"
"fmt"
"os"
"strings"
"sync"
log "github.com/Sirupsen/logrus"
"github.com/gin-gonic/gin"
"github.com/thbkrkr/go-utilz/http"
"github.com/thbkrkr/qli/client"
"github.com/thbkrkr/toctoc/types"
)
var (
name = "toctoc"
buildDate = "dev"
gitCommit = "dev"
port int
watchTick int
healthTimeout int
kafkaAlerter bool
mutex sync.RWMutex
events = map[string]map[string]types.Event{}
q *client.Qlient
namespaces string
)
func init() {
flag.IntVar(&port, "port", 4242, "Port")
flag.IntVar(&watchTick, "watch-tick", 30, "Tick in seconds to watch check states")
flag.IntVar(&healthTimeout, "health-timeout", 30, "Health timeout in seconds to consider a check in error")
flag.BoolVar(&kafkaAlerter, "kafka-alerter", false, "Send alerts to Kafka (required env vars: B, U, P, T)")
flag.StringVar(&namespaces, "ns", "c1,c2", "Namespaces")
flag.Parse()
}
func main() {
go Watch()
hostname, _ := os.Hostname()
if kafkaAlerter {
var err error
q, err = client.NewClientFromEnv(fmt.Sprintf("qws-%s", hostname))
if err != nil {
log.WithError(err).Fatal("Fail to create qlient")
}
if q == nil {
log.WithError(err).Fatal("Fail to create qlient (*)")
}
}
http.API(name, buildDate, gitCommit, port, router)
}
func router(e *gin.Engine) {
e.GET("/help", func(c *gin.Context) {
c.JSON(200, []string{
"POST /r/:ns/event HandleEvent",
"GET /r/:ns/health Health",
"GET /r/:ns/services Services",
"DELETE /r/:ns/service/:service DeleteService",
"DELETE /r/:ns/host/:host DeleteHost",
})
})
r := e.Group("/r", authMiddleware())
r.POST("/:ns/event", HandleEvent)
r.GET("/:ns/health", Health)
r.GET("/:ns/services", Services)
r.DELETE("/:ns/service/:service", DeleteService)
r.DELETE("/:ns/host/:host", DeleteHost)
}
func authMiddleware() gin.HandlerFunc {
return func(c *gin.Context) {
ns := c.Param("ns")
if !isAuthorized(ns) {
c.AbortWithError(401, errors.New("Authorization failed"))
}
}
}
func isAuthorized(ns string) bool {
authorizedNss := strings.Split(namespaces, ",")
for _, authorizedNs := range authorizedNss {
if authorizedNs == ns {
return true
}
}
return false
}