Skip to content

Commit 01611ef

Browse files
committed
feat: added custom nginx reload/restart cmd #140
1 parent cc6a325 commit 01611ef

File tree

7 files changed

+75
-144
lines changed

7 files changed

+75
-144
lines changed

app.example.ini

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,14 @@ StartCmd = login
88
Database = database
99
CADir =
1010
Demo =
11-
GithubProxy =
12-
NginxConfigDir =
1311

14-
[nginx_log]
12+
[nginx]
1513
AccessLogPath = /var/log/nginx/access.log
16-
ErrorLogPath = /var/log/nginx/error.log
14+
ErrorLogPath = /var/log/nginx/error.log
15+
ConfigDir =
16+
PIDPath =
17+
ReloadCmd =
18+
RestartCmd =
1719

1820
[openai]
1921
Model =

go.mod

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,6 @@ require (
175175
golang.org/x/mod v0.10.0 // indirect
176176
golang.org/x/net v0.10.0 // indirect
177177
golang.org/x/oauth2 v0.8.0 // indirect
178-
golang.org/x/sync v0.2.0 // indirect
179178
golang.org/x/sys v0.8.0 // indirect
180179
golang.org/x/text v0.9.0 // indirect
181180
golang.org/x/time v0.3.0 // indirect

go.sum

Lines changed: 5 additions & 107 deletions
Large diffs are not rendered by default.

server/api/nginx_log.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -141,20 +141,20 @@ func getLogPath(control *controlStruct) (logPath string, err error) {
141141
logPath = directive.Params
142142

143143
case "error":
144-
if settings.NginxLogSettings.ErrorLogPath == "" {
144+
if settings.NginxSettings.ErrorLogPath == "" {
145145
err = errors.New("settings.NginxLogSettings.ErrorLogPath is empty," +
146146
" see https://github.com/0xJacky/nginx-ui/wiki/Nginx-Log-Configuration for more information")
147147
return
148148
}
149-
logPath = settings.NginxLogSettings.ErrorLogPath
149+
logPath = settings.NginxSettings.ErrorLogPath
150150

151151
default:
152-
if settings.NginxLogSettings.AccessLogPath == "" {
152+
if settings.NginxSettings.AccessLogPath == "" {
153153
err = errors.New("settings.NginxLogSettings.AccessLogPath is empty," +
154154
" see https://github.com/0xJacky/nginx-ui/wiki/Nginx-Log-Configuration for more information")
155155
return
156156
}
157-
logPath = settings.NginxLogSettings.AccessLogPath
157+
logPath = settings.NginxSettings.AccessLogPath
158158
}
159159

160160
return

server/api/settings.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,26 @@ import (
88

99
func GetSettings(c *gin.Context) {
1010
c.JSON(http.StatusOK, gin.H{
11-
"server": settings.ServerSettings,
12-
"nginx_log": settings.NginxLogSettings,
13-
"openai": settings.OpenAISettings,
14-
"git": settings.GitSettings,
11+
"server": settings.ServerSettings,
12+
"nginx": settings.NginxSettings,
13+
"openai": settings.OpenAISettings,
14+
"git": settings.GitSettings,
1515
})
1616
}
1717

1818
func SaveSettings(c *gin.Context) {
1919
var json struct {
20-
Server settings.Server `json:"server"`
21-
NginxLog settings.NginxLog `json:"nginx_log"`
22-
Openai settings.OpenAI `json:"openai"`
20+
Server settings.Server `json:"server"`
21+
Nginx settings.Nginx `json:"nginx"`
22+
Openai settings.OpenAI `json:"openai"`
2323
}
2424

2525
if !BindAndValid(c, &json) {
2626
return
2727
}
2828

2929
settings.ServerSettings = &json.Server
30-
settings.NginxLogSettings = &json.NginxLog
30+
settings.NginxSettings = &json.Nginx
3131
settings.OpenAISettings = &json.Openai
3232

3333
settings.ReflectFrom()

server/internal/nginx/nginx.go

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@ import (
88
"regexp"
99
)
1010

11+
func execShell(cmd string) (out string, err error) {
12+
bytes, err := exec.Command("/bin/sh", "-c", cmd).CombinedOutput()
13+
out = string(bytes)
14+
return
15+
}
16+
1117
func TestConf() string {
1218
out, err := exec.Command("nginx", "-t").CombinedOutput()
1319
if err != nil {
@@ -18,29 +24,53 @@ func TestConf() string {
1824
}
1925

2026
func Reload() string {
21-
out, err := exec.Command("nginx", "-s", "reload").CombinedOutput()
27+
if settings.NginxSettings.ReloadCmd != "" {
28+
out, err := execShell(settings.NginxSettings.ReloadCmd)
2229

23-
if err != nil {
24-
logger.Error(err)
30+
if err != nil {
31+
logger.Error(err)
32+
}
33+
34+
return out
35+
36+
} else {
37+
out, err := exec.Command("nginx", "-s", "reload").CombinedOutput()
38+
39+
if err != nil {
40+
logger.Error(err)
41+
}
42+
43+
return string(out)
2544
}
2645

27-
return string(out)
2846
}
2947

3048
func Restart() string {
31-
out, err := exec.Command("nginx", "-s", "reopen").CombinedOutput()
49+
if settings.NginxSettings.RestartCmd != "" {
50+
out, err := execShell(settings.NginxSettings.RestartCmd)
3251

33-
if err != nil {
34-
logger.Error(err)
52+
if err != nil {
53+
logger.Error(err)
54+
}
55+
56+
return out
57+
} else {
58+
59+
out, err := exec.Command("nginx", "-s", "reopen").CombinedOutput()
60+
61+
if err != nil {
62+
logger.Error(err)
63+
}
64+
65+
return string(out)
3566
}
3667

37-
return string(out)
3868
}
3969

4070
func GetConfPath(dir ...string) string {
4171
var confPath string
4272

43-
if settings.ServerSettings.NginxConfigDir == "" {
73+
if settings.NginxSettings.ConfigDir == "" {
4474
out, err := exec.Command("nginx", "-V").CombinedOutput()
4575
if err != nil {
4676
logger.Error(err)
@@ -54,7 +84,7 @@ func GetConfPath(dir ...string) string {
5484
}
5585
confPath = r.FindStringSubmatch(string(out))[1]
5686
} else {
57-
confPath = settings.ServerSettings.NginxConfigDir
87+
confPath = settings.NginxSettings.ConfigDir
5888
}
5989

6090
return filepath.Join(confPath, filepath.Join(dir...))
@@ -63,7 +93,7 @@ func GetConfPath(dir ...string) string {
6393
func GetNginxPIDPath() string {
6494
var confPath string
6595

66-
if settings.ServerSettings.NginxPIDPath == "" {
96+
if settings.NginxSettings.PIDPath == "" {
6797
out, err := exec.Command("nginx", "-V").CombinedOutput()
6898
if err != nil {
6999
logger.Error(err)
@@ -77,7 +107,7 @@ func GetNginxPIDPath() string {
77107
}
78108
confPath = r.FindStringSubmatch(string(out))[1]
79109
} else {
80-
confPath = settings.ServerSettings.NginxPIDPath
110+
confPath = settings.NginxSettings.PIDPath
81111
}
82112

83113
return confPath

server/settings/settings.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,15 @@ type Server struct {
2828
Demo bool `json:"demo"`
2929
PageSize int `json:"page_size"`
3030
GithubProxy string `json:"github_proxy"`
31-
NginxConfigDir string `json:"nginx_config_dir"`
32-
NginxPIDPath string `json:"nginx_pid_path"`
3331
}
3432

35-
type NginxLog struct {
33+
type Nginx struct {
3634
AccessLogPath string `json:"access_log_path"`
3735
ErrorLogPath string `json:"error_log_path"`
36+
ConfigDir string `json:"nginx_config_dir"`
37+
PIDPath string `json:"nginx_pid_path"`
38+
ReloadCmd string `json:"reload_cmd"`
39+
RestartCmd string `json:"restart_cmd"`
3840
}
3941

4042
type OpenAI struct {
@@ -64,7 +66,7 @@ var ServerSettings = &Server{
6466
GithubProxy: "",
6567
}
6668

67-
var NginxLogSettings = &NginxLog{
69+
var NginxSettings = &Nginx{
6870
AccessLogPath: "",
6971
ErrorLogPath: "",
7072
}
@@ -76,10 +78,10 @@ var GitSettings = &Git{}
7678
var ConfPath string
7779

7880
var sections = map[string]interface{}{
79-
"server": ServerSettings,
80-
"nginx_log": NginxLogSettings,
81-
"openai": OpenAISettings,
82-
"git": GitSettings,
81+
"server": ServerSettings,
82+
"nginx": NginxSettings,
83+
"openai": OpenAISettings,
84+
"git": GitSettings,
8385
}
8486

8587
func init() {

0 commit comments

Comments
 (0)