-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathconfig.go
169 lines (141 loc) · 3.03 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
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
package master
import (
"bufio"
"fmt"
"io"
"log"
"os"
"runtime"
"strconv"
"strings"
)
const Version string = "1.1.2"
type Config struct {
Entries map[string]string
}
// from configure file of the app
var (
AppConf *Config
AppService string
AppLogPath string
AppOwner string
AppArgs string
AppRootDir string
AppUseLimit = 0
AppIdleLimit = 0
AppReusePort = false
AppQuickAbort = false
AppWaitLimit = 10
AppAccessAllow = "all"
Appthreads = 0
TlsCertFile string
TlsKeyFile string
)
func loadConf(confPath string) {
AppConf = new(Config)
AppConf.InitConfig(confPath)
AppLogPath = AppConf.GetString("master_log")
if len(AppLogPath) > 0 {
f, err := os.OpenFile(AppLogPath, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0643)
if err != nil {
fmt.Printf("open %s error %s\r\n", AppLogPath, err.Error())
} else {
log.SetOutput(f)
//log.SetOutput(io.MultiWriter(os.Stderr, f))
}
}
AppService = AppConf.GetString("master_service")
AppOwner = AppConf.GetString("master_owner")
AppArgs = AppConf.GetString("master_args")
AppReusePort = AppConf.GetBool("master_reuseport")
AppRootDir = AppConf.GetString("app_queue_dir")
AppUseLimit = AppConf.GetInt("app_use_limit")
AppIdleLimit = AppConf.GetInt("app_idle_limit")
AppQuickAbort = AppConf.GetBool("app_quick_abort")
AppWaitLimit = AppConf.GetInt("app_wait_limit")
AppAccessAllow = AppConf.GetString("app_access_allow")
Appthreads = AppConf.GetInt("app_threads")
if Appthreads > -1 {
runtime.GOMAXPROCS(Appthreads)
}
TlsCertFile = AppConf.GetString("tls_cert_file")
TlsKeyFile = AppConf.GetString("tls_key_file")
log.Printf("AppArgs: %s, AppAccessAllow: %s\r\n", AppArgs, AppAccessAllow)
}
func (c *Config) InitConfig(path string) {
c.Entries = make(map[string]string)
if len(path) == 0 {
return
}
f, err := os.Open(path)
if err != nil {
panic(err)
}
defer f.Close()
r := bufio.NewReader(f)
for {
line, _, err := r.ReadLine()
if err != nil {
if err == io.EOF {
break
}
panic(err)
}
s := strings.TrimSpace(string(line))
eq := strings.Index(s, "=")
if eq < 0 {
continue
}
name := strings.TrimSpace(s[:eq])
if len(name) == 0 {
continue
}
value := strings.TrimSpace(s[eq+1:])
pos := strings.Index(value, "\t#")
if pos > -1 {
value = value[0:pos]
}
pos = strings.Index(value, " #")
if pos > -1 {
value = value[0:pos]
}
if len(value) == 0 {
continue
}
c.Entries[name] = strings.TrimSpace(value)
}
}
func (c Config) GetString(name string) string {
val, found := c.Entries[name]
if !found {
return ""
}
return val
}
func (c Config) GetInt(name string) int {
val, found := c.Entries[name]
if !found {
return 0
}
n, err := strconv.Atoi(val)
if err != nil {
return 0
} else {
return n
}
}
func (c Config) GetBool(name string) bool {
val, found := c.Entries[name]
if !found {
return false
}
if val == "yes" || val == "true" || val == "y" {
return true
}
n, err := strconv.Atoi(val)
if err != nil {
return false
} else {
return n != 0
}
}