-
-
Notifications
You must be signed in to change notification settings - Fork 27
/
config.go
156 lines (133 loc) · 3.27 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
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
package main
import (
"fmt"
uj "github.com/nanoscopic/ujsonin/v2/mod"
log "github.com/sirupsen/logrus"
"io/ioutil"
"os"
"time"
)
type CDevice struct {
udid string
}
type ConfigText struct {
deviceVideo string
}
type Config struct {
listen string
https bool
crt string
key string
auth string
root uj.JNode
idleTimeout int
maxHeight int
text *ConfigText
disableCache bool
theme string
notes uj.JNode
}
func (self *Config) String() string {
https := "false"
if self.https {
https = "true"
}
return fmt.Sprintf("Listen: %s\nHTTPS: %s\n", self.listen, https)
}
func GetStr(root uj.JNode, path string) string {
node := root.Get(path)
if node == nil {
fmt.Fprintf(os.Stderr, "%s is not set in either config.json or default.json", path)
os.Exit(1)
}
return node.String()
}
func GetBool(root uj.JNode, path string) bool {
node := root.Get(path)
if node == nil {
fmt.Fprintf(os.Stderr, "%s is not set in either config.json or default.json", path)
os.Exit(1)
}
return node.Bool()
}
func GetInt(root uj.JNode, path string) int {
node := root.Get(path)
if node == nil {
fmt.Fprintf(os.Stderr, "%s is not set in either config.json or default.json", path)
os.Exit(1)
}
return node.Int()
}
func NewConfig(configPath string, defaultsPath string) *Config {
config := Config{
auth: "builtin",
}
root := loadConfig(configPath, defaultsPath)
config.root = root
config.listen = GetStr(root, "listen")
config.https = GetBool(root, "https")
if config.https {
config.key = GetStr(root, "key")
config.crt = GetStr(root, "crt")
}
idleTimeout := GetStr(root, "idleTimeout")
if idleTimeout == "" {
config.idleTimeout = 0
} else {
dur, _ := time.ParseDuration(idleTimeout)
config.idleTimeout = int(dur.Seconds())
}
authNode := config.root.Get("auth")
if authNode != nil {
config.auth = GetStr(authNode, "type")
}
config.maxHeight = GetInt(root, "video.maxHeight")
config.text = &ConfigText{
deviceVideo: GetStr(root, "text.deviceVideo"),
}
config.disableCache = GetBool(root, "disableCache")
config.theme = GetStr(root, "theme")
config.notes = root.Get("notes")
return &config
}
func loadConfig(configPath string, defaultsPath string) uj.JNode {
fh1, serr1 := os.Stat(defaultsPath)
if serr1 != nil {
log.WithFields(log.Fields{
"type": "err_read_defaults",
"error": serr1,
"defaults_path": defaultsPath,
}).Fatal("Could not read specified defaults path")
}
defaultsFile := defaultsPath
switch mode := fh1.Mode(); {
case mode.IsDir():
defaultsFile = fmt.Sprintf("%s/default.json", defaultsPath)
}
content1, err1 := ioutil.ReadFile(defaultsFile)
if err1 != nil {
log.Fatal(err1)
}
defaults, _ := uj.Parse(content1)
fh, serr := os.Stat(configPath)
if serr != nil {
log.WithFields(log.Fields{
"type": "err_read_config",
"error": serr,
"config_path": configPath,
}).Fatal("Could not read specified config path")
}
configFile := configPath
switch mode := fh.Mode(); {
case mode.IsDir():
configFile = fmt.Sprintf("%s/config.json", configPath)
}
content, err := ioutil.ReadFile(configFile)
if err != nil {
log.Fatal(err)
}
root, _ := uj.Parse(content)
defaults.Overlay(root)
//defaults.Dump()
return defaults
}