-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
116 lines (90 loc) · 2.29 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
package main
import (
"fmt"
"gopkg.in/ini.v1"
"net/http"
"os"
"path/filepath"
"strings"
"time"
)
func loadConfig(file string) {
cfg := configInit(file)
fmt.Println(`load config =`, configFileFinal)
return
section := cfg.Section(``)
// listen
httpListen = section.Key(`listen`).MustString(httpListen)
fmt.Println("\t", `listen`, httpListen)
// noopInterval
noopInterval = section.Key(`noop-interval`).MustInt64(noopInterval)
fmt.Println("\t", `noopInterval`, noopInterval)
// writeWait
iWriteWait := section.Key(`write-wait`).MustInt64(iWriteWait)
if iWriteWait < 1 {
iWriteWait = 1
}
writeWait = time.Duration(iWriteWait) * time.Second
fmt.Println("\t", `writeWait`, iWriteWait)
// whitelistFileName
whitelistFileName = section.Key(`whitelist-file`).MustString(whitelistFileName)
fmt.Println("\t", `whitelist-file`, whitelistFileName)
fmt.Println()
}
func configPage(w http.ResponseWriter, r *http.Request) {
writeHttp(w, fmt.Sprintf("version: %s\n\n", version))
writeHttp(w, "Settings:\n\n")
showVar(w, `listen`, httpListen)
showVar(w, `noop-interval`, noopInterval)
showVar(w, `write-wait`, iWriteWait)
showVar(w, `whitelist-file`, whitelistFileName)
writeHttp(w, "\n\nWhitelist File:\n\n\t"+whitelistFileFinal+"\n")
writeHttp(w, "\nAllowed files:\n\n")
for k, _ := range fileAllow {
writeHttp(w, "\t"+k+"\n")
}
writeHttp(w, "\nAllowed dirs:\n\n")
for k, _ := range dirAllow {
writeHttp(w, "\t"+k+"\n")
}
}
func configInit(file) (cfg *ini.File) {
var err error
separator := string(os.PathSeparator)
if strings.HasPrefix(file, separator) {
cfg, err = ini.Load(file)
if err == nil {
configFileFinal = file
}
return cfg
}
dirList := []string{}
fileLoad := ``
dir, err := os.Getwd()
if err == nil {
dirList = append(dirList, dir)
}
if strings.Contains(file, separator) {
dirList = append(dirList, os.Args[0])
}
for _, dir := range dirList {
dir, err = filepath.Abs(filepath.Dir(dir))
if err == nil {
if dir != `/` {
dir += `/`
}
fileLoad = dir + configFileName
cfgbak, err := ini.Load(fileLoad)
if err == nil {
cfg = cfgbak
configFileFinal = fileLoad
bLoad = true
break
}
}
}
return cfg
}
func showVar(w http.ResponseWriter, k string, v interface{}) {
writeHttp(w, fmt.Sprintf("\t%-14s = %v\n", k, v))
}