/
nginx.go
138 lines (113 loc) · 2.87 KB
/
nginx.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
// Copyright 2015 The Loadcat Authors. All rights reserved.
package nginx
import (
"errors"
"io/ioutil"
"os"
"path/filepath"
"sync"
"text/template"
"github.com/coreos/go-systemd/dbus"
"github.com/hjr265/loadcat/cfg"
"github.com/hjr265/loadcat/data"
"github.com/hjr265/loadcat/feline"
)
var TplNginxConf = template.Must(template.New("").Parse(`
upstream {{.Balancer.Id.Hex}} {
{{if eq .Balancer.Settings.Algorithm "least-connections"}}
least_conn;
{{else if eq .Balancer.Settings.Algorithm "source-ip"}}
ip_hash;
{{end}}
{{range $srv := .Balancer.Servers}}
server {{$srv.Settings.Address}} weight={{$srv.Settings.Weight}} {{if eq $srv.Settings.Availability "available"}}{{else if eq $srv.Settings.Availability "backup"}}backup{{else if eq $srv.Settings.Availability "unavailable"}}down{{end}};
{{end}}
}
server {
{{if eq .Balancer.Settings.Protocol "http"}}
listen {{.Balancer.Settings.Port}};
{{else if eq .Balancer.Settings.Protocol "https"}}
listen {{.Balancer.Settings.Port}} ssl;
{{end}}
server_name {{.Balancer.Settings.Hostname}};
{{if eq .Balancer.Settings.Protocol "https"}}
ssl on;
ssl_certificate {{.Dir}}/server.crt;
ssl_certificate_key {{.Dir}}/server.key;
{{end}}
location / {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_pass http://{{.Balancer.Id.Hex}};
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
}
}
`))
type Nginx struct {
sync.Mutex
Systemd *dbus.Conn
}
func (n Nginx) Generate(dir string, bal *data.Balancer) error {
n.Lock()
defer n.Unlock()
f, err := os.Create(filepath.Join(dir, "nginx.conf"))
if err != nil {
return err
}
err = TplNginxConf.Execute(f, struct {
Dir string
Balancer *data.Balancer
}{
Dir: dir,
Balancer: bal,
})
if err != nil {
return err
}
err = f.Close()
if err != nil {
return err
}
if bal.Settings.Protocol == "https" {
err = ioutil.WriteFile(filepath.Join(dir, "server.crt"), bal.Settings.SSLOptions.Certificate, 0666)
if err != nil {
return err
}
err = ioutil.WriteFile(filepath.Join(dir, "server.key"), bal.Settings.SSLOptions.PrivateKey, 0666)
if err != nil {
return err
}
}
return nil
}
func (n Nginx) Reload() error {
n.Lock()
defer n.Unlock()
switch cfg.Current.Nginx.Mode {
case "systemd":
if n.Systemd == nil {
c, err := dbus.NewSystemdConnection()
if err != nil {
return err
}
n.Systemd = c
}
ch := make(chan string)
_, err := n.Systemd.ReloadUnit(cfg.Current.Nginx.Systemd.Service, "replace", ch)
if err != nil {
return err
}
<-ch
return nil
default:
return errors.New("unknown Nginx mode")
}
panic("unreachable")
}
func init() {
feline.Register("nginx", Nginx{})
}