mirror of
https://github.com/0xJacky/nginx-ui.git
synced 2025-05-11 18:35:51 +02:00
enhance: replace error handling with cosy wrapper for nginx reload and test failures
This commit is contained in:
parent
c5e80fb98a
commit
d789326230
14 changed files with 57 additions and 43 deletions
|
@ -6,4 +6,6 @@ var (
|
|||
e = cosy.NewErrorScope("config")
|
||||
ErrPathIsNotUnderTheNginxConfDir = e.New(50006, "path: {0} is not under the nginx conf dir: {1}")
|
||||
ErrDstFileExists = e.New(50007, "destination file: {0} already exists")
|
||||
ErrNginxTestFailed = e.New(50008, "nginx test failed: {0}")
|
||||
ErrNginxReloadFailed = e.New(50009, "nginx reload failed: {0}")
|
||||
)
|
||||
|
|
|
@ -1,13 +1,13 @@
|
|||
package config
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/0xJacky/Nginx-UI/internal/helper"
|
||||
"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"
|
||||
)
|
||||
|
||||
|
@ -47,7 +47,7 @@ func Save(absPath string, content string, cfg *model.Config) (err error) {
|
|||
|
||||
output := nginx.Reload()
|
||||
if nginx.GetLogLevel(output) >= nginx.Warn {
|
||||
return fmt.Errorf("%s", output)
|
||||
return cosy.WrapErrorWithParams(ErrNginxReloadFailed, output)
|
||||
}
|
||||
|
||||
err = SyncToRemoteServer(cfg)
|
||||
|
|
|
@ -2,15 +2,17 @@ package site
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/0xJacky/Nginx-UI/internal/nginx"
|
||||
"github.com/0xJacky/Nginx-UI/internal/notification"
|
||||
"github.com/0xJacky/Nginx-UI/model"
|
||||
"github.com/go-resty/resty/v2"
|
||||
"github.com/uozi-tech/cosy/logger"
|
||||
"net/http"
|
||||
"os"
|
||||
"runtime"
|
||||
"sync"
|
||||
|
||||
"github.com/0xJacky/Nginx-UI/internal/nginx"
|
||||
"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"
|
||||
)
|
||||
|
||||
// Disable disables a site by removing the symlink in sites-enabled
|
||||
|
@ -35,7 +37,7 @@ func Disable(name string) (err error) {
|
|||
|
||||
output := nginx.Reload()
|
||||
if nginx.GetLogLevel(output) > nginx.Warn {
|
||||
return fmt.Errorf("%s", output)
|
||||
return cosy.WrapErrorWithParams(ErrNginxReloadFailed, output)
|
||||
}
|
||||
|
||||
go syncDisable(name)
|
||||
|
|
|
@ -11,6 +11,7 @@ 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"
|
||||
)
|
||||
|
||||
|
@ -37,12 +38,12 @@ func Enable(name string) (err error) {
|
|||
output := nginx.TestConf()
|
||||
if nginx.GetLogLevel(output) > nginx.Warn {
|
||||
_ = os.Remove(enabledConfigFilePath)
|
||||
return fmt.Errorf("%s", output)
|
||||
return cosy.WrapErrorWithParams(ErrNginxTestFailed, output)
|
||||
}
|
||||
|
||||
output = nginx.Reload()
|
||||
if nginx.GetLogLevel(output) > nginx.Warn {
|
||||
return fmt.Errorf("%s", output)
|
||||
return cosy.WrapErrorWithParams(ErrNginxReloadFailed, output)
|
||||
}
|
||||
|
||||
go syncEnable(name)
|
||||
|
|
|
@ -8,4 +8,6 @@ var (
|
|||
ErrDstFileExists = e.New(50001, "destination file already exists")
|
||||
ErrSiteIsEnabled = e.New(50002, "site is enabled")
|
||||
ErrSiteIsInMaintenance = e.New(50003, "site is in maintenance mode")
|
||||
ErrNginxTestFailed = e.New(50004, "nginx test failed: {0}")
|
||||
ErrNginxReloadFailed = e.New(50005, "nginx reload failed: {0}")
|
||||
)
|
||||
|
|
|
@ -16,6 +16,7 @@ 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"
|
||||
)
|
||||
|
@ -82,13 +83,13 @@ func EnableMaintenance(name string) (err error) {
|
|||
if helper.FileExists(originalEnabledPath + "_backup") {
|
||||
_ = os.Rename(originalEnabledPath+"_backup", originalEnabledPath)
|
||||
}
|
||||
return fmt.Errorf("%s", output)
|
||||
return cosy.WrapErrorWithParams(ErrNginxTestFailed, output)
|
||||
}
|
||||
|
||||
// Reload nginx
|
||||
output = nginx.Reload()
|
||||
if nginx.GetLogLevel(output) > nginx.Warn {
|
||||
return fmt.Errorf("%s", output)
|
||||
return cosy.WrapErrorWithParams(ErrNginxReloadFailed, output)
|
||||
}
|
||||
|
||||
// Synchronize with other nodes
|
||||
|
|
|
@ -14,6 +14,7 @@ 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"
|
||||
)
|
||||
|
||||
|
@ -40,13 +41,13 @@ func Save(name string, content string, overwrite bool, envGroupId uint64, syncNo
|
|||
output := nginx.TestConf()
|
||||
|
||||
if nginx.GetLogLevel(output) > nginx.Warn {
|
||||
return fmt.Errorf("%s", output)
|
||||
return cosy.WrapErrorWithParams(ErrNginxTestFailed, output)
|
||||
}
|
||||
|
||||
if postAction == model.PostSyncActionReloadNginx {
|
||||
output = nginx.Reload()
|
||||
if nginx.GetLogLevel(output) > nginx.Warn {
|
||||
return fmt.Errorf("%s", output)
|
||||
return cosy.WrapErrorWithParams(ErrNginxReloadFailed, output)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,15 +2,17 @@ package stream
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/0xJacky/Nginx-UI/internal/nginx"
|
||||
"github.com/0xJacky/Nginx-UI/internal/notification"
|
||||
"github.com/0xJacky/Nginx-UI/model"
|
||||
"github.com/go-resty/resty/v2"
|
||||
"github.com/uozi-tech/cosy/logger"
|
||||
"net/http"
|
||||
"os"
|
||||
"runtime"
|
||||
"sync"
|
||||
|
||||
"github.com/0xJacky/Nginx-UI/internal/nginx"
|
||||
"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"
|
||||
)
|
||||
|
||||
// Disable disables a site by removing the symlink in sites-enabled
|
||||
|
@ -35,7 +37,7 @@ func Disable(name string) (err error) {
|
|||
|
||||
output := nginx.Reload()
|
||||
if nginx.GetLogLevel(output) > nginx.Warn {
|
||||
return fmt.Errorf("%s", output)
|
||||
return cosy.WrapErrorWithParams(ErrNginxReloadFailed, output)
|
||||
}
|
||||
|
||||
go syncDisable(name)
|
||||
|
|
|
@ -2,15 +2,17 @@ package stream
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/0xJacky/Nginx-UI/internal/helper"
|
||||
"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/logger"
|
||||
"net/http"
|
||||
"os"
|
||||
"runtime"
|
||||
"sync"
|
||||
|
||||
"github.com/0xJacky/Nginx-UI/internal/helper"
|
||||
"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"
|
||||
)
|
||||
|
||||
// Enable enables a site by creating a symlink in sites-enabled
|
||||
|
@ -36,12 +38,12 @@ func Enable(name string) (err error) {
|
|||
output := nginx.TestConf()
|
||||
if nginx.GetLogLevel(output) > nginx.Warn {
|
||||
_ = os.Remove(enabledConfigFilePath)
|
||||
return fmt.Errorf("%s", output)
|
||||
return cosy.WrapErrorWithParams(ErrNginxTestFailed, output)
|
||||
}
|
||||
|
||||
output = nginx.Reload()
|
||||
if nginx.GetLogLevel(output) > nginx.Warn {
|
||||
return fmt.Errorf("%s", output)
|
||||
return cosy.WrapErrorWithParams(ErrNginxReloadFailed, output)
|
||||
}
|
||||
|
||||
go syncEnable(name)
|
||||
|
|
|
@ -7,4 +7,6 @@ var (
|
|||
ErrStreamNotFound = e.New(40401, "stream not found")
|
||||
ErrDstFileExists = e.New(50001, "destination file already exists")
|
||||
ErrStreamIsEnabled = e.New(50002, "stream is enabled")
|
||||
ErrNginxTestFailed = e.New(50003, "nginx test failed: {0}")
|
||||
ErrNginxReloadFailed = e.New(50004, "nginx reload failed: {0}")
|
||||
)
|
||||
|
|
|
@ -7,6 +7,7 @@ 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"
|
||||
"net/http"
|
||||
"os"
|
||||
|
@ -49,13 +50,13 @@ func Rename(oldName string, newName string) (err error) {
|
|||
// test nginx configuration
|
||||
output := nginx.TestConf()
|
||||
if nginx.GetLogLevel(output) > nginx.Warn {
|
||||
return fmt.Errorf("%s", output)
|
||||
return cosy.WrapErrorWithParams(ErrNginxTestFailed, output)
|
||||
}
|
||||
|
||||
// reload nginx
|
||||
output = nginx.Reload()
|
||||
if nginx.GetLogLevel(output) > nginx.Warn {
|
||||
return fmt.Errorf("%s", output)
|
||||
return cosy.WrapErrorWithParams(ErrNginxReloadFailed, output)
|
||||
}
|
||||
|
||||
go syncRename(oldName, newName)
|
||||
|
|
|
@ -14,6 +14,7 @@ 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"
|
||||
)
|
||||
|
||||
|
@ -40,13 +41,13 @@ func Save(name string, content string, overwrite bool, syncNodeIds []uint64, pos
|
|||
output := nginx.TestConf()
|
||||
|
||||
if nginx.GetLogLevel(output) > nginx.Warn {
|
||||
return fmt.Errorf("%s", output)
|
||||
return cosy.WrapErrorWithParams(ErrNginxTestFailed, output)
|
||||
}
|
||||
|
||||
if postAction == model.PostSyncActionReloadNginx {
|
||||
output = nginx.Reload()
|
||||
if nginx.GetLogLevel(output) > nginx.Warn {
|
||||
return fmt.Errorf("%s", output)
|
||||
return cosy.WrapErrorWithParams(ErrNginxReloadFailed, output)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue