-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
257 lines (213 loc) · 6.04 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
package main
import (
"crypto/tls"
"encoding/json"
"expvar"
"flag"
"net/http"
"runtime"
"runtime/pprof"
"strings"
"time"
"github.com/sirupsen/logrus"
prefixed "github.com/x-cray/logrus-prefixed-formatter"
"golang.org/x/crypto/acme/autocert"
"os"
"github.com/gorilla/mux"
"github.com/gorilla/websocket"
"github.com/rs/cors"
)
var (
// Tag is set by Gitlab's CI build process
Tag string
// Build is set by Gitlab's CI build process
Build string
// WebhookURL is the url for the webhook
WebhookURL string
// WebHookHeaders is an env var to transmit HTTP headers in JSON
WebHookHeaders map[string]string
// WebhookBearerToken is the bearer token passed to the webhook
WebhookBearerToken string
// SecretKey is used to check JWT tokens signatures
SecretKey string
)
var upgrader = websocket.Upgrader{
ReadBufferSize: 2048,
WriteBufferSize: 4096,
}
var log = logrus.New()
func main() {
var goroutines = expvar.NewInt("num_goroutine")
var interval = time.Duration(5) * time.Second
go func() {
for {
<-time.After(interval)
// The next line goes after the runtime.NumGoroutine() call
goroutines.Set(int64(runtime.NumGoroutine()))
}
}()
log.Formatter = &prefixed.TextFormatter{
DisableTimestamp: true,
ForceFormatting: true,
}
loglevel := os.Getenv("LOGLEVEL")
switch loglevel {
case "debug":
log.SetLevel(logrus.DebugLevel)
case "info":
log.SetLevel(logrus.InfoLevel)
case "warn":
log.SetLevel(logrus.WarnLevel)
case "error":
log.SetLevel(logrus.ErrorLevel)
default:
log.SetLevel(logrus.InfoLevel)
}
log.SetOutput(os.Stdout)
if Build != "" {
if Tag == "" {
log.Infof("GeeoServer - build %s", Build)
} else {
log.Infof("GeeoServer %s - build %s", Tag, Build)
}
} else {
log.Infof("GeeoServer development version")
}
var hostPort = flag.String("host", "localhost:8000", "host and port for http server")
var dbfile = flag.String("db", "bolt.db", "database file name")
var secret = flag.String("secret", "developmentKey", "secret for JWT signatures")
var cpuprofile = flag.String("cpuprofile", "", "write cpu profile to file")
var memprofile = flag.String("memprofile", "", "write memory profile to this file")
var ssl = flag.Bool("ssl", false, "Enable SSL support")
var sslhost = flag.String("sslhost", "", "FQDN for the SSL certificate")
var dev = flag.Bool("dev", false, "allow development routes")
flag.Parse()
var webhookwriter *WebhookWriter
SecretKey = *secret
if wh := os.Getenv("WEBHOOK_URL"); wh != "" {
WebhookURL = wh
if whbt := os.Getenv("WEBHOOK_BEARER"); whbt != "" {
WebhookBearerToken = whbt
}
if whh := os.Getenv("WEBHOOK_HEADERS"); whh != "" {
err := json.Unmarshal([]byte(whh), &WebHookHeaders)
if err != nil {
log.Error(err)
log.Fatal("Can't JSON parse WEBHOOK_HEADERS")
}
webhookwriter = NewWebhookWriter(wh, WebHookHeaders, WebhookBearerToken)
} else {
webhookwriter = NewWebhookWriter(wh, nil, WebhookBearerToken)
}
}
if s := os.Getenv("SECRET"); s != "" {
SecretKey = s
}
if hp := os.Getenv("HOST_PORT"); hp != "" {
hostPort = &hp
}
if dbname := os.Getenv("DB_NAME"); dbname != "" {
dbfile = &dbname
}
if envSSL := os.Getenv("SSL"); envSSL != "" {
*ssl = true
}
if envSSLhost := os.Getenv("SSL_HOST"); envSSLhost != "" {
sslhost = &envSSLhost
}
if envDev := os.Getenv("DEV"); envDev != "" {
*dev = true
}
if *cpuprofile != "" {
after2min := time.After(time.Minute * 2)
log.Debug("Setting up CPU Prof")
f, err := os.Create(*cpuprofile)
if err != nil {
log.Fatal(err)
}
pprof.StartCPUProfile(f)
go func() {
<-after2min
log.Debug("Starting CPU prof output")
pprof.StopCPUProfile()
log.Debug("Done CPU prof output")
}()
}
if *memprofile != "" {
after2min := time.After(time.Minute * 1)
log.Debug("Setting up Mem Prof")
f, err := os.Create(*memprofile)
if err != nil {
log.Fatal(err)
}
go func() {
<-after2min
log.Debug("Starting Mem prof output")
pprof.WriteHeapProfile(f)
log.Debug("Done Mem prof output")
f.Close()
}()
}
//persister := newNullPersister()
persister := newBoltDBPersister(*dbfile)
defer persister.close()
geeodb := NewGeeoDB(persister, 5)
wshandler := NewWSRouter(geeodb, webhookwriter)
r := mux.NewRouter()
upgrader.CheckOrigin = func(r *http.Request) bool { return true }
r.HandleFunc("/ws", wshandler.handle(upgrader))
subrouter := r.PathPrefix("/api").Subrouter()
NewHTTPRouter(subrouter, geeodb, wshandler)
// TODO make it really private
r.HandleFunc("/api/private/backup", persister.BackupHandleFunc)
r.HandleFunc("/api/private/jsondump", persister.JSONDumpHandleFunc)
if *dev {
r.HandleFunc("/api/dev/token", DevHelperGetToken)
}
corsOptions := cors.Options{
AllowedMethods: []string{"GET", "POST", "DELETE"},
AllowedHeaders: []string{"X-GEEO-TOKEN"},
}
if o := os.Getenv("ORIGIN"); o != "" {
corsOptions.AllowedOrigins = strings.Split(o, ",")
}
c := cors.New(corsOptions)
withCors := c.Handler(r) // TODO LATER finer handling of allowed origins
http.Handle("/", withCors)
if *ssl {
certManager := autocert.Manager{
Prompt: autocert.AcceptTOS,
HostPolicy: autocert.HostWhitelist(*sslhost),
Cache: autocert.DirCache("certs"), // TODO LATER store in bolt db or distributed db ?
Email: "[email protected]", // TODO env var
ForceRSA: true,
}
srv := &http.Server{
Addr: ":https",
TLSConfig: &tls.Config{
GetCertificate: func(hello *tls.ClientHelloInfo) (*tls.Certificate, error) {
if hello.ServerName == "" {
hello.ServerName = *sslhost
}
return certManager.GetCertificate(hello)
},
//NextProtos: []string{"h2", "http/1.1", acme.ALPNProto},
MinVersion: tls.VersionTLS10,
PreferServerCipherSuites: true,
},
}
log.Info("TLS Server starting")
s := &http.Server{
Handler: certManager.HTTPHandler(nil),
Addr: ":80",
}
go s.ListenAndServe()
log.Fatal(srv.ListenAndServeTLS("", ""))
return
}
log.Info("Server starting at ", *hostPort)
srv := &http.Server{
Addr: *hostPort,
}
log.Fatal(srv.ListenAndServe())
}