-
Notifications
You must be signed in to change notification settings - Fork 0
/
container.go
110 lines (89 loc) · 2.35 KB
/
container.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
99
100
101
102
103
104
105
106
107
108
109
110
package main
import (
"fmt"
"os"
"os/exec"
"path"
"text/template"
)
var tmpl *template.Template
var nginxConfLocation string
var nginxReload = true
func init() {
var err error
tmpl, err = template.New("nginx.tmpl").Parse(`
upstream {{.Hostname}} {
server {{.IP}}:{{.Port}};
}
server {
listen 80;
server_name {{.Hostname}};
access_log /var/log/nginx/{{.Hostname}}.access.json.log;
error_log /var/log/nginx/{{.Hostname}}.access.log;
# include /etc/nginx/ssl-public.conf;
# require headers for http proxy
proxy_set_header Client-IP $remote_addr;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-Port $server_port;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $http_connection;
proxy_http_version 1.1;
proxy_redirect off;
proxy_next_upstream off;
proxy_read_timeout 100s;
# error_page 502 /502.devctl.html;
# location /502.devctl.html {
# return 502 'Nothing is running at port {{.Port}} on your host. For this to work, you need to check your the relevant project and start the corresponding server on OS X.';
# }
location / {
proxy_pass http://{{.Hostname}};
}
}
`)
handleError(err, true)
nginxConfLocation = os.Getenv("NGINXCONFLOCATION")
if nginxConfLocation == "" {
nginxConfLocation = "/etc/nginx/conf.d"
}
val := os.Getenv("NGINXRELOAD")
if val == "false" {
nginxReload = false
}
}
type container struct {
ID string
IP string
Hostname string
Port string
}
func (c *container) SetIP(ip string) {
c.IP = ip
}
func (c *container) WriteConfig() {
fileName := c.fileName()
fmt.Println("Writing conf to " + fileName)
f, err := os.Create(fileName)
handleError(err)
err = tmpl.Execute(f, c)
handleError(err)
if nginxReload {
err := exec.Command("nginx", "-s", "reload").Run()
handleError(err)
}
}
func (c *container) DeleteConfig() {
fileName := c.fileName()
fmt.Println("deleting conf " + fileName)
err := os.Remove(fileName)
handleError(err)
if nginxReload {
err := exec.Command("nginx", "-s", "reload").Run()
handleError(err)
}
}
func (c *container) fileName() string {
return path.Join(nginxConfLocation, c.ID) + ".conf"
}