refactor(nginx): replace direct reload calls with unified control method for better error handling

This commit is contained in:
Jacky
2025-05-28 01:35:21 +00:00
parent eb876498cc
commit 7069cb16d0
14 changed files with 90 additions and 155 deletions
+3 -8
View File
@@ -74,14 +74,9 @@ func AddConfig(c *gin.Context) {
return
}
output, err := nginx.Reload()
if err != nil {
cosy.ErrHandler(c, err)
return
}
if nginx.GetLogLevel(output) >= nginx.Warn {
cosy.ErrHandler(c, cosy.WrapErrorWithParams(config.ErrNginxReloadFailed, output))
res := nginx.Control(nginx.Reload)
if res.IsError() {
res.RespError(c)
return
}
+17 -8
View File
@@ -3,19 +3,21 @@ import { message } from 'ant-design-vue'
import ngx from '@/api/ngx'
import site from '@/api/site'
import NgxConfigEditor, { DirectiveEditor, LocationEditor, useNgxConfigStore } from '@/components/NgxConfigEditor'
import { ConfigStatus } from '@/constants'
import Cert from '../site_edit/components/Cert'
import EnableTLS from '../site_edit/components/EnableTLS'
import { useSiteEditorStore } from '../site_edit/components/SiteEditor/store'
const currentStep = ref(0)
const enabled = ref(true)
const autoCert = ref(false)
onMounted(() => {
init()
})
const ngxConfigStore = useNgxConfigStore()
const editorStore = useSiteEditorStore()
const { ngxConfig, curServerDirectives, curServerLocations } = storeToRefs(ngxConfigStore)
const { curSupportSSL } = storeToRefs(editorStore)
function init() {
site.get_default_template().then(r => {
@@ -106,10 +108,17 @@ async function next() {
</div>
<template v-else-if="currentStep === 1">
<NgxConfigEditor
v-model:auto-cert="autoCert"
:enabled="enabled"
/>
<EnableTLS />
<NgxConfigEditor>
<template v-if="curSupportSSL" #tab-content>
<Cert
class="mb-4"
:site-status="ConfigStatus.Enabled"
:config-name="ngxConfig.name"
/>
</template>
</NgxConfigEditor>
<br>
</template>
@@ -8,7 +8,6 @@ import site from '@/api/site'
import { useNgxConfigStore } from '@/components/NgxConfigEditor'
export const useSiteEditorStore = defineStore('siteEditor', () => {
const name = ref('')
const advanceMode = ref(false)
const parseErrorStatus = ref(false)
const parseErrorMessage = ref('')
@@ -25,10 +24,19 @@ export const useSiteEditorStore = defineStore('siteEditor', () => {
const ngxConfigStore = useNgxConfigStore()
const { ngxConfig, configText, curServerIdx, curServer, curServerDirectives, curDirectivesMap } = storeToRefs(ngxConfigStore)
const name = computed({
get() {
return ngxConfig.value.name
},
set(v) {
ngxConfig.value.name = v
},
})
async function init(_name: string) {
loading.value = true
name.value = _name
await nextTick()
name.value = _name
if (name.value) {
try {
+3 -8
View File
@@ -7,7 +7,6 @@ import (
"github.com/0xJacky/Nginx-UI/internal/nginx"
"github.com/0xJacky/Nginx-UI/model"
"github.com/0xJacky/Nginx-UI/query"
"github.com/uozi-tech/cosy"
"gorm.io/gen/field"
)
@@ -45,13 +44,9 @@ func Save(absPath string, content string, cfg *model.Config) (err error) {
return
}
output, err := nginx.Reload()
if err != nil {
return
}
if nginx.GetLogLevel(output) >= nginx.Warn {
return cosy.WrapErrorWithParams(ErrNginxReloadFailed, output)
res := nginx.Control(nginx.Reload)
if res.IsError() {
return res.GetError()
}
err = SyncToRemoteServer(cfg)
+3 -7
View File
@@ -11,7 +11,6 @@ import (
"github.com/0xJacky/Nginx-UI/internal/notification"
"github.com/0xJacky/Nginx-UI/model"
"github.com/go-resty/resty/v2"
"github.com/uozi-tech/cosy"
"github.com/uozi-tech/cosy/logger"
)
@@ -35,12 +34,9 @@ func Disable(name string) (err error) {
return
}
output, err := nginx.Reload()
if err != nil {
return
}
if nginx.GetLogLevel(output) > nginx.Warn {
return cosy.WrapErrorWithParams(ErrNginxReloadFailed, output)
res := nginx.Control(nginx.Reload)
if res.IsError() {
return res.GetError()
}
go syncDisable(name)
+6 -14
View File
@@ -11,7 +11,6 @@ import (
"github.com/0xJacky/Nginx-UI/internal/nginx"
"github.com/0xJacky/Nginx-UI/internal/notification"
"github.com/go-resty/resty/v2"
"github.com/uozi-tech/cosy"
"github.com/uozi-tech/cosy/logger"
)
@@ -35,21 +34,14 @@ func Enable(name string) (err error) {
}
// Test nginx config, if not pass, then disable the site.
output, err := nginx.TestConfig()
if err != nil {
return
}
if nginx.GetLogLevel(output) > nginx.Warn {
_ = os.Remove(enabledConfigFilePath)
return cosy.WrapErrorWithParams(ErrNginxTestFailed, output)
res := nginx.Control(nginx.TestConfig)
if res.IsError() {
return res.GetError()
}
output, err = nginx.Reload()
if err != nil {
return
}
if nginx.GetLogLevel(output) > nginx.Warn {
return cosy.WrapErrorWithParams(ErrNginxReloadFailed, output)
res = nginx.Control(nginx.Reload)
if res.IsError() {
return res.GetError()
}
go syncEnable(name)
+12 -25
View File
@@ -16,7 +16,6 @@ import (
"github.com/go-resty/resty/v2"
"github.com/tufanbarisyildirim/gonginx/config"
"github.com/tufanbarisyildirim/gonginx/parser"
"github.com/uozi-tech/cosy"
"github.com/uozi-tech/cosy/logger"
cSettings "github.com/uozi-tech/cosy/settings"
)
@@ -76,26 +75,20 @@ func EnableMaintenance(name string) (err error) {
}
// Test nginx config, if not pass, then restore original configuration
output, err := nginx.TestConfig()
if err != nil {
return
}
if nginx.GetLogLevel(output) > nginx.Warn {
res := nginx.Control(nginx.TestConfig)
if res.IsError() {
// Configuration error, cleanup and revert
_ = os.Remove(maintenanceConfigPath)
if helper.FileExists(originalEnabledPath + "_backup") {
_ = os.Rename(originalEnabledPath+"_backup", originalEnabledPath)
}
return cosy.WrapErrorWithParams(ErrNginxTestFailed, output)
return res.GetError()
}
// Reload nginx
output, err = nginx.Reload()
if err != nil {
return
}
if nginx.GetLogLevel(output) > nginx.Warn {
return cosy.WrapErrorWithParams(ErrNginxReloadFailed, output)
res = nginx.Control(nginx.Reload)
if res.IsError() {
return res.GetError()
}
// Synchronize with other nodes
@@ -138,24 +131,18 @@ func DisableMaintenance(name string) (err error) {
}
// Test nginx config, if not pass, then revert
output, err := nginx.TestConfig()
if err != nil {
return
}
if nginx.GetLogLevel(output) > nginx.Warn {
res := nginx.Control(nginx.TestConfig)
if res.IsError() {
// Configuration error, cleanup and revert
_ = os.Remove(enabledConfigFilePath)
_ = os.Symlink(configFilePath, maintenanceConfigPath)
return fmt.Errorf("%s", output)
return res.GetError()
}
// Reload nginx
output, err = nginx.Reload()
if err != nil {
return
}
if nginx.GetLogLevel(output) > nginx.Warn {
return fmt.Errorf("%s", output)
res = nginx.Control(nginx.Reload)
if res.IsError() {
return res.GetError()
}
// Synchronize with other nodes
+6 -12
View File
@@ -48,21 +48,15 @@ func Rename(oldName string, newName string) (err error) {
}
// test nginx configuration
output, err := nginx.TestConfig()
if err != nil {
return
}
if nginx.GetLogLevel(output) > nginx.Warn {
return fmt.Errorf("%s", output)
res := nginx.Control(nginx.TestConfig)
if res.IsError() {
return res.GetError()
}
// reload nginx
output, err = nginx.Reload()
if err != nil {
return
}
if nginx.GetLogLevel(output) > nginx.Warn {
return fmt.Errorf("%s", output)
res = nginx.Control(nginx.Reload)
if res.IsError() {
return res.GetError()
}
// update ChatGPT history
+6 -15
View File
@@ -14,7 +14,6 @@ import (
"github.com/0xJacky/Nginx-UI/model"
"github.com/0xJacky/Nginx-UI/query"
"github.com/go-resty/resty/v2"
"github.com/uozi-tech/cosy"
"github.com/uozi-tech/cosy/logger"
)
@@ -38,23 +37,15 @@ func Save(name string, content string, overwrite bool, envGroupId uint64, syncNo
enabledConfigFilePath := nginx.GetConfPath("sites-enabled", name)
if helper.FileExists(enabledConfigFilePath) {
// Test nginx configuration
var output string
output, err = nginx.TestConfig()
if err != nil {
return
}
if nginx.GetLogLevel(output) > nginx.Warn {
return cosy.WrapErrorWithParams(ErrNginxTestFailed, output)
c := nginx.Control(nginx.TestConfig)
if c.IsError() {
return c.GetError()
}
if postAction == model.PostSyncActionReloadNginx {
output, err = nginx.Reload()
if err != nil {
return
}
if nginx.GetLogLevel(output) > nginx.Warn {
return cosy.WrapErrorWithParams(ErrNginxReloadFailed, output)
c := nginx.Control(nginx.Reload)
if c.IsError() {
return c.GetError()
}
}
}
+3 -7
View File
@@ -11,7 +11,6 @@ import (
"github.com/0xJacky/Nginx-UI/internal/notification"
"github.com/0xJacky/Nginx-UI/model"
"github.com/go-resty/resty/v2"
"github.com/uozi-tech/cosy"
"github.com/uozi-tech/cosy/logger"
)
@@ -35,12 +34,9 @@ func Disable(name string) (err error) {
return
}
output, err := nginx.Reload()
if err != nil {
return
}
if nginx.GetLogLevel(output) > nginx.Warn {
return cosy.WrapErrorWithParams(ErrNginxReloadFailed, output)
res := nginx.Control(nginx.Reload)
if res.IsError() {
return res.GetError()
}
go syncDisable(name)
+6 -14
View File
@@ -11,7 +11,6 @@ import (
"github.com/0xJacky/Nginx-UI/internal/nginx"
"github.com/0xJacky/Nginx-UI/internal/notification"
"github.com/go-resty/resty/v2"
"github.com/uozi-tech/cosy"
"github.com/uozi-tech/cosy/logger"
)
@@ -35,21 +34,14 @@ func Enable(name string) (err error) {
}
// Test nginx config, if not pass, then disable the site.
output, err := nginx.TestConfig()
if err != nil {
return
}
if nginx.GetLogLevel(output) > nginx.Warn {
_ = os.Remove(enabledConfigFilePath)
return cosy.WrapErrorWithParams(ErrNginxTestFailed, output)
res := nginx.Control(nginx.TestConfig)
if res.IsError() {
return res.GetError()
}
output, err = nginx.Reload()
if err != nil {
return
}
if nginx.GetLogLevel(output) > nginx.Warn {
return cosy.WrapErrorWithParams(ErrNginxReloadFailed, output)
res = nginx.Control(nginx.Reload)
if res.IsError() {
return res.GetError()
}
go syncEnable(name)
+6 -13
View File
@@ -12,7 +12,6 @@ import (
"github.com/0xJacky/Nginx-UI/internal/notification"
"github.com/0xJacky/Nginx-UI/query"
"github.com/go-resty/resty/v2"
"github.com/uozi-tech/cosy"
"github.com/uozi-tech/cosy/logger"
)
@@ -49,21 +48,15 @@ func Rename(oldName string, newName string) (err error) {
}
// test nginx configuration
output, err := nginx.TestConfig()
if err != nil {
return
}
if nginx.GetLogLevel(output) > nginx.Warn {
return cosy.WrapErrorWithParams(ErrNginxTestFailed, output)
res := nginx.Control(nginx.TestConfig)
if res.IsError() {
return res.GetError()
}
// reload nginx
output, err = nginx.Reload()
if err != nil {
return
}
if nginx.GetLogLevel(output) > nginx.Warn {
return cosy.WrapErrorWithParams(ErrNginxReloadFailed, output)
res = nginx.Control(nginx.Reload)
if res.IsError() {
return res.GetError()
}
// update ChatGPT history
+6 -15
View File
@@ -14,7 +14,6 @@ import (
"github.com/0xJacky/Nginx-UI/model"
"github.com/0xJacky/Nginx-UI/query"
"github.com/go-resty/resty/v2"
"github.com/uozi-tech/cosy"
"github.com/uozi-tech/cosy/logger"
)
@@ -37,24 +36,16 @@ func Save(name string, content string, overwrite bool, syncNodeIds []uint64, pos
enabledConfigFilePath := nginx.GetConfPath("streams-enabled", name)
if helper.FileExists(enabledConfigFilePath) {
var output string
// Test nginx configuration
output, err = nginx.TestConfig()
if err != nil {
return
}
if nginx.GetLogLevel(output) > nginx.Warn {
return cosy.WrapErrorWithParams(ErrNginxTestFailed, output)
res := nginx.Control(nginx.TestConfig)
if res.IsError() {
return res.GetError()
}
if postAction == model.PostSyncActionReloadNginx {
output, err = nginx.Reload()
if err != nil {
return
}
if nginx.GetLogLevel(output) > nginx.Warn {
return cosy.WrapErrorWithParams(ErrNginxReloadFailed, output)
res = nginx.Control(nginx.Reload)
if res.IsError() {
return res.GetError()
}
}
}
+3 -7
View File
@@ -71,13 +71,9 @@ func handleNginxConfigAdd(ctx context.Context, request mcp.CallToolRequest) (*mc
return nil, err
}
output, err := nginx.Reload()
if err != nil {
return nil, err
}
if nginx.GetLogLevel(output) >= nginx.Warn {
return nil, config.ErrNginxReloadFailed
res := nginx.Control(nginx.Reload)
if res.IsError() {
return nil, res.GetError()
}
q := query.Config