-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
69 lines (53 loc) · 1.32 KB
/
main.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
package main
import (
"github.com/davidbanham/required_env"
"log"
"os"
"regexp"
"strings"
"text/template"
)
func main() {
type Server struct {
Name string
Port string
}
type Servers []Server
type TemplateConfig struct {
Servers Servers
Healthport string
Listenport string
Domain string
}
required_env.Ensure(map[string]string{"HEALTH_PORT": "", "LISTEN_PORT": "", "DOMAIN": ""})
re := regexp.MustCompile("DISTRIBUTOR_*")
servers := Servers{}
for _, env := range os.Environ() {
loc := re.FindStringIndex(env)
if loc == nil {
continue
}
strippedEnv := env[loc[1]:]
vals := strings.Split(strippedEnv, "=")
envArgs := strings.Split(vals[1], ",")
name, port := envArgs[0], envArgs[1]
suffix := vals[0]
if strings.ToLower(name) != strings.ToLower(suffix) {
log.Panicf("Server %s is malformed. %s should be %s", name, suffix, strings.ToUpper(name))
}
server := Server{name, port}
servers = append(servers, server)
}
healthport := os.Getenv("HEALTH_PORT")
listenport := os.Getenv("LISTEN_PORT")
domain := os.Getenv("DOMAIN")
config, err := template.New("config.template").ParseFiles("config.template")
if err != nil {
panic(err)
}
conf := TemplateConfig{servers, healthport, listenport, domain}
err = config.Execute(os.Stdout, conf)
if err != nil {
panic(err)
}
}