-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver_config.go
62 lines (57 loc) · 2.06 KB
/
server_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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"strings"
)
// ServerConfig global server config
type ServerConfig struct {
ServerHTTPPort int `json:"serverHTTPPort"`
RunHTTPS bool `json:"runHttps"`
ServerHTTPSecurePort int `json:"serverHTTPSecurePort"`
SSLCertPath string `json:"sslCertPath"`
SSLKeyPath string `json:"sslKeyPath"`
LoggingReleaseMode bool `json:"loggingReleaseMode"`
LoggingLevel string `json:"loggingLevel"`
LoggingDestination string `json:"loggingDestination"`
DBHostAddress string `json:"DBHostAddress"`
DBName string `json:"DBName"`
DBUsername string `json:"DBUsername"`
DBPassword string `json:"DBPassword"`
AzureStorageAccount string `json:"azureStorageAccount"`
AzureStorageAccessKey string `json:"azureStorageAccessKey"`
AzureStorageContainer string `json:"azureStorageContainer"`
RelativeWeChatLoginURL string `json:"relativeWeChatLoginURL"`
StudentBindingCodeLifeTime int `json:"studentBindingCodeLifeTime"` // hour
}
var serverConfig *ServerConfig
func readServerConfig(ConfigFile string) (*ServerConfig, error) {
configHandle, err := os.Open(ConfigFile)
if err != nil {
return nil, fmt.Errorf("Server Config File Open Error - %v", err.Error())
}
defer configHandle.Close()
configBytes, err := ioutil.ReadAll(configHandle)
if err != nil {
return nil, fmt.Errorf("Server Config Read Error - %v", err.Error())
}
var serverconfig ServerConfig
if err = json.Unmarshal(configBytes, &serverconfig); err != nil {
return nil, fmt.Errorf("Server Config Parse Error - %v", err.Error())
}
return &serverconfig, nil
}
func initDefaultServerConfig(sc *ServerConfig) {
sc.LoggingLevel = strings.ToLower(sc.LoggingLevel)
if sc.LoggingLevel == "" {
sc.LoggingLevel = "debug"
}
if sc.LoggingDestination == "" {
sc.LoggingDestination = "stdout+file"
}
if sc.StudentBindingCodeLifeTime == 0 {
sc.StudentBindingCodeLifeTime = 72
}
}