-
Notifications
You must be signed in to change notification settings - Fork 16
/
config.go
91 lines (74 loc) · 1.94 KB
/
config.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
package main
import(
"net/http"
"time"
"github.com/gorilla/websocket"
"github.com/ianschenck/envflag"
log "github.com/sirupsen/logrus"
)
type config struct {
Upgrader websocket.Upgrader
jwtSecret []byte
port string
version string
readBufferSize int
writeBufferSize int
maxConnections int
checkOrigin bool
log log.Level
launchUnixTime int64
}
func LoadConfig() (*config) {
c := &config{}
secret := envflag.String(
"SECRET",
"",
"JWT secret used to validate access keys.")
port := envflag.String(
"PORT",
"6380",
"Port in which to serve Philote websocket connections")
logLevel := envflag.String(
"LOGLEVEL",
"info",
"Log level, accepts: 'debug', 'info', 'warning', 'error', 'fatal', 'panic'")
maxConnections := envflag.Int(
"MAX_CONNECTIONS",
255,
"Maximum amount of permitted concurrent connections")
readBufferSize := envflag.Int(
"READ_BUFFER_SIZE",
1024,
"Size (in bytes) for the read buffer")
writeBufferSize := envflag.Int(
"WRITE_BUFFER_SIZE",
1024,
"Size (in bytes) for the write buffer")
checkOrigin := envflag.Bool(
"CHECK_ORIGIN",
false,
"Compare the Origin and Host request header during websocket handshake")
envflag.Parse()
c.jwtSecret = []byte(*secret)
c.port = *port
c.maxConnections = *maxConnections
c.readBufferSize = *readBufferSize
c.writeBufferSize = *writeBufferSize
c.checkOrigin = *checkOrigin
c.Upgrader = websocket.Upgrader{
ReadBufferSize: c.readBufferSize,
WriteBufferSize: c.writeBufferSize,
}
if !c.checkOrigin {
c.Upgrader.CheckOrigin = func(r *http.Request) bool {
return true
}
}
c.launchUnixTime = time.Now().Unix()
var err error
c.log, err = log.ParseLevel(*logLevel); if err != nil {
log.WithFields(log.Fields{"error": err}).Panic("Unparsable log level")
}
log.SetLevel(c.log)
return c
}