-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.go
More file actions
44 lines (35 loc) · 798 Bytes
/
config.go
File metadata and controls
44 lines (35 loc) · 798 Bytes
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
package goproxy
import (
"os"
"gopkg.in/yaml.v2"
"io/ioutil"
)
type Config struct {
ServerAddr string `yaml:"Server-address"`
ServerPort int `yaml:"Server-port"`
BindAddr string `yaml:"bind-address"`
BindPort int `yaml:"bind-port"`
}
func NewConfig() Config {
if _, err := os.Stat("./config.yml"); os.IsNotExist(err) {
o, _ := yaml.Marshal(Config{
ServerAddr: "0.0.0.0",
ServerPort: 19132,
BindAddr: "0.0.0.0",
BindPort: 19133,
})
f, err := os.OpenFile("./config.yml", os.O_APPEND | os.O_CREATE | os.O_WRONLY, 0644)
if err != nil {
panic(err)
}
_, err = f.WriteString(string(o))
if err != nil {
panic(err)
}
f.Close()
}
var config Config
yaml2, _ := ioutil.ReadFile("./config.yml")
yaml.Unmarshal(yaml2, &config)
return config
}