@@ -19,28 +19,30 @@ import (
19
19
"math"
20
20
"os"
21
21
"sort"
22
+ "strconv"
22
23
"strings"
23
24
"sync/atomic"
24
25
"time"
25
26
)
26
27
27
28
type (
28
- // GlobalConfig is global configuration
29
+ // GlobalConfig is global config
29
30
GlobalConfig struct {
30
31
Cache CacheConfig `ini:"cache" comment:"Cache section"`
31
32
Gzip GzipConfig `ini:"gzip" comment:"Whether enabled or not"`
32
33
Log LogConfig `ini:"log" comment:"Whether enabled or not console logger"`
33
34
warnMsg string `int:"-"`
34
35
}
35
- // Config is the configuration information for each web instance
36
+ // Config is the config information for each web instance
36
37
Config struct {
37
38
// RunMode string `ini:"run_mode" comment:"run mode: dev|prod"`
38
39
NetTypes []string `ini:"net_types" delim:"|" comment:"List of network type: http|https|unix_http|unix_https|letsencrypt|unix_letsencrypt"`
39
40
Addrs []string `ini:"addrs" delim:"|" comment:"List of multiple listening addresses"`
40
41
TLSCertFile string `ini:"tls_certfile" comment:"TLS certificate file path"`
41
42
TLSKeyFile string `ini:"tls_keyfile" comment:"TLS key file path"`
42
43
LetsencryptDir string `ini:"letsencrypt_dir" comment:"Let's Encrypt TLS certificate cache directory"`
43
- UNIXFileMode os.FileMode `ini:"unix_filemode" comment:"File permissions for UNIX listener (438 equivalent to 0666)"`
44
+ UNIXFileMode string `ini:"unix_filemode" comment:"File permissions for UNIX listener, requires octal number"`
45
+ unixFileMode os.FileMode `ini:"-"`
44
46
// Maximum duration for reading the full request (including body).
45
47
//
46
48
// This also limits the maximum duration for idle keep-alive
@@ -54,15 +56,15 @@ type (
54
56
WriteTimeout time.Duration `ini:"write_timeout" comment:"Maximum duration for writing the full response (including body); ns|µs|ms|s|m|h"`
55
57
MultipartMaxMemoryMB int64 `ini:"multipart_maxmemory_mb" comment:"Maximum size of memory that can be used when receiving uploaded files"`
56
58
multipartMaxMemory int64 `ini:"-"`
57
- Router RouterConfig `ini:"router" comment:"Routing configuration section"`
59
+ Router RouterConfig `ini:"router" comment:"Routing config section"`
58
60
XSRF XSRFConfig `ini:"xsrf" comment:"XSRF security section"`
59
61
Session SessionConfig `ini:"session" comment:"Session section"`
60
62
SlowResponseThreshold time.Duration `ini:"slow_response_threshold" comment:"When response time > slow_response_threshold, log level = 'WARNING'; 0 means not limited; ns|µs|ms|s|m|h"`
61
63
slowResponseThreshold time.Duration `ini:"-"`
62
64
PrintBody bool `ini:"print_body" comment:"Form requests are printed in JSON format, but other types are printed as-is"`
63
65
APIdoc APIdocConfig `ini:"apidoc" comment:"API documentation section"`
64
66
}
65
- // RouterConfig is the configuration about router
67
+ // RouterConfig is the config about router
66
68
RouterConfig struct {
67
69
// Enables automatic redirection if the current route can't be matched but a
68
70
// handler for the path with (without) the trailing slash exists.
93
95
DefaultUpload bool `ini:"default_upload" comment:"Automatically register the default router: /upload/*filepath"`
94
96
DefaultStatic bool `ini:"default_static" comment:"Automatically register the default router: /static/*filepath"`
95
97
}
96
- // GzipConfig is the configuration about gzip
98
+ // GzipConfig is the config about gzip
97
99
GzipConfig struct {
98
100
// if EnableGzip, compress response content.
99
101
Enable bool `ini:"enable" comment:"Whether enabled or not"`
@@ -107,7 +109,7 @@ type (
107
109
Methods []string `ini:"methods" delim:"|" comment:"List of HTTP methods to compress. If not set, only GET requests are compressed."`
108
110
// StaticExtensionsToGzip []string
109
111
}
110
- // CacheConfig is the configuration about cache
112
+ // CacheConfig is the config about cache
111
113
CacheConfig struct {
112
114
// Whether to enable caching static files
113
115
Enable bool `ini:"enable" comment:"Whether enabled or not"`
@@ -121,18 +123,18 @@ type (
121
123
// ExpireSecond <= 0 (second) means no expire, but it can be evicted when cache is full.
122
124
ExpireSecond int `ini:"expire_second" comment:"Maximum duration for caching"`
123
125
}
124
- // XSRFConfig is the configuration about XSRF filter
126
+ // XSRFConfig is the config about XSRF filter
125
127
XSRFConfig struct {
126
128
Enable bool `ini:"enable" comment:"Whether enabled or not"`
127
129
Key string `ini:"key" comment:"Encryption key"`
128
130
ExpireSecond int `ini:"expire_second" comment:"Expire of XSRF token"`
129
131
}
130
- // SessionConfig is the configuration about session
132
+ // SessionConfig is the config about session
131
133
SessionConfig struct {
132
134
Enable bool `ini:"enable" comment:"Whether enabled or not"`
133
135
Provider string `ini:"provider" comment:"Data storage"`
134
136
Name string `ini:"name" comment:"The client stores the name of the cookie"`
135
- ProviderConfig string `ini:"provider_config" comment:"According to the different engine settings different configuration information"`
137
+ ProviderConfig string `ini:"provider_config" comment:"According to the different engine settings different config information"`
136
138
CookieLifeSecond int `ini:"cookie_life_second" comment:"The default value is 0, which is the lifetime of the browser"`
137
139
GcLifeSecond int64 `ini:"gc_life_second" comment:"The interval between triggering the GC"`
138
140
MaxLifeSecond int64 `ini:"max_life_second" comment:"The session max lefetime"`
@@ -142,15 +144,15 @@ type (
142
144
NameInHttpHeader string `ini:"name_in_header" comment:"The name of the header when the session ID is written to the header"`
143
145
EnableSidInUrlQuery bool `ini:"enable_sid_in_urlquery" comment:"Whether to write the session ID to the URL Query params"`
144
146
}
145
- // LogConfig is the configuration about log
147
+ // LogConfig is the config about log
146
148
LogConfig struct {
147
149
ConsoleEnable bool `ini:"console_enable" comment:"Whether enabled or not console logger"`
148
150
ConsoleLevel string `ini:"console_level" comment:"Console logger level: critical|error|warning|notice|info|debug"`
149
151
FileEnable bool `ini:"file_enable" comment:"Whether enabled or not file logger"`
150
152
FileLevel string `ini:"file_level" comment:"File logger level: critical|error|warning|notice|info|debug"`
151
153
AsyncLen int `ini:"async_len" comment:"The length of asynchronous buffer, 0 means synchronization"`
152
154
}
153
- // APIdocConfig is the configuration about API doc
155
+ // APIdocConfig is the config about API doc
154
156
APIdocConfig struct {
155
157
Enable bool `ini:"enable" comment:"Whether enabled or not"`
156
158
Path string `ini:"path" comment:"The URL path"`
@@ -174,7 +176,7 @@ const (
174
176
defaultMultipartMaxMemoryMB = 32
175
177
defaultPort = 8080
176
178
177
- // The path for the configuration files
179
+ // The path for the config files
178
180
CONFIG_DIR = "./config/"
179
181
// global config file name
180
182
GLOBAL_CONFIG_FILE = "__global___.ini"
@@ -209,12 +211,12 @@ var globalConfig = func() GlobalConfig {
209
211
210
212
err := SyncINI (
211
213
& background ,
212
- func (existed bool , mustSave func () error ) error {
214
+ func (onecUpdateFunc func () error ) error {
213
215
if ! (background .Log .ConsoleEnable || background .Log .FileEnable ) {
214
216
background .Log .ConsoleEnable = true
215
217
background .warnMsg = "config: log::enable_console and log::enable_file can not be disabled at the same time, so automatically open console log."
216
218
}
217
- return mustSave ()
219
+ return onecUpdateFunc ()
218
220
},
219
221
filename ,
220
222
)
@@ -235,7 +237,7 @@ func newConfig(filename string) Config {
235
237
// RunMode: RUNMODE_DEV,
236
238
NetTypes : []string {NETTYPE_HTTP },
237
239
Addrs : []string {addr },
238
- UNIXFileMode : 0666 ,
240
+ UNIXFileMode : " 0666" ,
239
241
MultipartMaxMemoryMB : defaultMultipartMaxMemoryMB ,
240
242
Router : RouterConfig {
241
243
RedirectTrailingSlash : true ,
@@ -278,33 +280,39 @@ func newConfig(filename string) Config {
278
280
279
281
err := SyncINI (
280
282
& background ,
281
- func (existed bool , mustSave func () error ) error {
283
+ func (onecUpdateFunc func () error ) error {
282
284
// switch background.RunMode {
283
285
// case RUNMODE_DEV, RUNMODE_PROD:
284
286
// default:
285
287
// panic("Please set a valid config item run_mode, refer to the following:\ndev|prod")
286
288
// }
287
289
if len (background .NetTypes ) != len (background .Addrs ) {
288
- panic ("The number of configuration items `net_types` and `addrs` must be equal" )
290
+ panic ("The number of config items `net_types` and `addrs` must be equal" )
289
291
}
290
292
if len (background .NetTypes ) == 0 {
291
- panic ("The number of configuration items `net_types` and `addrs` must be greater than zero" )
293
+ panic ("The number of config items `net_types` and `addrs` must be greater than zero" )
292
294
}
293
295
for _ , t := range background .NetTypes {
294
296
switch t {
295
297
case NETTYPE_HTTP , NETTYPE_UNIX_HTTP , NETTYPE_HTTPS , NETTYPE_UNIX_HTTPS , NETTYPE_LETSENCRYPT , NETTYPE_UNIX_LETSENCRYPT :
296
298
default :
297
- panic ("Please set a valid config item `net_types`, refer to the following:" + __netTypes__ + " \n " )
299
+ panic ("Please set a valid config item `net_types`, refer to the following:" + __netTypes__ )
298
300
}
299
301
}
302
+ fileMode , err := strconv .ParseUint (background .UNIXFileMode , 8 , 32 )
303
+ if err != nil {
304
+ panic ("The config item `unix_filemode` is not a valid octal number:" + background .UNIXFileMode )
305
+ }
306
+ background .unixFileMode = os .FileMode (fileMode )
307
+ background .UNIXFileMode = fmt .Sprintf ("%#o" , fileMode )
300
308
background .multipartMaxMemory = background .MultipartMaxMemoryMB * MB
301
309
if background .SlowResponseThreshold <= 0 {
302
310
background .slowResponseThreshold = time .Duration (math .MaxInt64 )
303
311
} else {
304
312
background .slowResponseThreshold = background .SlowResponseThreshold
305
313
}
306
314
background .APIdoc .Comb ()
307
- return mustSave ()
315
+ return onecUpdateFunc ()
308
316
},
309
317
filename ,
310
318
)
0 commit comments