-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
45 lines (40 loc) · 882 Bytes
/
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
package main
import (
"fmt"
"io/ioutil"
"os"
"gopkg.in/yaml.v2"
)
// Configer 配置文件
type Configer struct {
Daemon bool `yaml:"daemon"`
Debug bool `yaml:"debug"`
Server struct {
TCPAddr string `yaml:"tcp_addr"`
UDPAddr string `yaml:"udp_addr"`
} `yaml:"server"`
DNS struct {
IN []string `yaml:"in"`
OUT []string `yaml:"out"`
} `yaml:"dns"`
Rule []string `yaml:"rule"`
}
// NewConfig 读取配置文件,实例化配置并返回
func NewConfig(filename string) (*Configer, error) {
c := new(Configer)
data, err := ReadFile(filename)
if err != nil {
return nil, fmt.Errorf("config.parse error: %s", err.Error())
}
err = yaml.Unmarshal(data, c)
return c, nil
}
// ReadFile read file content
func ReadFile(file string) ([]byte, error) {
f, err := os.Open(file)
if err != nil {
return nil, err
}
defer f.Close()
return ioutil.ReadAll(f)
}