-
Notifications
You must be signed in to change notification settings - Fork 14
/
service.go
160 lines (128 loc) · 3.89 KB
/
service.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package service
import (
"context"
"log"
"net"
"net/http"
"time"
"github.com/go-chi/chi"
"github.com/go-chi/chi/middleware"
"github.com/golang/glog"
"strings"
"github.com/GeertJohan/go.rice"
"github.com/myntra/cortex/pkg/config"
"github.com/myntra/cortex/pkg/store"
)
// Service encapsulates the http server and the raft store
type Service struct {
srv *http.Server
node *store.Node
listener net.Listener
snapshotInterval int
httpAddr string
}
// Shutdown the service
func (s *Service) Shutdown(ctx context.Context) error {
s.srv.Shutdown(ctx)
if err := s.node.Shutdown(); err != nil {
return err
}
return nil
}
// Start the service
func (s *Service) Start() error {
// start the raft node
if err := s.node.Start(); err != nil {
return err
}
// start the http service
go func() {
if err := s.srv.Serve(s.listener); err != nil {
glog.Infof("server closed %v", err)
}
}()
go func() {
ticker := time.NewTicker(time.Minute * time.Duration(s.snapshotInterval))
for {
select {
case <-ticker.C:
glog.Infof("take snapshot => %v", s.node.Snapshot())
}
}
}()
glog.Infof("======> join addr %v%v\n", getOutboundIP(), s.httpAddr)
glog.Infof("======> open ui http://%v%v/ui or http://localhost%v/ui\n", getOutboundIP(), s.httpAddr, s.httpAddr)
return nil
}
func getOutboundIP() net.IP {
conn, err := net.Dial("udp", "8.8.8.8:80")
if err != nil {
log.Fatal(err)
}
defer conn.Close()
localAddr := conn.LocalAddr().(*net.UDPAddr)
return localAddr.IP
}
// fileServer starts the file server and return the file
func fileServer(r chi.Router, path string) {
if strings.ContainsAny(path, "{}*") {
panic("FileServer does not permit URL parameters.")
}
fs := http.StripPrefix(path, http.FileServer(rice.MustFindBox("build").HTTPBox()))
path += "*"
r.Get(path, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fs.ServeHTTP(w, r)
}))
}
// New returns the http service wrapper for the store.
func New(cfg *config.Config) (*Service, error) {
node, err := store.NewNode(cfg)
if err != nil {
return nil, err
}
svc := &Service{
node: node,
snapshotInterval: cfg.SnapshotInterval,
httpAddr: cfg.HTTPAddr,
}
router := chi.NewRouter()
router.Use(middleware.Recoverer)
router.Use(func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
glog.Infof("Received request on %s %s", r.URL.String(), r.RequestURI)
next.ServeHTTP(w, r)
})
})
if cfg.EnableFileServer {
fileServer(router, "/ui")
}
router.Post("/event", svc.leaderProxy(svc.eventHandler))
router.Post("/event/sink/site247", svc.leaderProxy(svc.site247AlertHandler))
router.Post("/event/sink/icinga", svc.leaderProxy(svc.icingaAlertHandler))
router.Post("/event/sink/azure", svc.leaderProxy(svc.azureAlertHandler))
router.Get("/rules", svc.getRulesHandler)
router.Get("/rules/{id}", svc.getRuleHandler)
router.Get("/rules/{id}/executions", svc.getRulesExecutions)
router.Post("/rules", svc.leaderProxy(svc.addRuleHandler))
router.Put("/rules", svc.leaderProxy(svc.updateRuleHandler))
router.Delete("/rules/{id}", svc.leaderProxy(svc.removeRuleHandler))
router.Get("/scripts", svc.getScriptListHandler)
router.Get("/scripts/{id}", svc.getScriptHandler)
router.Post("/scripts", svc.leaderProxy(svc.addScriptHandler))
router.Put("/scripts", svc.leaderProxy(svc.updateScriptHandler))
router.Delete("/scripts/{id}", svc.leaderProxy(svc.removeScriptHandler))
router.Get("/leave/{id}", svc.leaveHandler)
router.Post("/join", svc.joinHandler)
router.NotFound(func(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "/ui/"+r.URL.String(), 302)
})
srv := &http.Server{
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
IdleTimeout: 60 * time.Second,
Handler: router,
}
svc.srv = srv
svc.listener = cfg.HTTPListener
return svc, nil
}