forked from codeskyblue/gosuv
-
Notifications
You must be signed in to change notification settings - Fork 10
/
config.go
124 lines (104 loc) · 2.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
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
package main
import (
"io/ioutil"
"os"
"path/filepath"
"github.com/go-yaml/yaml"
)
type Configuration struct {
Include string `yaml:"include"`
Server GosuvServer `yaml:"server"`
Client GosuvClient `yaml:"client"`
}
type GosuvServer struct {
HttpServer HttpServer `yaml:"httpserver"`
UnixServer UnixServer `yaml:"unixserver"`
Auth Auth `yaml:"auth"`
PidFile string `yaml:"pidfile"`
Log GosuvLog `yaml:"log"`
MinFds int `yaml:"minfds"`
MinProcs int `yaml:"minprocs"`
}
type GosuvClient struct {
ServerURL string `yaml:"server_url"`
Username string `yaml:"username"`
Password string `yaml:"password"`
}
type HttpServer struct {
Enabled bool `yaml:"enabled"`
Addr string `yaml:"addr"`
}
type Auth struct {
Enabled bool `yaml:"enabled"`
User string `yaml:"username"`
Password string `yaml:"password"`
IPFile string `yaml:"ipfile"`
}
type UnixServer struct {
Enabled bool `yaml:"enabled"`
SockFile string `yaml:"sockfile"`
}
type GosuvLog struct {
LogPath string `yaml:"logpath"`
Level string `yaml:"level"`
FileMax int `yaml:"filemax"`
Backups int `yaml:"backups"`
}
func readConf(filename string) (c Configuration, err error) {
// initial default value
// 初始化配置文件 如果config.yml不存在的时候.
c.Server.HttpServer.Enabled=false
c.Server.HttpServer.Addr="127.0.0.1:11333"
c.Server.UnixServer.Enabled=true
c.Server.UnixServer.SockFile=".gosuv.sock"
c.Server.Log.LogPath="logs"
c.Client.ServerURL="unix://.gosuv.sock"
c.Server.PidFile=".gosuv.pid"
c.Server.Log.Backups=7
c.Server.Log.Level="info"
c.Server.Log.FileMax=10000
data, err := ioutil.ReadFile(filename)
if err != nil {
data = []byte("")
}
err = yaml.Unmarshal(data, &c)
if err != nil {
return
}
cfgDir := filepath.Dir(filename)
if !IsDir(CfgDir) {
os.MkdirAll(cfgDir, 0755)
}
data, _ = yaml.Marshal(c)
err = ioutil.WriteFile(filename, data, 0640)
return
}
/*
include: ./conf/programs.yml
# include主要是Programs的配置 看是一个文件还是多个文件.
server:
httpserver:
enabled: true
addr: :11313
httpauth:
enabled: true
username: abc
password: abc
ipfile: ./allow.list
unix_http_server:
enabled: true
sockfile: ./.gosuv.sock
pidfile: ./.gosuv.pid
log:
logpath: ./logs # gosuv服务的日志会输出到 gosuv.log programs的日志会按program的名字存储在该目录中.
level: info # 只对gosuv服务的日志有效. program的日志是stdout stderr的日志内容.
filemax: 50MB
backups: 10
minfds: 1024
minprocs: 1024
client:
server_url: http://:11313
# 添加http://或者sock://做为不同的client方式.
username:
password:
*/