-
Notifications
You must be signed in to change notification settings - Fork 58
/
config.go
78 lines (70 loc) · 2.52 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
package main
import (
"encoding/json"
"log"
"os"
"time"
"github.com/gammazero/nexus/v3/router"
)
type Config struct {
// Websocket configuration parameters.
WebSocket struct {
// String form of address (example, "192.0.2.1:25", "[2001:db8::1]:80")
Address string `json:"address"`
// Files containing a certificate and matching private key.
CertFile string `json:"cert_file"`
KeyFile string `json:"key_file"`
// Heartbeat ("pings") interval in seconds. Set to 0 to disable.
KeepAlive time.Duration `json:"keep_alive"`
// Enable per message write compression.
EnableCompression bool `json:"enable_compression"`
// Enable sending cookie to identify client in later connections.
EnableTrackingCookie bool `json:"enable_tracking_cookie"`
// Enable reading HTTP header from client requests.
EnableRequestCapture bool `json:"enable_request_capture"`
// Allow origins that match these glob patterns when an origin header
// is present in the websocket upgrade request.
AllowOrigins []string `json:"allow_origins"`
// Limit on number of pending messages to send to each client.
OutQueueSize int `json:"out_queue_size"`
}
// RawSocket configuration parameters.
RawSocket struct {
// String form of address (example, "192.0.2.1:25", "[2001:db8::1]:80")
TCPAddress string `json:"tcp_address"`
// TCP keepalive interval in seconds. Set to 0 to disable.
TCPKeepAliveInterval time.Duration `json:"tcp_keepalive_interval"`
// Path to Unix domain socket.
UnixAddress string `json:"unix_address"`
// Maximum message length server can receive. Default = 16M.
MaxMsgLen int `json:"max_msg_len"`
// Files containing a certificate and matching private key.
CertFile string `json:"cert_file"`
KeyFile string `json:"key_file"`
// Limit on number of pending messages to send to each client.
OutQueueSize int `json:"out_queue_size"`
}
// File to write log data to. If not specified, log to stdout.
LogPath string `json:"log_path"`
// Router configuration parameters.
// See https://godoc.org/github.com/gammazero/nexus/v3#RouterConfig
Router router.Config
}
func LoadConfig(path string) *Config {
file, err := os.ReadFile(path)
if err != nil {
log.Fatal("Config File Missing. ", err)
}
var config Config
err = json.Unmarshal(file, &config)
if err != nil {
log.Fatal("Config Parse Error: ", err)
}
if config.WebSocket.KeepAlive != 0 {
config.WebSocket.KeepAlive *= time.Second
}
if config.RawSocket.TCPKeepAliveInterval != 0 {
config.RawSocket.TCPKeepAliveInterval *= time.Second
}
return &config
}