This repository has been archived by the owner on Jun 4, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
197 lines (168 loc) · 4.81 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
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
package main
import (
"fmt"
"log"
"os"
"path/filepath"
"github.com/spf13/viper"
"gopkg.in/yaml.v2"
)
var defaultConfigContent = `
Service:
Listen: "127.0.0.1:8000"
SignKey: ""
SignTTL: 300
SignDisabled: false
MaxContentLength: 4194304
RedisStoreAddress: "127.0.0.1:6379"
CookieKey: ""
TLSEnabled: false
TLSCertFile: ""
TLSKeyFile: ""
HealthCheckKey: ""
WaitForContainerReadyTimeout: 30
Datalab:
HookScriptMaxExecutionSeconds: 300
HookAfterStart: |-
/usr/bin/env
python3
./scripts/hook_after_start.py
${datalab_home}
${default_filename}
DataHome: "./data"
DataBackupHome: "./backup"
BasePort: 10000
NonceTimeout: 600
MaxInstanceCount: 20
RedisCacheURL: "redis://127.0.0.1:6379/1"
DockerEndpoint: "unix:///var/run/docker.sock"
InstanceAutoRemove: true
# browser open, but no interaction, after timeout expired, close instance
InstanceIdleTimeout: 1800
# browser closed, after timeout expired, close instance
InstanceKeepAliveTimeout: 300
`
type Config struct {
Service ServiceConfig
Datalab DatalabConfig
}
type ServiceConfig struct {
Listen string
SignKey string
SignTTL int
SignDisabled bool
MaxContentLength int64
RedisStoreAddress string
CookieKey string
TLSEnabled bool
TLSCertFile string
TLSKeyFile string
HealthCheckKey string
WaitForContainerReadyTimeout int // wait for container service ready timeout
}
type DatalabConfig struct {
HookScriptMaxExecutionSeconds int
HookAfterStart string
DataHome string
DataBackupHome string
BasePort int
NonceTimeout int
MaxInstanceCount int
RedisCacheURL string
DockerEndpoint string
InstanceAutoRemove bool
InstanceIdleTimeout int // browser open, but no interaction, after timeout expired, close instance
InstanceKeepAliveTimeout int // browser closed, after timeout expired, close instance
}
func setupConfig(configFilename string) (config *Config, err error) {
var (
configPath string
configName string
configExt string
)
if configFilename == "" {
configFilename = "./config/default.yml"
}
configName = filepath.Base(configFilename)
configExt = filepath.Ext(configName)
configName = configName[0 : len(configName)-len(configExt)]
configPath = filepath.Dir(configFilename)
log.Printf("Loading config from directory: %s, config name: %s", configPath, configName)
// check file exists
saveDefaultConfigIfNotExists(configPath, configName, configExt)
viper.SetConfigName(configName)
viper.AddConfigPath(configPath)
if err = viper.ReadInConfig(); err != nil {
err = fmt.Errorf("read config error: %v", err)
return
}
config = &Config{}
if err = viper.Unmarshal(config); err != nil {
err = fmt.Errorf("unmarshal config error: %v", err)
return
}
content, _ := yaml.Marshal(config)
log.Printf("config loaded:\n%v", string(content))
if err = checkConfig(config); err != nil {
return
}
return
}
func checkConfig(config *Config) (err error) {
// service config
if config.Service.CookieKey == "" {
err = fmt.Errorf("configuration: Service.CookieKey should not be empty")
return
}
if config.Service.SignKey == "" {
err = fmt.Errorf("configuration: Service.SignKey should not be empty")
return
}
if config.Service.SignTTL <= 0 {
config.Service.SignTTL = 300
}
if config.Service.TLSEnabled {
if config.Service.TLSCertFile == "" {
err = fmt.Errorf("configuration: Service.TLSCertFile should not be empty if TLSEnabled = true")
return
}
if config.Service.TLSKeyFile == "" {
err = fmt.Errorf("configuration: Service.TLSKeyFile should not be empty if TLSEnabled = true")
return
}
}
// datalab config
if config.Datalab.InstanceIdleTimeout <= 0 {
config.Datalab.InstanceIdleTimeout = 1800
}
if config.Datalab.InstanceKeepAliveTimeout <= 0 {
config.Datalab.InstanceKeepAliveTimeout = 300
}
if config.Datalab.HookScriptMaxExecutionSeconds <= 0 {
config.Datalab.HookScriptMaxExecutionSeconds = 300
}
return
}
func saveDefaultConfigIfNotExists(configPath string, configName string, configExt string) (err error) {
var configFullPath string
configFullPath = filepath.Join(configPath, configName+configExt)
_, err = os.Stat(configFullPath)
if err == nil {
return
}
if os.IsNotExist(err) {
if err = os.MkdirAll(configPath, 0755); err != nil {
log.Printf("create config directory failure: %s, err = %v", configPath, err)
return
}
var fout *os.File
if fout, err = os.Create(configFullPath); err != nil {
log.Printf("create config file failure: %s, err = %v", configFullPath, err)
return
}
defer fout.Close()
fout.WriteString(defaultConfigContent)
log.Printf("creating default config file: %s", configFullPath)
}
return
}