-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.go
188 lines (171 loc) · 4.57 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
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package main
import (
"context"
"fmt"
"github.com/clawio/lib"
"github.com/go-kit/kit/log/levels"
"github.com/gorilla/handlers"
"github.com/gorilla/mux"
"github.com/prometheus/client_golang/prometheus"
"io"
"net/http"
"os"
"time"
)
type server struct {
logger levels.Levels
router http.Handler
config lib.Configuration
httpLogger io.Writer
registryDriver lib.RegistryDriver
webServices map[string]lib.WebService
}
func newServer(config lib.Configuration) (*server, error) {
logger, err := getLogger(config)
if err != nil {
return nil, err
}
registryDriver, err := getRegistryDriver(config)
if err != nil {
return nil, err
}
s := &server{logger: logger, config: config, registryDriver: registryDriver}
err = s.configureRouter()
if err != nil {
return nil, err
}
// do the register in other routine repeatedly to avoid the node
// being removed by the TTL constraint
go func() {
err = s.registerNode()
if err != nil {
s.logger.Error().Log("error", "error registering node")
}
for range time.Tick(time.Second * 5) {
err = s.registerNode()
if err != nil {
s.logger.Error().Log("error", "error registering node")
}
}
}()
return s, nil
}
func (s *server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
handlers.CombinedLoggingHandler(s.httpLogger, s.router).ServeHTTP(w, r)
}
func (s *server) registerNode() error {
hostname, err := os.Hostname()
if err != nil {
s.logger.Error().Log("error", err)
return err
}
for key, ws := range s.webServices {
rol := key + "-node"
if ws.IsProxy() {
rol = rol + "-proxy"
}
url := fmt.Sprintf("%s:%d", hostname, s.config.GetPort())
if s.config.IsTLSEnabled() {
url = fmt.Sprintf("https://%s", url)
} else {
url = fmt.Sprintf("http://%s", url)
}
node := &node{
xhost: fmt.Sprintf("%s:%d", hostname, s.config.GetPort()),
xid: fmt.Sprintf("%s:%d", hostname, s.config.GetPort()),
xrol: rol,
xurl: url,
xversion: "TODO"}
err := s.registryDriver.Register(context.Background(), node)
if err != nil {
s.logger.Error().Log("error", err)
return err
}
}
return nil
}
func (s *server) configureRouter() error {
config := s.config
httpLogger, err := getHTTPLogger(config)
if err != nil {
s.logger.Error().Log("error", err)
return err
}
s.httpLogger = httpLogger
loggerMiddleware, err := getLoggerMiddleware(config)
if err != nil {
s.logger.Error().Log("error", err)
return err
}
corsMiddleware, err := getCORSMiddleware(config)
if err != nil {
s.logger.Error().Log("error", err)
return err
}
webServices, err := getWebServices(config)
if err != nil {
s.logger.Error().Log("error", err)
return err
}
s.logger.Info().Log("msg", "web services enabled", "webservices", config.GetEnabledWebServices())
s.webServices = webServices
router := mux.NewRouter()
router.Handle("/metrics", prometheus.Handler()).Methods("GET")
s.logger.Info().Log("method", "GET", "endpoint", "/metrics", "msg", "endpoint available - created by prometheus")
for key, service := range webServices {
s.logger.Info().Log("msg", key+" web service enabled")
for path, methods := range service.Endpoints() {
for method, handlerFunc := range methods {
handlerFunc = loggerMiddleware.HandlerFunc(handlerFunc)
handlerFunc := http.HandlerFunc(handlerFunc)
var handler http.Handler
if config.IsCORSMiddlewareEnabled() {
handler = handlerFunc
handler = corsMiddleware.Handler(handler)
if method == "*" {
router.Handle(path, handler)
} else {
router.Handle(path, handler).Methods(method)
}
prometheus.InstrumentHandler(path, handler)
s.logger.Info().Log("method", method, "endpoint", path, "msg", "endpoint available")
router.Handle(path, handler).Methods("OPTIONS")
s.logger.Info().Log("method", "OPTIONS", "endpoint", path, "msg", "endpoint available - created by corsmiddleware")
} else {
handler = handlerFunc
if method == "*" {
router.Handle(path, handler)
} else {
router.Handle(path, handler).Methods(method)
}
prometheus.InstrumentHandler(path, handler)
s.logger.Info().Log("method", method, "endpoint", path, "msg", "endpoint available")
}
}
}
}
s.router = router
return nil
}
type node struct {
xid string
xrol string
xhost string
xversion string
xurl string
}
func (n *node) ID() string {
return n.xid
}
func (n *node) Rol() string {
return n.xrol
}
func (n *node) Host() string {
return n.xhost
}
func (n *node) Version() string {
return n.xversion
}
func (n *node) URL() string {
return n.xurl
}