-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
74 lines (66 loc) · 1.65 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
package main
import (
"errors"
"log"
"os"
"strconv"
"strings"
)
const CONFIG_DEFAULT_HZ = 10 // Time interrupt calls/sec.
const CONFIG_MIN_HZ = 1
const CONFIG_BINDADDR_MAX = 16
const CONFIG_DEFAULT_SERVER_PORT = 6379
const CONFIG_DEFAULT_MAX_CLIENTS = 10000
const CONFIG_DEFAULT_TCP_BACKLOG = 511
const CONFIG_DEFAULT_DBNUM = 16
const PROTO_IOBUF_LEN = (1024 * 16) /* Generic I/O buffer size */
func initServerConfig() {
server.maxclients = CONFIG_DEFAULT_MAX_CLIENTS
server.port = CONFIG_DEFAULT_SERVER_PORT
server.tcp_backlog = CONFIG_DEFAULT_TCP_BACKLOG
server.dbnum = CONFIG_DEFAULT_DBNUM
}
func loadServerConfig(confFile string, options string) {
var config string
if len(confFile) > 0 {
content, err := os.ReadFile(confFile)
if err != nil {
log.Fatalln("Fatal error, can't read config file ", confFile)
}
config += string(content)
}
if len(options) > 0 {
config += "\n"
config += options
}
loadServerConfigFromString(config)
}
func loadServerConfigFromString(config string) {
lines := strings.Split(config, "\n")
var err error
for _, line := range lines {
line = strings.Trim(line, " \t\r\n")
if strings.HasPrefix(line, "#") || len(line) == 0 {
continue
}
argv := strings.Split(line, " ")
argc := len(argv)
if argc == 0 {
continue
}
argv[0] = strings.ToLower(argv[0])
if argv[0] == "port" && argc == 2 {
if port, _err := strconv.Atoi(argv[1]); port < 0 || port > 65535 || _err != nil {
err = errors.New("invalid port")
goto loaderr
} else {
server.port = port
}
}
}
return
loaderr:
os.Stderr.WriteString("\n*** FATAL CONFIG FILE ERROR ***\n")
os.Stderr.WriteString(err.Error())
os.Exit(1)
}