-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnginxreverse.go
147 lines (121 loc) · 3.06 KB
/
nginxreverse.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
139
140
141
142
143
144
145
146
147
package gounix
import (
"os"
"os/exec"
"strings"
)
type nginxReverseProxy struct {
name string
port string
domains []string
template TemplateEngine
}
func (driver *nginxReverseProxy) path() string {
return "/etc/nginx/sites-available/" + driver.name
}
func (driver *nginxReverseProxy) link() string {
return "/etc/nginx/sites-enabled/" + driver.name
}
func (driver *nginxReverseProxy) Name(name string) ServerBlock {
driver.name = name
return driver
}
func (driver *nginxReverseProxy) Port(port string) ServerBlock {
driver.port = port
return driver
}
func (driver *nginxReverseProxy) Domains(domains ...string) ServerBlock {
driver.domains = append(driver.domains, domains...)
return driver
}
func (driver *nginxReverseProxy) Template(engine TemplateEngine) ServerBlock {
driver.template = engine
return driver
}
func (driver *nginxReverseProxy) Disable() error {
// Delete link
err := os.Remove(driver.link())
if os.IsNotExist(err) {
return nil
} else if err != nil {
return err
}
// Restart nginx to apply the changes
return cmdError(exec.Command("sudo", "systemctl", "restart", "nginx").Run())
}
func (driver *nginxReverseProxy) Enable() error {
// Skip if exists
exists, err := fileExists(driver.link())
if err != nil {
return err
} else if exists {
return nil
}
// Create link
err = os.Symlink(driver.path(), driver.link())
if err != nil {
return err
}
// Restart nginx to apply the changes
return cmdError(exec.Command("sudo", "systemctl", "restart", "nginx").Run())
}
func (driver *nginxReverseProxy) Exists() (bool, error) {
return fileExists(driver.path())
}
func (driver *nginxReverseProxy) Enabled() (bool, error) {
available, err := fileExists(driver.path())
if err != nil {
return false, err
}
enabled, err := fileExists(driver.link())
if err != nil {
return false, err
}
return available && enabled, nil
}
func (driver *nginxReverseProxy) Install(override bool) (bool, error) {
// Check exists and override
exists, err := fileExists(driver.path())
if err != nil {
return false, err
} else if exists && !override {
return false, nil
}
// Compile template
content := []byte(
driver.template.
AddParameter("port", driver.port).
AddParameter("domains", strings.Join(driver.domains, " ")).
Compile(),
)
// Create server file
err = os.WriteFile(driver.path(), content, 0644)
if err != nil {
return false, err
}
// Create link and Skip if link exists
err = driver.Enable()
if err != nil {
return false, err
}
// Restart nginx to apply the changes
err = cmdError(exec.Command("sudo", "systemctl", "restart", "nginx").Run())
if err != nil {
return false, err
}
return true, nil
}
func (driver *nginxReverseProxy) Uninstall() error {
// Remove the enabled site link
err := os.Remove(driver.link())
if err != nil && !os.IsNotExist(err) {
return err
}
// Remove the available site file
err = os.Remove(driver.path())
if err != nil && !os.IsNotExist(err) {
return err
}
// Restart nginx to apply the changes
return cmdError(exec.Command("sudo", "systemctl", "restart", "nginx").Run())
}