-
Notifications
You must be signed in to change notification settings - Fork 0
/
setting.go
186 lines (158 loc) · 4.47 KB
/
setting.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
package main
import (
"os"
"os/exec"
"path/filepath"
"strconv"
"syscall"
"github.com/lunny/log"
"github.com/unknwon/goconfig"
)
var (
permType = "simple"
driverType = "file"
rootPath string
qiniu = struct {
Bucket string
AccessKey string
SecretKey string
}{}
minio = struct {
Endpoint string
Bucket string
AccessKey string
SecretKey string
UseSSL bool
}{}
webCfg = struct {
Enabled bool
Listen string
TLS bool
CertFile string
KeyFile string
}{
Enabled: true,
Listen: ":8181",
TLS: false,
}
admin = "admin"
pass = "123456"
serv = struct {
Name string
Port int
TLS bool
KeyFile string
CertFile string
}{
Name: "Go Ftp Server",
Port: 2121,
TLS: false,
KeyFile: "key.pem",
CertFile: "cert.pem",
}
)
// exePath returns the executable path.
func exePath() (string, error) {
file, err := exec.LookPath(os.Args[0])
if err != nil {
return "", err
}
return filepath.Abs(file)
}
func initConfig() error {
var configFile = cfgPath
if len(configFile) == 0 {
dir, err := exePath()
if err != nil {
return err
}
defaultCfgPath := filepath.Join(filepath.Dir(dir), "config.ini")
_, err = os.Stat(defaultCfgPath)
if err != nil && !os.IsNotExist(err) {
return err
} else if err == nil {
configFile = defaultCfgPath
}
}
if len(configFile) == 0 {
return nil
}
cfg, err := goconfig.LoadConfigFile(configFile)
if err != nil {
return err
}
log.Info("Loaded config file:", configFile)
permType = cfg.MustValue("perm", "type", permType)
driverType = cfg.MustValue("driver", "type", driverType)
rootPath = cfg.MustValue("file", "rootpath", rootPath)
qiniu.AccessKey, _ = cfg.GetValue("qiniu", "accessKey")
qiniu.SecretKey, _ = cfg.GetValue("qiniu", "secretKey")
qiniu.Bucket, _ = cfg.GetValue("qiniu", "bucket")
minio.Endpoint, _ = cfg.GetValue("minio", "endpoint")
minio.AccessKey, _ = cfg.GetValue("minio", "accessKey")
minio.SecretKey, _ = cfg.GetValue("minio", "secretKey")
minio.Bucket, _ = cfg.GetValue("minio", "bucket")
minio.UseSSL = cfg.MustBool("minio", "use_ssl", false)
webCfg.Enabled = cfg.MustBool("web", "enable", webCfg.Enabled)
webCfg.Listen = cfg.MustValue("web", "listen", webCfg.Listen)
admin = cfg.MustValue("admin", "user", admin)
pass = cfg.MustValue("admin", "pass", pass)
webCfg.TLS = cfg.MustBool("web", "tls", webCfg.TLS)
webCfg.CertFile = cfg.MustValue("web", "certFile", webCfg.CertFile)
webCfg.KeyFile = cfg.MustValue("web", "keyFile", webCfg.KeyFile)
serv.Name = cfg.MustValue("server", "name", serv.Name)
serv.Port = cfg.MustInt("server", "port", serv.Port)
serv.TLS = cfg.MustBool("server", "tls", false)
serv.KeyFile = cfg.MustValue("server", "key_file", "")
serv.CertFile = cfg.MustValue("server", "cert_file", "")
return nil
}
func getEnv(name string, defaultValue string) string {
v, ok := syscall.Getenv(name)
if !ok {
return defaultValue
}
return v
}
func getEnvBool(name string, defaultValue bool) bool {
v, ok := syscall.Getenv(name)
if !ok {
return defaultValue
}
r, _ := strconv.ParseBool(v)
return r
}
func getEnvInt(name string, defaultValue int) int {
v, ok := syscall.Getenv(name)
if !ok {
return defaultValue
}
r, _ := strconv.Atoi(v)
return r
}
func readConfigFromEnvs() error {
permType = getEnv("PERM_TYPE", permType)
driverType = getEnv("DRIVER_TYPE", driverType)
rootPath = getEnv("FILE_ROOTPATH", rootPath)
qiniu.AccessKey = getEnv("QINIU_ACCESSKEY", qiniu.AccessKey)
qiniu.SecretKey = getEnv("QINIU_SECRETKEY", qiniu.SecretKey)
qiniu.Bucket = getEnv("QINIU_BUCKET", qiniu.Bucket)
minio.Endpoint = getEnv("MINIO_ENDPOINT", minio.Endpoint)
minio.AccessKey = getEnv("MINIO_ACCESSKEY", minio.AccessKey)
minio.SecretKey = getEnv("MINIO_SECRETKEY", minio.SecretKey)
minio.Bucket = getEnv("MINIO_BUCKET", minio.Bucket)
minio.UseSSL = getEnvBool("MINIO_USE_SSL", minio.UseSSL)
webCfg.Enabled = getEnvBool("WEB_ENABLE", webCfg.Enabled)
webCfg.Listen = getEnv("WEB_LISTEN", webCfg.Listen)
admin = getEnv("ADMIN_USER", admin)
pass = getEnv("ADMIN_PASS", pass)
webCfg.TLS = getEnvBool("WEB_TLS", webCfg.TLS)
webCfg.CertFile = getEnv("WEB_CERTFILE", webCfg.CertFile)
webCfg.KeyFile = getEnv("WEB_KEYFILE", webCfg.KeyFile)
serv.Name = getEnv("SERVER_NAME", serv.Name)
serv.Port = getEnvInt("SERVER_PORT", serv.Port)
serv.TLS = getEnvBool("SERVER_TLS", serv.TLS)
serv.KeyFile = getEnv("SERVER_KEY_FILE", serv.KeyFile)
serv.CertFile = getEnv("SERVER_CERT_FILE", serv.CertFile)
return nil
}