-
Notifications
You must be signed in to change notification settings - Fork 10
/
siridb-http.go
483 lines (409 loc) · 12.3 KB
/
siridb-http.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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
package main
import (
"encoding/json"
"errors"
"fmt"
"net/http"
"os"
"os/signal"
"strconv"
"strings"
"time"
siridb "github.com/SiriDB/go-siridb-connector"
kingpin "github.com/alecthomas/kingpin/v2"
"github.com/astaxie/beego/session"
socketio "github.com/googollee/go-socket.io"
ini "gopkg.in/ini.v1"
)
// AppVersion exposes version information
const AppVersion = "2.0.20"
const retryConnectTime = 5
// Conn is used to store the user/password with the client.
type Conn struct {
user string
password string
client *siridb.Client
}
type store struct {
connections []Conn
dbname string
timePrecision string
version string
servers []server
port uint16
insertTimeout uint16
logCh chan string
reqAuth bool
multiUser bool
enableWeb bool
enableSio bool
enableSSL bool
enableBasicAuth bool
ssessions map[string]string
cookieMaxAge uint64
crtFile string
keyFile string
gsessions *session.Manager
}
type server struct {
host string
port uint16
}
var (
xApp = kingpin.New("siridb-http", "Provides a HTTP API and optional web interface for SiriDB.")
xConfig = xApp.Flag("config", "Configuration and connection file for SiriDB HTTP.").Default("").Short('c').String()
xVerbose = xApp.Flag("verbose", "Enable verbose logging.").Bool()
xVersion = xApp.Flag("version", "Print version information and exit.").Short('v').Bool()
)
var base = store{}
func getHostAndPort(addr string) (server, error) {
parts := strings.Split(addr, ":")
// IPv4
if len(parts) == 1 {
return server{parts[0], 9000}, nil
}
if len(parts) == 2 {
u, err := strconv.ParseUint(parts[1], 10, 16)
return server{parts[0], uint16(u)}, err
}
// IPv6
if addr[0] != '[' {
return server{fmt.Sprintf("[%s]", addr), 9000}, nil
}
if addr[len(addr)-1] == ']' {
return server{addr, 9000}, nil
}
u, err := strconv.ParseUint(parts[len(parts)-1], 10, 16)
addr = strings.Join(parts[:len(parts)-1], ":")
return server{addr, uint16(u)}, err
}
func getServers(addrstr string) ([]server, error) {
arr := strings.Split(addrstr, ",")
servers := make([]server, len(arr))
for i, addr := range arr {
addr = strings.TrimSpace(addr)
server, err := getHostAndPort(addr)
if err != nil {
return nil, err
}
servers[i] = server
}
return servers, nil
}
func serversToInterface(servers []server) [][]interface{} {
ret := make([][]interface{}, len(servers))
for i, svr := range servers {
ret[i] = make([]interface{}, 2)
ret[i][0] = svr.host
ret[i][1] = int(svr.port)
}
return ret
}
func logHandle(logCh chan string) {
for {
msg := <-logCh
if *xVerbose {
println(msg)
}
}
}
func sigHandle(sigCh chan os.Signal) {
for {
<-sigCh
quit(nil)
}
}
func quit(err error) {
rc := 0
if err != nil {
fmt.Printf("%s\n", err)
rc = 1
}
for _, conn := range base.connections {
if conn.client != nil {
conn.client.Close()
}
}
os.Exit(rc)
}
func connect(conn Conn) {
for !conn.client.IsConnected() {
base.logCh <- fmt.Sprintf("not connected to SiriDB, try again in %d seconds", retryConnectTime)
time.Sleep(retryConnectTime * time.Second)
}
res, err := conn.client.Query("show time_precision, version", 10)
if err != nil {
quit(err)
}
v, ok := res.(map[string]interface{})
if !ok {
quit(fmt.Errorf("missing 'map' in data"))
}
arr, ok := v["data"].([]interface{})
if !ok || len(arr) != 2 {
quit(fmt.Errorf("missing array 'data' or length 2 in map"))
}
base.timePrecision, ok = arr[0].(map[string]interface{})["value"].(string)
base.version, ok = arr[1].(map[string]interface{})["value"].(string)
if !ok {
quit(fmt.Errorf("cannot find time_precision and/or version in data"))
}
}
func readBool(section *ini.Section, key string) (b bool) {
if bIni, err := section.GetKey(key); err != nil {
quit(err)
} else if b, err = bIni.Bool(); err != nil {
quit(err)
}
return b
}
func readString(section *ini.Section, key string) (s string) {
if sIni, err := section.GetKey(key); err != nil {
quit(err)
} else {
s = sIni.String()
}
return s
}
type customServer struct {
Server *socketio.Server
}
func (s *customServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Credentials", "true")
origin := r.Header.Get("Origin")
w.Header().Set("Access-Control-Allow-Origin", origin)
s.Server.ServeHTTP(w, r)
}
func main() {
// parse arguments
_, err := xApp.Parse(os.Args[1:])
if err != nil {
quit(err)
}
if *xVersion {
fmt.Printf("%s\n", AppVersion)
os.Exit(0)
}
if *xConfig == "" {
fmt.Printf(
`# SiriDB HTTP Configuration file
[Database]
# User with at least 'show' privileges.
user = <your_username>
# A password is required. To protect the password this file should be placed in
# a folder where unauthorized users have no access.
password = <your_password>
# Database to connect to.
dbname = <your_database>
# Multiple servers are allowed and should be comma separated. When a port
# is not provided the default 9000 is used. IPv6 address are supported and
# should be wrapped in square brackets [] in case an alternative port is
# required. SiriDB HTTP will randomly select an available siridb server
# for each request.
#
# Valid examples:
# siridb01.local,siridb02.local,siridb03.local,siridb04.local
# 10.20.30.40
# [::1]:5050,[::1]:5051
# 2001:0db8:85a3:0000:0000:8a2e:0370:7334
servers = localhost
[Configuration]
# Listening to TCP port.
port = 5050
# When disabled no authentication is required. When enabled session
# authentication or basic authentication is required.
require_authentication = True
# When enabled /socket.io/ will be enabled and Socket-IO can be used as an
# alternative to the standard http rest api.
enable_socket_io = True
# When enabled the crt_file and key_file must be configured and the server
# will be hosted on https.
enable_ssl = False
# When enabled a website is hosted on the configured port. When disabled the
# resource URIs like /query, /insert, /auth/.. etc. are still available.
enable_web = True
# When enabled the /query and /insert resource URIs can be used with basic
# authentication.
enable_basic_auth = False
# When multi user is disabled, only the user/password combination provided in
# this configuration file can be used.
enable_multi_user = False
# Cookie max age is used to set the cookie expiration time in seconds.
cookie_max_age = 604800
# The query api allows you to specify a timeout for each query, but the insert
# api only accepts data. Therefore the insert timeout is set as a general
# value and is applicable to each insert.
insert_timeout = 60
[SSL]
# Self-signed certificates can be created with the following command:
#
# openssl req -x509 -nodes -days 365 -newkey rsa:2048 \
# -keyout certificate.key -out certificate.crt
#
crt_file = certificate.crt
key_file = certificate.key
#
# Welcome and thank you for using SiriDB!
#
# A configuration file is required and shoud be provided with the
# --config <file> argument.
# Above you find an example template which can be used.
#
`)
os.Exit(0)
}
var conn Conn
cfg, err := ini.Load(*xConfig)
if err != nil {
quit(err)
}
section, err := cfg.GetSection("Database")
if err != nil {
quit(err)
}
base.servers, err = getServers(readString(section, "servers"))
if err != nil {
quit(err)
}
base.dbname = readString(section, "dbname")
conn.user = readString(section, "user")
conn.password = readString(section, "password")
base.logCh = make(chan string)
go logHandle(base.logCh)
sigCh := make(chan os.Signal, 1)
signal.Notify(sigCh, os.Interrupt)
go sigHandle(sigCh)
conn.client = siridb.NewClient(
conn.user, // user
conn.password, // password
base.dbname, // database
serversToInterface(base.servers), // siridb server(s)
base.logCh, // optional log channel
)
base.connections = append(base.connections, conn)
base.ssessions = make(map[string]string)
section, err = cfg.GetSection("Configuration")
if err != nil {
quit(err)
}
base.reqAuth = readBool(section, "require_authentication")
base.enableWeb = readBool(section, "enable_web")
base.enableSio = readBool(section, "enable_socket_io")
base.enableSSL = readBool(section, "enable_ssl")
base.multiUser = readBool(section, "enable_multi_user")
base.enableBasicAuth = readBool(section, "enable_basic_auth")
if portIni, err := section.GetKey("port"); err != nil {
quit(err)
} else if port64, err := portIni.Uint64(); err != nil {
quit(err)
} else {
base.port = uint16(port64)
}
if cookieMaxAgeIni, err := section.GetKey("cookie_max_age"); err != nil {
quit(err)
} else if base.cookieMaxAge, err = cookieMaxAgeIni.Uint64(); err != nil {
quit(err)
}
if insertTimeoutIni, err := section.GetKey("insert_timeout"); err != nil {
quit(err)
} else if insertTimeout64, err := insertTimeoutIni.Uint64(); err != nil {
quit(err)
} else {
base.insertTimeout = uint16(insertTimeout64)
}
if base.enableSSL {
section, err = cfg.GetSection("SSL")
if err != nil {
quit(err)
}
base.crtFile = readString(section, "crt_file")
base.keyFile = readString(section, "key_file")
}
http.HandleFunc("*", handlerNotFound)
if base.enableWeb {
http.HandleFunc("/", handlerMain)
http.HandleFunc("/js/bundle", handlerJsBundle)
http.HandleFunc("/css/bootstrap", handlerBootstrapCSS)
http.HandleFunc("/css/layout", handlerLayout)
http.HandleFunc("/favicon.ico", handlerFaviconIco)
http.HandleFunc("/img/siridb-large.png", handlerSiriDBLargePNG)
http.HandleFunc("/img/siridb-small.png", handlerSiriDBSmallPNG)
http.HandleFunc("/img/loader.gif", handlerLoaderGIF)
http.HandleFunc("/css/font-awesome.min.css", handlerFontAwesomeMinCSS)
http.HandleFunc("/fonts/FontAwesome.otf", handlerFontsFaOTF)
http.HandleFunc("/fonts/fontawesome-webfont.eot", handlerFontsFaEOT)
http.HandleFunc("/fonts/fontawesome-webfont.svg", handlerFontsFaSVG)
http.HandleFunc("/fonts/fontawesome-webfont.ttf", handlerFontsFaTTF)
http.HandleFunc("/fonts/fontawesome-webfont.woff", handlerFontsFaWOFF)
http.HandleFunc("/fonts/fontawesome-webfont.woff2", handlerFontsFaWOFF2)
}
http.HandleFunc("/db-info", handlerDbInfo)
http.HandleFunc("/auth/fetch", handlerAuthFetch)
http.HandleFunc("/query", handlerQuery)
http.HandleFunc("/insert", handlerInsert)
cf := new(session.ManagerConfig)
cf.EnableSetCookie = true
s := fmt.Sprintf(`{"cookieName":"siridbadminsessionid","gclifetime":%d}`, base.cookieMaxAge)
if err = json.Unmarshal([]byte(s), cf); err != nil {
quit(err)
}
if base.gsessions, err = session.NewManager("memory", cf); err != nil {
quit(err)
}
go base.gsessions.GC()
http.HandleFunc("/auth/login", handlerAuthLogin)
http.HandleFunc("/auth/logout", handlerAuthLogout)
conn.client.Connect()
go connect(conn)
if base.enableSio {
server := socketio.NewServer(nil)
if server != nil {
quit(errors.New("failed to create server"))
}
server.OnConnect("/", func(s socketio.Conn) error {
s.SetContext("/")
return nil
})
server.OnEvent("/", "db-info", func(so socketio.Conn, _ string) (int, interface{}) {
return onDbInfo(&so)
})
server.OnEvent("/", "auth fetch", func(so socketio.Conn, _ string) (int, interface{}) {
return onAuthFetch(&so)
})
server.OnEvent("/", "auth login", func(so socketio.Conn, req tAuthLoginReq) (int, interface{}) {
return onAuthLogin(&so, &req)
})
server.OnEvent("/", "query", func(so socketio.Conn, req tQuery) (int, interface{}) {
return onQuery(&so, &req)
})
server.OnEvent("/", "insert", func(so socketio.Conn, req interface{}) (int, interface{}) {
return onInsert(&so, &req)
})
server.OnDisconnect("disconnection", func(so socketio.Conn, _ string) {
delete(base.ssessions, so.ID())
})
server.OnError("error", func(so socketio.Conn, err error) {
base.logCh <- fmt.Sprintf("socket.io error: %s", err.Error())
})
go server.Serve()
defer server.Close()
http.Handle("/socket.io/", &customServer{Server: server})
}
msg := "Serving SiriDB API on http%s://0.0.0.0:%d\n"
if base.enableSSL {
fmt.Printf(msg, "s", base.port)
if err = http.ListenAndServeTLS(
fmt.Sprintf(":%d", base.port),
base.crtFile,
base.keyFile,
nil); err != nil {
fmt.Printf("error: %s\n", err)
}
} else {
fmt.Printf(msg, "", base.port)
if err = http.ListenAndServe(fmt.Sprintf(":%d", base.port), nil); err != nil {
fmt.Printf("error: %s\n", err)
}
}
quit(nil)
}