refactor: self check

This commit is contained in:
Jacky 2025-04-27 15:43:18 +08:00
parent 28ed291250
commit 63824d5724
No known key found for this signature in database
GPG key ID: 215C21B10DF38B4D
34 changed files with 4357 additions and 2551 deletions

View file

@ -4,18 +4,25 @@ import "github.com/uozi-tech/cosy"
var (
e = cosy.NewErrorScope("self_check")
ErrTaskNotFound = e.New(4040, "Task not found")
ErrFailedToReadNginxConf = e.New(4041, "Failed to read nginx.conf")
ErrParseNginxConf = e.New(5001, "Failed to parse nginx.conf")
ErrNginxConfNoHttpBlock = e.New(4042, "Nginx conf no http block")
ErrNginxConfNotIncludeSitesEnabled = e.New(4043, "Nginx conf not include sites-enabled")
ErrorNginxConfNoStreamBlock = e.New(4044, "Nginx conf no stream block")
ErrNginxConfNotIncludeStreamEnabled = e.New(4045, "Nginx conf not include stream-enabled")
ErrFailedToCreateBackup = e.New(5002, "Failed to create backup")
ErrSitesAvailableNotExist = e.New(4046, "Sites-available directory not exist")
ErrSitesEnabledNotExist = e.New(4047, "Sites-enabled directory not exist")
ErrStreamAvailableNotExist = e.New(4048, "Streams-available directory not exist")
ErrStreamEnabledNotExist = e.New(4049, "Streams-enabled directory not exist")
ErrNginxConfNotIncludeConfD = e.New(4050, "Nginx conf not include conf.d directory")
ErrDockerSocketNotExist = e.New(4051, "Docker socket not exist")
ErrTaskNotFound = e.New(40400, "Task not found")
ErrTaskNotFixable = e.New(40401, "Task is not fixable")
ErrFailedToReadNginxConf = e.New(40402, "Failed to read nginx.conf")
ErrParseNginxConf = e.New(50001, "Failed to parse nginx.conf")
ErrNginxConfNoHttpBlock = e.New(40403, "Nginx conf no http block")
ErrNginxConfNotIncludeSitesEnabled = e.New(40404, "Nginx conf not include sites-enabled")
ErrNginxConfNoStreamBlock = e.New(40405, "Nginx conf no stream block")
ErrNginxConfNotIncludeStreamEnabled = e.New(40406, "Nginx conf not include stream-enabled")
ErrFailedToCreateBackup = e.New(50002, "Failed to create backup")
ErrSitesAvailableNotExist = e.New(40407, "Sites-available directory not exist")
ErrSitesEnabledNotExist = e.New(40408, "Sites-enabled directory not exist")
ErrStreamAvailableNotExist = e.New(40409, "Streams-available directory not exist")
ErrStreamEnabledNotExist = e.New(40410, "Streams-enabled directory not exist")
ErrNginxConfNotIncludeConfD = e.New(40411, "Nginx conf not include conf.d directory")
ErrDockerSocketNotExist = e.New(40412, "Docker socket not exist")
ErrConfigDirNotExist = e.New(40413, "Config directory not exist")
ErrConfigEntryFileNotExist = e.New(40414, "Config entry file not exist")
ErrPIDPathNotExist = e.New(40415, "PID path not exist")
ErrSbinPathNotExist = e.New(40416, "Sbin path not exist")
ErrAccessLogPathNotExist = e.New(40417, "Access log path not exist")
ErrErrorLogPathNotExist = e.New(40418, "Error log path not exist")
)

View file

@ -0,0 +1,78 @@
package self_check
import (
"github.com/0xJacky/Nginx-UI/internal/helper"
"github.com/0xJacky/Nginx-UI/internal/nginx"
)
// CheckConfigDir checks if the config directory exists
func CheckConfigDir() error {
dir := nginx.GetConfPath()
if dir == "" {
return ErrConfigDirNotExist
}
if !helper.FileExists(dir) {
return ErrConfigDirNotExist
}
return nil
}
// CheckConfigEntryFile checks if the config entry file exists
func CheckConfigEntryFile() error {
dir := nginx.GetConfPath()
if dir == "" {
return ErrConfigEntryFileNotExist
}
if !helper.FileExists(dir) {
return ErrConfigEntryFileNotExist
}
return nil
}
// CheckPIDPath checks if the PID path exists
func CheckPIDPath() error {
path := nginx.GetPIDPath()
if path == "" {
return ErrPIDPathNotExist
}
if !helper.FileExists(path) {
return ErrPIDPathNotExist
}
return nil
}
// CheckSbinPath checks if the sbin path exists
func CheckSbinPath() error {
path := nginx.GetSbinPath()
if path == "" {
return ErrSbinPathNotExist
}
if !helper.FileExists(path) {
return ErrSbinPathNotExist
}
return nil
}
// CheckAccessLogPath checks if the access log path exists
func CheckAccessLogPath() error {
path := nginx.GetAccessLogPath()
if path == "" {
return ErrAccessLogPathNotExist
}
if !helper.FileExists(path) {
return ErrAccessLogPathNotExist
}
return nil
}
// CheckErrorLogPath checks if the error log path exists
func CheckErrorLogPath() error {
path := nginx.GetErrorLogPath()
if path == "" {
return ErrErrorLogPathNotExist
}
if !helper.FileExists(path) {
return ErrErrorLogPathNotExist
}
return nil
}

View file

@ -75,7 +75,7 @@ func CheckNginxConfIncludeStreams() error {
}
}
return ErrorNginxConfNoStreamBlock
return ErrNginxConfNoStreamBlock
}
// FixNginxConfIncludeSites attempts to fix nginx.conf include sites-enabled

View file

@ -6,27 +6,33 @@ import (
"github.com/uozi-tech/cosy"
)
func Run() (reports Reports) {
reports = make(Reports, 0)
for _, task := range selfCheckTasks {
var cErr *cosy.Error
status := ReportStatusSuccess
if err := task.CheckFunc(); err != nil {
errors.As(err, &cErr)
status = ReportStatusError
}
reports = append(reports, &Report{
Name: task.Name,
Err: cErr,
Name: task.Name,
Description: task.Description,
Fixable: task.FixFunc != nil,
Err: cErr,
Status: status,
})
}
return
}
func AttemptFix(taskName string) (err error) {
task, ok := selfCheckTaskMap[taskName]
task, ok := selfCheckTaskMap.Get(taskName)
if !ok {
return ErrTaskNotFound
}
if task.FixFunc == nil {
return ErrTaskNotFixable
}
return task.FixFunc()
}

View file

@ -2,60 +2,123 @@ package self_check
import (
"github.com/0xJacky/Nginx-UI/internal/helper"
"github.com/0xJacky/Nginx-UI/internal/translation"
"github.com/elliotchance/orderedmap/v3"
"github.com/uozi-tech/cosy"
)
type Task struct {
Name string
CheckFunc func() error
FixFunc func() error
Key string
Name *translation.Container
Description *translation.Container
CheckFunc func() error
FixFunc func() error
}
type ReportStatus string
const (
ReportStatusSuccess ReportStatus = "success"
ReportStatusWarning ReportStatus = "warning"
ReportStatusError ReportStatus = "error"
)
type Report struct {
Name string `json:"name"`
Err *cosy.Error `json:"err,omitempty"`
Name *translation.Container `json:"name"`
Description *translation.Container `json:"description,omitempty"`
Fixable bool `json:"fixable"`
Err *cosy.Error `json:"err,omitempty"`
Status ReportStatus `json:"status"`
}
type Reports []*Report
var selfCheckTasks = []*Task{
{
Name: "Directory-Sites",
Key: "Directory-Sites",
Name: translation.C("Sites directory exists"),
Description: translation.C("Check if the " +
"sites-available and sites-enabled directories are " +
"under the nginx configuration directory"),
CheckFunc: CheckSitesDirectory,
FixFunc: FixSitesDirectory,
},
{
Name: "Directory-Streams",
Key: "Directory-Streams",
Name: translation.C("Streams directory exists"),
Description: translation.C("Check if the " +
"streams-available and streams-enabled directories are " +
"under the nginx configuration directory"),
CheckFunc: CheckStreamDirectory,
FixFunc: FixStreamDirectory,
},
{
Name: "NginxConf-Sites-Enabled",
Key: "NginxConf-Sites-Enabled",
Name: translation.C("Nginx.conf includes sites-enabled directory"),
Description: translation.C("Check if the nginx.conf includes the " +
"sites-enabled directory"),
CheckFunc: CheckNginxConfIncludeSites,
FixFunc: FixNginxConfIncludeSites,
},
{
Name: "NginxConf-Streams-Enabled",
Key: "NginxConf-Streams-Enabled",
Name: translation.C("Nginx.conf includes streams-enabled directory"),
Description: translation.C("Check if the nginx.conf includes the " +
"streams-enabled directory"),
CheckFunc: CheckNginxConfIncludeStreams,
FixFunc: FixNginxConfIncludeStreams,
},
{
Name: "NginxConf-ConfD",
Key: "NginxConf-ConfD",
Name: translation.C("Nginx.conf includes conf.d directory"),
Description: translation.C("Check if the nginx.conf includes the " +
"conf.d directory"),
CheckFunc: CheckNginxConfIncludeConfD,
FixFunc: FixNginxConfIncludeConfD,
},
{
Key: "NginxConf-Directory",
Name: translation.C("Nginx configuration directory exists"),
Description: translation.C("Check if the nginx configuration directory exists"),
CheckFunc: CheckConfigDir,
},
{
Key: "NginxConf-Entry-File",
Name: translation.C("Nginx configuration entry file exists"),
Description: translation.C("Check if the nginx configuration entry file exists"),
CheckFunc: CheckConfigEntryFile,
},
{
Key: "NginxPID-Path",
Name: translation.C("Nginx PID path exists"),
Description: translation.C("Check if the nginx PID path exists"),
CheckFunc: CheckPIDPath,
},
{
Key: "NginxAccessLog-Path",
Name: translation.C("Nginx access log path exists"),
Description: translation.C("Check if the nginx access log path exists"),
CheckFunc: CheckAccessLogPath,
},
{
Key: "NginxErrorLog-Path",
Name: translation.C("Nginx error log path exists"),
Description: translation.C("Check if the nginx error log path exists"),
CheckFunc: CheckErrorLogPath,
},
}
var selfCheckTaskMap = make(map[string]*Task)
var selfCheckTaskMap = orderedmap.NewOrderedMap[string, *Task]()
func init() {
for _, task := range selfCheckTasks {
selfCheckTaskMap[task.Name] = task
selfCheckTaskMap.Set(task.Key, task)
}
if helper.InNginxUIOfficialDocker() {
selfCheckTasks = append(selfCheckTasks, &Task{
Name: "Docker-Socket",
CheckFunc: CheckDockerSocket,
Name: translation.C("Docker socket exists"),
Description: translation.C("Check if the docker socket exists."),
CheckFunc: CheckDockerSocket,
})
}
}