/
config.go
91 lines (73 loc) · 1.85 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
package slimgo
import (
"github.com/Unknwon/goconfig"
"os"
"path/filepath"
"strings"
)
var (
SlimApp *App
AppName string
WorkPath string
AppPath string
HttpAddress string
HttpPort int
HttpServerTimeOut int64
SessionOn bool
StaticPath map[string]string
)
func init() {
SlimApp = NewApp()
AppName = "SlimGo"
WorkPath, _ = os.Getwd()
WorkPath, _ = filepath.Abs(WorkPath)
AppPath, _ = filepath.Abs(filepath.Dir(os.Args[0]))
HttpAddress = ""
HttpPort = 6969
HttpServerTimeOut = 0
SessionOn = false
StaticPath = map[string]string{
"/public": "public",
}
CoverConfigByUser()
}
//覆盖配置
func CoverConfigByUser() {
confPath := "conf/conf.ini"
coverConfs, err := goconfig.LoadConfigFile(confPath)
if err != nil {
Waring("Can't find config file:conf/conf.ini ! Use default config.", err.Error())
return
}
if appname, err := coverConfs.GetValue(goconfig.DEFAULT_SECTION, "AppName"); err == nil {
AppName = appname
}
if httpAddress, err := coverConfs.GetValue(goconfig.DEFAULT_SECTION, "HttpAddress"); err == nil {
HttpAddress = httpAddress
}
if httpPort, err := coverConfs.Int(goconfig.DEFAULT_SECTION, "HttpPort"); err == nil {
HttpPort = httpPort
}
if httpServerTimeOut, err := coverConfs.Int64(goconfig.DEFAULT_SECTION, "HttpServerTimeOut"); err == nil {
HttpServerTimeOut = httpServerTimeOut
}
if sessionOn, err := coverConfs.Bool(goconfig.DEFAULT_SECTION, "SessionOn"); err == nil {
SessionOn = sessionOn
}
}
//set static path
func SetStaticPath(url, path string) {
if !strings.HasPrefix(url, "/") {
url = "/" + url
}
url = strings.TrimRight(url, "/")
StaticPath[url] = path
}
//delete static path
func DeleteStaticPath(url string) {
if !strings.HasPrefix(url, "/") {
url = "/" + url
}
url = strings.TrimRight(url, "/")
delete(StaticPath, url)
}