/
config.go
85 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
package server
import (
"errors"
"fmt"
"gopkg.in/yaml.v3"
"os"
)
type Server struct {
HTTPAddr string `yaml:"http_addr"`
HTTPPort string `yaml:"http_port"`
KeyFile string `yaml:"key_file"`
CertFile string `yaml:"cert_file"`
IdleTimeout int `yaml:"idle_timeout"`
ReadTimeout int `yaml:"read_timeout"`
ThreadCount int `yaml:"thread_count"`
KeepAlive bool `yaml:"keep_alive"`
SSL bool `yaml:"user_ssl"`
Debug bool `yaml:"debug"`
}
type Tunl struct {
Addr string `yaml:"addr"`
Port string `yaml:"port"`
Domain string `yaml:"domain"`
SchemeHttps bool `yaml:"scheme_https"`
MaxClients int `yaml:"max_clients"`
MaxTimeout int `yaml:"max_timeout"`
MaxPostSize int `yaml:"max_post_size"`
ClientExpireAt int `yaml:"client_expire_at"`
UriPrefixSize int `yaml:"uri_prefix_size"`
ServerPrivate bool `yaml:"server_private"`
ServerPassword string `yaml:"server_password"`
BrowserWarning bool `yaml:"browser_warning"`
}
type Log struct {
Enabled bool `yaml:"enabled"`
LogDir string `yaml:"log_dir"`
LogDaily bool `yaml:"log_daily"`
}
type Config struct {
Server *Server `yaml:"server"`
Tunl *Tunl `yaml:"tunl"`
Log *Log `yaml:"log"`
}
var configFiles = []string{
"prod.yaml",
"dev.yaml",
"default.yaml",
}
func getConfigYamlFile() (string, error) {
for _, fn := range configFiles {
fname := fmt.Sprintf("./conf/%s", fn)
if _, err := os.Stat(fname); err == nil {
return fname, nil
}
}
return "", errors.New("not found config files")
}
func LoadConfig() (*Config, error) {
fn, err := getConfigYamlFile()
if err != nil {
return nil, err
}
bytes, err := os.ReadFile(fn)
if err != nil {
return nil, err
}
conf := &Config{}
err = yaml.Unmarshal(bytes, conf)
if err != nil {
return nil, err
}
return conf, nil
}