Browse Source

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

Jacky 1 month ago
parent
commit
7069cb16d0

+ 3 - 8
api/config/add.go

@@ -74,14 +74,9 @@ func AddConfig(c *gin.Context) {
 		return
 		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
 		return
 	}
 	}
 
 

+ 17 - 8
app/src/views/site/site_add/SiteAdd.vue

@@ -3,19 +3,21 @@ import { message } from 'ant-design-vue'
 import ngx from '@/api/ngx'
 import ngx from '@/api/ngx'
 import site from '@/api/site'
 import site from '@/api/site'
 import NgxConfigEditor, { DirectiveEditor, LocationEditor, useNgxConfigStore } from '@/components/NgxConfigEditor'
 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 currentStep = ref(0)
 
 
-const enabled = ref(true)
-
-const autoCert = ref(false)
-
 onMounted(() => {
 onMounted(() => {
   init()
   init()
 })
 })
 
 
 const ngxConfigStore = useNgxConfigStore()
 const ngxConfigStore = useNgxConfigStore()
+const editorStore = useSiteEditorStore()
 const { ngxConfig, curServerDirectives, curServerLocations } = storeToRefs(ngxConfigStore)
 const { ngxConfig, curServerDirectives, curServerLocations } = storeToRefs(ngxConfigStore)
+const { curSupportSSL } = storeToRefs(editorStore)
 
 
 function init() {
 function init() {
   site.get_default_template().then(r => {
   site.get_default_template().then(r => {
@@ -106,10 +108,17 @@ async function next() {
       </div>
       </div>
 
 
       <template v-else-if="currentStep === 1">
       <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>
         <br>
       </template>
       </template>

+ 10 - 2
app/src/views/site/site_edit/components/SiteEditor/store.ts

@@ -8,7 +8,6 @@ import site from '@/api/site'
 import { useNgxConfigStore } from '@/components/NgxConfigEditor'
 import { useNgxConfigStore } from '@/components/NgxConfigEditor'
 
 
 export const useSiteEditorStore = defineStore('siteEditor', () => {
 export const useSiteEditorStore = defineStore('siteEditor', () => {
-  const name = ref('')
   const advanceMode = ref(false)
   const advanceMode = ref(false)
   const parseErrorStatus = ref(false)
   const parseErrorStatus = ref(false)
   const parseErrorMessage = ref('')
   const parseErrorMessage = ref('')
@@ -25,10 +24,19 @@ export const useSiteEditorStore = defineStore('siteEditor', () => {
   const ngxConfigStore = useNgxConfigStore()
   const ngxConfigStore = useNgxConfigStore()
   const { ngxConfig, configText, curServerIdx, curServer, curServerDirectives, curDirectivesMap } = storeToRefs(ngxConfigStore)
   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) {
   async function init(_name: string) {
     loading.value = true
     loading.value = true
-    name.value = _name
     await nextTick()
     await nextTick()
+    name.value = _name
 
 
     if (name.value) {
     if (name.value) {
       try {
       try {

+ 3 - 8
internal/config/save.go

@@ -7,7 +7,6 @@ import (
 	"github.com/0xJacky/Nginx-UI/internal/nginx"
 	"github.com/0xJacky/Nginx-UI/internal/nginx"
 	"github.com/0xJacky/Nginx-UI/model"
 	"github.com/0xJacky/Nginx-UI/model"
 	"github.com/0xJacky/Nginx-UI/query"
 	"github.com/0xJacky/Nginx-UI/query"
-	"github.com/uozi-tech/cosy"
 	"gorm.io/gen/field"
 	"gorm.io/gen/field"
 )
 )
 
 
@@ -45,13 +44,9 @@ func Save(absPath string, content string, cfg *model.Config) (err error) {
 		return
 		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)
 	err = SyncToRemoteServer(cfg)

+ 3 - 7
internal/site/disable.go

@@ -11,7 +11,6 @@ import (
 	"github.com/0xJacky/Nginx-UI/internal/notification"
 	"github.com/0xJacky/Nginx-UI/internal/notification"
 	"github.com/0xJacky/Nginx-UI/model"
 	"github.com/0xJacky/Nginx-UI/model"
 	"github.com/go-resty/resty/v2"
 	"github.com/go-resty/resty/v2"
-	"github.com/uozi-tech/cosy"
 	"github.com/uozi-tech/cosy/logger"
 	"github.com/uozi-tech/cosy/logger"
 )
 )
 
 
@@ -35,12 +34,9 @@ func Disable(name string) (err error) {
 		return
 		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)
 	go syncDisable(name)

+ 6 - 14
internal/site/enable.go

@@ -11,7 +11,6 @@ import (
 	"github.com/0xJacky/Nginx-UI/internal/nginx"
 	"github.com/0xJacky/Nginx-UI/internal/nginx"
 	"github.com/0xJacky/Nginx-UI/internal/notification"
 	"github.com/0xJacky/Nginx-UI/internal/notification"
 	"github.com/go-resty/resty/v2"
 	"github.com/go-resty/resty/v2"
-	"github.com/uozi-tech/cosy"
 	"github.com/uozi-tech/cosy/logger"
 	"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.
 	// 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)
 	go syncEnable(name)

+ 12 - 25
internal/site/maintenance.go

@@ -16,7 +16,6 @@ import (
 	"github.com/go-resty/resty/v2"
 	"github.com/go-resty/resty/v2"
 	"github.com/tufanbarisyildirim/gonginx/config"
 	"github.com/tufanbarisyildirim/gonginx/config"
 	"github.com/tufanbarisyildirim/gonginx/parser"
 	"github.com/tufanbarisyildirim/gonginx/parser"
-	"github.com/uozi-tech/cosy"
 	"github.com/uozi-tech/cosy/logger"
 	"github.com/uozi-tech/cosy/logger"
 	cSettings "github.com/uozi-tech/cosy/settings"
 	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
 	// 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
 		// Configuration error, cleanup and revert
 		_ = os.Remove(maintenanceConfigPath)
 		_ = os.Remove(maintenanceConfigPath)
 		if helper.FileExists(originalEnabledPath + "_backup") {
 		if helper.FileExists(originalEnabledPath + "_backup") {
 			_ = os.Rename(originalEnabledPath+"_backup", originalEnabledPath)
 			_ = os.Rename(originalEnabledPath+"_backup", originalEnabledPath)
 		}
 		}
-		return cosy.WrapErrorWithParams(ErrNginxTestFailed, output)
+		return res.GetError()
 	}
 	}
 
 
 	// Reload nginx
 	// 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
 	// Synchronize with other nodes
@@ -138,24 +131,18 @@ func DisableMaintenance(name string) (err error) {
 	}
 	}
 
 
 	// Test nginx config, if not pass, then revert
 	// 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
 		// Configuration error, cleanup and revert
 		_ = os.Remove(enabledConfigFilePath)
 		_ = os.Remove(enabledConfigFilePath)
 		_ = os.Symlink(configFilePath, maintenanceConfigPath)
 		_ = os.Symlink(configFilePath, maintenanceConfigPath)
-		return fmt.Errorf("%s", output)
+		return res.GetError()
 	}
 	}
 
 
 	// Reload nginx
 	// 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
 	// Synchronize with other nodes

+ 6 - 12
internal/site/rename.go

@@ -48,21 +48,15 @@ func Rename(oldName string, newName string) (err error) {
 	}
 	}
 
 
 	// test nginx configuration
 	// 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
 	// 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
 	// update ChatGPT history

+ 6 - 15
internal/site/save.go

@@ -14,7 +14,6 @@ import (
 	"github.com/0xJacky/Nginx-UI/model"
 	"github.com/0xJacky/Nginx-UI/model"
 	"github.com/0xJacky/Nginx-UI/query"
 	"github.com/0xJacky/Nginx-UI/query"
 	"github.com/go-resty/resty/v2"
 	"github.com/go-resty/resty/v2"
-	"github.com/uozi-tech/cosy"
 	"github.com/uozi-tech/cosy/logger"
 	"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)
 	enabledConfigFilePath := nginx.GetConfPath("sites-enabled", name)
 	if helper.FileExists(enabledConfigFilePath) {
 	if helper.FileExists(enabledConfigFilePath) {
 		// Test nginx configuration
 		// 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 {
 		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
internal/stream/disable.go

@@ -11,7 +11,6 @@ import (
 	"github.com/0xJacky/Nginx-UI/internal/notification"
 	"github.com/0xJacky/Nginx-UI/internal/notification"
 	"github.com/0xJacky/Nginx-UI/model"
 	"github.com/0xJacky/Nginx-UI/model"
 	"github.com/go-resty/resty/v2"
 	"github.com/go-resty/resty/v2"
-	"github.com/uozi-tech/cosy"
 	"github.com/uozi-tech/cosy/logger"
 	"github.com/uozi-tech/cosy/logger"
 )
 )
 
 
@@ -35,12 +34,9 @@ func Disable(name string) (err error) {
 		return
 		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)
 	go syncDisable(name)

+ 6 - 14
internal/stream/enable.go

@@ -11,7 +11,6 @@ import (
 	"github.com/0xJacky/Nginx-UI/internal/nginx"
 	"github.com/0xJacky/Nginx-UI/internal/nginx"
 	"github.com/0xJacky/Nginx-UI/internal/notification"
 	"github.com/0xJacky/Nginx-UI/internal/notification"
 	"github.com/go-resty/resty/v2"
 	"github.com/go-resty/resty/v2"
-	"github.com/uozi-tech/cosy"
 	"github.com/uozi-tech/cosy/logger"
 	"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.
 	// 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)
 	go syncEnable(name)

+ 6 - 13
internal/stream/rename.go

@@ -12,7 +12,6 @@ import (
 	"github.com/0xJacky/Nginx-UI/internal/notification"
 	"github.com/0xJacky/Nginx-UI/internal/notification"
 	"github.com/0xJacky/Nginx-UI/query"
 	"github.com/0xJacky/Nginx-UI/query"
 	"github.com/go-resty/resty/v2"
 	"github.com/go-resty/resty/v2"
-	"github.com/uozi-tech/cosy"
 	"github.com/uozi-tech/cosy/logger"
 	"github.com/uozi-tech/cosy/logger"
 )
 )
 
 
@@ -49,21 +48,15 @@ func Rename(oldName string, newName string) (err error) {
 	}
 	}
 
 
 	// test nginx configuration
 	// 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
 	// 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
 	// update ChatGPT history

+ 6 - 15
internal/stream/save.go

@@ -14,7 +14,6 @@ import (
 	"github.com/0xJacky/Nginx-UI/model"
 	"github.com/0xJacky/Nginx-UI/model"
 	"github.com/0xJacky/Nginx-UI/query"
 	"github.com/0xJacky/Nginx-UI/query"
 	"github.com/go-resty/resty/v2"
 	"github.com/go-resty/resty/v2"
-	"github.com/uozi-tech/cosy"
 	"github.com/uozi-tech/cosy/logger"
 	"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)
 	enabledConfigFilePath := nginx.GetConfPath("streams-enabled", name)
 	if helper.FileExists(enabledConfigFilePath) {
 	if helper.FileExists(enabledConfigFilePath) {
-		var output string
 		// Test nginx configuration
 		// 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 {
 		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
mcp/config/config_add.go

@@ -71,13 +71,9 @@ func handleNginxConfigAdd(ctx context.Context, request mcp.CallToolRequest) (*mc
 		return nil, err
 		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
 	q := query.Config