This repository has been archived by the owner on Jul 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
main.go
333 lines (280 loc) · 8.47 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
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
package main
/*
* Microservice gateway application
* Copyright (C) 2015 Martin Helmich <[email protected]>
* Mittwald CM Service GmbH & Co. KG
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import (
"bytes"
"encoding/json"
"flag"
"fmt"
"html/template"
"io/ioutil"
"net/http"
"os"
"os/signal"
"runtime/pprof"
"strings"
"github.com/braintree/manners"
"github.com/gomodule/redigo/redis"
"github.com/hashicorp/consul/api"
"github.com/mittwald/servicegateway/auth"
"github.com/mittwald/servicegateway/config"
"github.com/mittwald/servicegateway/dispatcher"
"github.com/mittwald/servicegateway/httplogging"
"github.com/mittwald/servicegateway/monitoring"
"github.com/mittwald/servicegateway/proxy"
"github.com/op/go-logging"
)
func main() {
startup := config.Startup{}
flag.StringVar(&startup.ConfigFile, "config", "/etc/servicegateway.json", "configuration file")
flag.StringVar(&startup.DispatchingMode, "dispatch", "path", "dispatching mode ('path' or 'host')")
flag.IntVar(&startup.Port, "port", 8080, "HTTP port to listen on")
flag.StringVar(&startup.AdminAddress, "admin-addr", "127.0.0.1", "Address to listen on (administration port)")
flag.IntVar(&startup.AdminPort, "admin-port", 8081, "HTTP port to listen on (administration port)")
flag.StringVar(&startup.MonitorAddress, "monitor-addr", "0.0.0.0", "Address to listen on (monitoring port)")
flag.IntVar(&startup.MonitorPort, "monitor-port", 8082, "HTTP port to listen on (monitoring port)")
flag.BoolVar(&startup.Debug, "debug", false, "enable to add debug information to each request")
flag.StringVar(&startup.ConsulBaseKey, "consul-base", "", "base key name for configuration")
flag.StringVar(&startup.UiDir, "ui-dir", "/usr/share/servicegateway", "directory in which UI files can be found")
flag.StringVar(&startup.ProfileCpu, "cpu-profile", "", "write CPU profile to file")
flag.Parse()
logger := logging.MustGetLogger("startup")
format := logging.MustStringFormatter("%{color}%{time:15:04:05.000} %{module:12s} ▶ %{level:.4s} %{id:03x}%{color:reset} %{message}")
backend := logging.NewLogBackend(os.Stderr, "", 0)
if startup.ProfileCpu != "" {
f, err := os.Create(startup.ProfileCpu)
if err != nil {
logger.Fatal(err)
}
if err := pprof.StartCPUProfile(f); err != nil {
logger.Fatal(err)
}
defer pprof.StopCPUProfile()
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
go func() {
for range c {
pprof.StopCPUProfile()
os.Exit(0)
}
}()
}
logging.SetBackend(logging.NewBackendFormatter(backend, format))
if !startup.Debug {
logging.SetLevel(logging.INFO, "")
}
logger.Info("Completed startup")
// read in config file to get raw content
rawCfgContent, err := ioutil.ReadFile(startup.ConfigFile)
if err != nil {
logger.Fatal(err)
}
// create a new template from the raw content of our config file
var tpl *template.Template
tpl, err = template.New("").Parse(string(rawCfgContent))
if err != nil {
logger.Fatal(err)
}
// prepare template data
type templateData struct {
Env map[string]string
}
data := templateData{
Env: make(map[string]string),
}
// load all env-vars into template data
for _, e := range os.Environ() {
e := strings.SplitN(e, "=", 2)
if len(e) > 1 {
data.Env[e[0]] = e[1]
}
}
// render the raw config in order to replace env-variables (if given)
renderedCfgContent := new(bytes.Buffer)
err = tpl.Execute(renderedCfgContent, &data)
if err != nil {
logger.Fatal(err)
}
// unmarshal rendered config to proper json
cfg := config.Configuration{}
err = json.Unmarshal(renderedCfgContent.Bytes(), &cfg)
if err != nil {
logger.Fatal(err)
}
logger.Debugf("%s", cfg)
var monitoringController monitoring.Controller
monitoringLogger := logging.MustGetLogger("monitoring")
if startup.IsConsulConfig() {
consulClient, consulClientErr := cfg.Consul.BuildConsulClient()
if consulClientErr != nil {
logger.Panic(err)
}
monitoringController, err = monitoring.NewConsulIntegrationMonitoringController(
startup.MonitorAddress,
startup.MonitorPort,
consulClient,
monitoringLogger,
)
} else {
monitoringController, err = monitoring.NewNoIntegrationMonitoringController(
startup.MonitorAddress,
startup.MonitorPort,
monitoringLogger,
)
}
if err != nil {
logger.Fatal(err)
}
err = monitoringController.Start()
if err != nil {
logger.Fatal(err)
}
metrics := monitoringController.Metrics()
if err != nil {
logger.Fatal(err)
}
redisPool := &redis.Pool{
MaxIdle: 8,
Dial: func() (redis.Conn, error) {
conn, err := redis.Dial("tcp", cfg.Redis.Address, cfg.Redis.DialOptions()...)
if err != nil {
return nil, err
}
return conn, nil
},
}
tokenVerifier, err := auth.NewJwtVerifier(&cfg.Authentication)
if err != nil {
logger.Panic(err)
}
tokenStore, err := auth.NewTokenStore(redisPool, tokenVerifier, auth.TokenStoreOptions{})
if err != nil {
logger.Panic(err)
}
httpLoggers, err := buildLoggers(&cfg, tokenVerifier)
if err != nil {
logger.Panic(err)
}
handler := proxy.NewProxyHandler(logging.MustGetLogger("proxy"), &cfg, metrics)
listenAddress := fmt.Sprintf(":%d", startup.Port)
adminListenAddress := fmt.Sprintf("%s:%d", startup.AdminAddress, startup.AdminPort)
done := make(chan bool)
serverShutdown := make(chan bool)
serverShutdownComplete := make(chan bool)
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
go func() {
for range c {
logger.Notice("received interrupt signal")
monitoringController.SendShutdown()
serverShutdown <- true
}
}()
go func() {
monitoringController.WaitForShutdown()
<-serverShutdownComplete
logger.Notice("everything has shut down. exiting process.")
done <- true
}()
go func() {
var err error
var proxyServer, adminServer *manners.GracefulServer
shutdownServers := func() {
if proxyServer != nil {
logger.Debug("Closing proxy server")
proxyServer.Close()
}
if adminServer != nil {
logger.Debug("Closing admin server")
adminServer.Close()
}
}
go func() {
<-serverShutdown
logger.Noticef("received server shutdown request. stopping creating new servers")
shutdownServers()
serverShutdownComplete <- true
}()
var disp http.Handler
var adminHandler http.Handler
if startup.IsConsulConfig() {
var consulClient *api.Client
consulClient, err = cfg.Consul.BuildConsulClient()
if err != nil {
logger.Error(err.Error())
return
}
disp, adminHandler, err = dispatcher.BuildConsulDispatcher(
&startup,
&cfg,
consulClient,
handler,
redisPool,
logger,
tokenStore,
tokenVerifier,
httpLoggers,
)
} else {
disp, adminHandler, err = dispatcher.BuildNoIntegrationDispatcher(
&startup,
&cfg,
handler,
redisPool,
logger,
tokenStore,
tokenVerifier,
httpLoggers,
)
}
if err != nil {
logger.Error(err.Error())
return
}
shutdownServers()
proxyServer = manners.NewWithServer(&http.Server{Addr: listenAddress, Handler: disp})
adminServer = manners.NewWithServer(&http.Server{Addr: adminListenAddress, Handler: adminHandler})
logger.Debug("Starting new servers")
go func() {
logger.Infof("starting dispatcher on address %s", listenAddress)
_ = proxyServer.ListenAndServe()
}()
go func() {
logger.Infof("starting admin server on address %s", adminListenAddress)
_ = adminServer.ListenAndServe()
}()
}()
logger.Info("waiting to die")
<-done
}
func buildLoggers(cfg *config.Configuration, tok *auth.JwtVerifier) ([]httplogging.HttpLogger, error) {
loggers := make([]httplogging.HttpLogger, len(cfg.Logging))
for i, loggingConfig := range cfg.Logging {
loggingLogger, err := logging.GetLogger("logger-" + loggingConfig.Type)
if err != nil {
return nil, err
}
httpLogger, err := httplogging.LoggerFromConfig(&loggingConfig, loggingLogger, tok)
if err != nil {
return nil, err
}
loggers[i] = httpLogger
}
return loggers, nil
}