-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathconfig.go
98 lines (81 loc) · 1.99 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
package main
import (
"io/ioutil"
"log"
"regexp"
"github.com/fsouza/go-dockerclient"
"gopkg.in/yaml.v1"
)
type Config struct {
Host Host `yaml:"host"`
Listen Listen `yaml:"listen"`
Docker DockerCfg `yaml:"docker"`
Storage StorageCfg `yaml:"storage"`
Parameter Paramters `yaml:"parameters"`
}
type Host struct {
WebApi string `yaml:"webapi"`
ReverseProxySuffix string `yaml:"reverse_proxy_suffix"`
}
type Listen struct {
ForeignAddress string `yaml:"foreign_address"`
HTTP []PortMap `yaml:"http"`
HTTPS []PortMap `yaml:"https"`
}
type PortMap struct {
ListenPort int `yaml:"listen"`
TargetPort int `yaml:"target"`
}
type DockerCfg struct {
Endpoint string `yaml:"endpoint"`
DefaultImage string `yaml:"default_image"`
HostConfig *docker.HostConfig `yaml:"host_config"` // TODO depending docker.HostConfig is so risky?
}
type StorageCfg struct {
DataDir string `yaml:"datadir"`
HtmlDir string `yaml:"htmldir"`
}
type Parameter struct {
Name string `yaml:"name"`
Env string `yaml:"env"`
Rule string `yaml:"rule"`
Required bool `yaml:"required"`
Regexp regexp.Regexp
}
type Paramters []*Parameter
func NewConfig(path string) *Config {
// default config
cfg := &Config{
Host: Host{
WebApi: "localhost",
ReverseProxySuffix: ".dev.example.net",
},
Listen: Listen{
ForeignAddress: "127.0.0.1",
HTTP: []PortMap{},
HTTPS: []PortMap{},
},
Docker: DockerCfg{
Endpoint: "unix:///var/run/docker.sock",
DefaultImage: "",
},
Storage: StorageCfg{
DataDir: "./data",
HtmlDir: "./html",
},
}
data, err := ioutil.ReadFile(path)
if err != nil {
log.Fatalf("cannot read %v: %v", path, err)
}
if err := yaml.Unmarshal(data, cfg); err != nil {
log.Fatalf("powawa: %v", err)
}
for _, v := range cfg.Parameter {
if v.Rule != "" {
paramRegex := regexp.MustCompile(v.Rule)
v.Regexp = *paramRegex
}
}
return cfg
}