Skip to content

Commit 58f5d6d

Browse files
committed
refactor(nginx): simplify control error handling
1 parent ab97f94 commit 58f5d6d

File tree

8 files changed

+92
-55
lines changed

8 files changed

+92
-55
lines changed

api/nginx/control.go

Lines changed: 8 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -9,34 +9,12 @@ import (
99

1010
// Reload reloads the nginx
1111
func Reload(c *gin.Context) {
12-
output, err := nginx.Reload()
13-
if err != nil {
14-
c.JSON(http.StatusInternalServerError, gin.H{
15-
"message": output + err.Error(),
16-
"level": nginx.GetLogLevel(output),
17-
})
18-
return
19-
}
20-
c.JSON(http.StatusOK, gin.H{
21-
"message": output,
22-
"level": nginx.GetLogLevel(output),
23-
})
12+
nginx.Control(nginx.Reload).Resp(c)
2413
}
2514

2615
// TestConfig tests the nginx config
2716
func TestConfig(c *gin.Context) {
28-
output, err := nginx.TestConfig()
29-
if err != nil {
30-
c.JSON(http.StatusInternalServerError, gin.H{
31-
"message": output + err.Error(),
32-
"level": nginx.GetLogLevel(output),
33-
})
34-
return
35-
}
36-
c.JSON(http.StatusOK, gin.H{
37-
"message": output,
38-
"level": nginx.GetLogLevel(output),
39-
})
17+
nginx.Control(nginx.TestConfig).Resp(c)
4018
}
4119

4220
// Restart restarts the nginx
@@ -49,20 +27,17 @@ func Restart(c *gin.Context) {
4927

5028
// Status returns the status of the nginx
5129
func Status(c *gin.Context) {
52-
lastOutput, err := nginx.GetLastOutput()
53-
if err != nil {
54-
c.JSON(http.StatusInternalServerError, gin.H{
55-
"message": lastOutput + err.Error(),
56-
"level": nginx.GetLogLevel(lastOutput),
57-
})
30+
lastResult := nginx.GetLastResult()
31+
if lastResult.IsError() {
32+
lastResult.RespError(c)
5833
return
5934
}
6035

61-
running := nginx.IsNginxRunning()
36+
running := nginx.IsRunning()
6237

6338
c.JSON(http.StatusOK, gin.H{
6439
"running": running,
65-
"message": lastOutput,
66-
"level": nginx.GetLogLevel(lastOutput),
40+
"message": lastResult.GetOutput(),
41+
"level": lastResult.GetLevel(),
6742
})
6843
}

internal/nginx/control.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package nginx
2+
3+
import (
4+
"errors"
5+
"net/http"
6+
"strings"
7+
8+
"github.com/gin-gonic/gin"
9+
"github.com/uozi-tech/cosy"
10+
)
11+
12+
type ControlFunc func() (stdOut string, stdErr error)
13+
14+
type ControlResult struct {
15+
stdOut string
16+
stdErr error
17+
}
18+
19+
type ControlResp struct {
20+
Message string `json:"message"`
21+
Level int `json:"level"`
22+
}
23+
24+
func Control(controlFunc ControlFunc) *ControlResult {
25+
stdout, stderr := controlFunc()
26+
return &ControlResult{
27+
stdOut: stdout,
28+
stdErr: stderr,
29+
}
30+
}
31+
32+
func (t *ControlResult) IsError() bool {
33+
return GetLogLevel(t.stdOut) > Warn || t.stdErr != nil
34+
}
35+
36+
func (t *ControlResult) Resp(c *gin.Context) {
37+
if t.IsError() {
38+
t.RespError(c)
39+
return
40+
}
41+
c.JSON(http.StatusOK, ControlResp{
42+
Message: t.stdOut,
43+
Level: GetLogLevel(t.stdOut),
44+
})
45+
}
46+
47+
func (t *ControlResult) RespError(c *gin.Context) {
48+
cosy.ErrHandler(c,
49+
cosy.WrapErrorWithParams(ErrNginx, strings.Join([]string{t.stdOut, t.stdErr.Error()}, " ")))
50+
}
51+
52+
func (t *ControlResult) GetOutput() string {
53+
return strings.Join([]string{t.stdOut, t.stdErr.Error()}, " ")
54+
}
55+
56+
func (t *ControlResult) GetError() error {
57+
return errors.New(t.GetOutput())
58+
}
59+
60+
func (t *ControlResult) GetLevel() int {
61+
return GetLogLevel(t.stdOut)
62+
}

internal/nginx/errors.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ package nginx
33
import "github.com/uozi-tech/cosy"
44

55
var (
6-
e = cosy.NewErrorScope("nginx")
7-
ErrBlockIsNil = e.New(50001, "block is nil")
6+
e = cosy.NewErrorScope("nginx")
7+
ErrNginx = e.New(50000, "nginx error: {0}")
8+
ErrBlockIsNil = e.New(50001, "block is nil")
89
ErrReloadFailed = e.New(50002, "reload nginx failed: {0}")
910
)

internal/nginx/modules.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func isPIDFileChanged() bool {
5151
}
5252

5353
// If Nginx is not running, consider PID changed
54-
if !IsNginxRunning() {
54+
if !IsRunning() {
5555
return true
5656
}
5757

@@ -101,7 +101,7 @@ func updateDynamicModulesStatus() {
101101
// Look for lines like "load_module modules/ngx_http_image_filter_module.so;"
102102
loadModuleRe := regexp.MustCompile(`load_module\s+(?:modules/|/.*/)([a-zA-Z0-9_-]+)\.so;`)
103103
matches := loadModuleRe.FindAllStringSubmatch(out, -1)
104-
104+
105105
for _, match := range matches {
106106
if len(match) > 1 {
107107
// Extract the module name without path and suffix

internal/nginx/nginx.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,13 +72,16 @@ func Restart() {
7272
}
7373

7474
// GetLastOutput returns the last output of the nginx command
75-
func GetLastOutput() (stdOut string, stdErr error) {
75+
func GetLastResult() *ControlResult {
7676
mutex.Lock()
7777
defer mutex.Unlock()
78-
return lastStdOut, lastStdErr
78+
return &ControlResult{
79+
stdOut: lastStdOut,
80+
stdErr: lastStdErr,
81+
}
7982
}
8083

81-
func IsNginxRunning() bool {
84+
func IsRunning() bool {
8285
pidPath := GetPIDPath()
8386
switch settings.NginxSettings.RunningInAnotherContainer() {
8487
case true:

internal/performance/performance.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ type NginxPerformanceResponse struct {
1919

2020
func GetPerformanceData() NginxPerformanceResponse {
2121
// Check if Nginx is running
22-
running := nginx.IsNginxRunning()
22+
running := nginx.IsRunning()
2323
if !running {
2424
return NginxPerformanceResponse{
2525
StubStatusEnabled: false,

mcp/nginx/restart.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ var nginxRestartTool = mcp.NewTool(
1616

1717
func handleNginxRestart(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) {
1818
nginx.Restart()
19-
output, err := nginx.GetLastOutput()
20-
if err != nil {
21-
return mcp.NewToolResultError(output + "\n" + err.Error()), err
19+
lastResult := nginx.GetLastResult()
20+
if lastResult.IsError() {
21+
return mcp.NewToolResultError(lastResult.GetOutput()), lastResult.GetError()
2222
}
23-
return mcp.NewToolResultText(output), nil
23+
return mcp.NewToolResultText(lastResult.GetOutput()), nil
2424
}

mcp/nginx/status.go

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,15 @@ var statusTool = mcp.NewTool(
1919

2020
// handleNginxStatus handles the Nginx status request
2121
func handleNginxStatus(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) {
22-
lastOutput, err := nginx.GetLastOutput()
23-
if err != nil {
24-
return mcp.NewToolResultError(lastOutput + "\n" + err.Error()), err
22+
lastResult := nginx.GetLastResult()
23+
if lastResult.IsError() {
24+
return mcp.NewToolResultError(lastResult.GetOutput()), lastResult.GetError()
2525
}
26-
27-
running := nginx.IsNginxRunning()
28-
level := nginx.GetLogLevel(lastOutput)
29-
3026
// build result
3127
result := gin.H{
32-
"running": running,
33-
"message": lastOutput,
34-
"level": level,
28+
"running": nginx.IsRunning(),
29+
"message": lastResult.GetOutput(),
30+
"level": lastResult.GetLevel(),
3531
}
3632

3733
// marshal to json and return text result

0 commit comments

Comments
 (0)