From 1e8c4945d5efb3767abf7f23310797dc15342262 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=80=97=E5=AD=90?= Date: Sat, 16 Mar 2024 22:42:36 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E7=BB=9F=E4=B8=80=E7=B1=BB?= =?UTF-8?q?=E5=9E=8B=E7=BB=93=E6=9E=84=E4=BD=93=E8=87=B3=20types=20?= =?UTF-8?q?=E7=9B=AE=E5=BD=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/http/controllers/plugins/constants.go | 39 ------------------- .../plugins/fail2ban_controller.go | 9 +++-- .../plugins/postgresql15_controller.go | 3 +- .../plugins/postgresql16_controller.go | 3 +- .../controllers/plugins/redis_controller.go | 3 +- .../controllers/plugins/rsync_controller.go | 11 +++--- .../controllers/plugins/s3fs_controller.go | 17 ++++---- internal/constants.go | 11 ------ types/fail2ban.go | 10 +++++ types/rsync.go | 11 ++++++ types/s3fs.go | 8 ++++ 11 files changed, 55 insertions(+), 70 deletions(-) delete mode 100644 app/http/controllers/plugins/constants.go delete mode 100644 internal/constants.go create mode 100644 types/fail2ban.go create mode 100644 types/rsync.go create mode 100644 types/s3fs.go diff --git a/app/http/controllers/plugins/constants.go b/app/http/controllers/plugins/constants.go deleted file mode 100644 index 9b0e2159..00000000 --- a/app/http/controllers/plugins/constants.go +++ /dev/null @@ -1,39 +0,0 @@ -package plugins - -type PHPExtension struct { - Name string `json:"name"` - Slug string `json:"slug"` - Description string `json:"description"` - Installed bool `json:"installed"` -} - -type LoadInfo struct { - Name string `json:"name"` - Value string `json:"value"` -} - -type Fail2banJail struct { - Name string `json:"name"` - Enabled bool `json:"enabled"` - LogPath string `json:"log_path"` - MaxRetry int `json:"max_retry"` - FindTime int `json:"find_time"` - BanTime int `json:"ban_time"` -} - -type S3fsMount struct { - ID int64 `json:"id"` - Path string `json:"path"` - Bucket string `json:"bucket"` - Url string `json:"url"` -} - -type RsyncModule struct { - Name string `json:"name"` - Path string `json:"path"` - Comment string `json:"comment"` - ReadOnly bool `json:"read_only"` - AuthUser string `json:"auth_user"` - Secret string `json:"secret"` - HostsAllow string `json:"hosts_allow"` -} diff --git a/app/http/controllers/plugins/fail2ban_controller.go b/app/http/controllers/plugins/fail2ban_controller.go index 0c25abc7..1415e7d9 100644 --- a/app/http/controllers/plugins/fail2ban_controller.go +++ b/app/http/controllers/plugins/fail2ban_controller.go @@ -13,6 +13,7 @@ import ( "panel/internal" "panel/internal/services" "panel/pkg/tools" + "panel/types" ) type Fail2banController struct { @@ -89,7 +90,7 @@ func (r *Fail2banController) List(ctx http.Context) http.Response { return controllers.Error(ctx, http.StatusUnprocessableEntity, "Fail2ban 规则为空") } - var jails []Fail2banJail + var jails []types.Fail2banJail for i, jail := range jailList { if i == 0 { continue @@ -106,7 +107,7 @@ func (r *Fail2banController) List(ctx http.Context) http.Response { jailFindTime := regexp.MustCompile(`findtime = (.*)`).FindStringSubmatch(jailRaw) jailBanTime := regexp.MustCompile(`bantime = (.*)`).FindStringSubmatch(jailRaw) - jails = append(jails, Fail2banJail{ + jails = append(jails, types.Fail2banJail{ Name: jailName, Enabled: jailEnabled, LogPath: jailLogPath[1], @@ -121,7 +122,7 @@ func (r *Fail2banController) List(ctx http.Context) http.Response { if startIndex > len(jails) { return controllers.Success(ctx, http.Json{ "total": 0, - "items": []Fail2banJail{}, + "items": []types.Fail2banJail{}, }) } if endIndex > len(jails) { @@ -129,7 +130,7 @@ func (r *Fail2banController) List(ctx http.Context) http.Response { } pagedJails := jails[startIndex:endIndex] if pagedJails == nil { - pagedJails = []Fail2banJail{} + pagedJails = []types.Fail2banJail{} } return controllers.Success(ctx, http.Json{ diff --git a/app/http/controllers/plugins/postgresql15_controller.go b/app/http/controllers/plugins/postgresql15_controller.go index 2503db66..66c49463 100644 --- a/app/http/controllers/plugins/postgresql15_controller.go +++ b/app/http/controllers/plugins/postgresql15_controller.go @@ -11,6 +11,7 @@ import ( "panel/internal" "panel/internal/services" "panel/pkg/tools" + "panel/types" ) type Postgresql15Controller struct { @@ -152,7 +153,7 @@ func (r *Postgresql15Controller) Load(ctx http.Context) http.Response { return controllers.Error(ctx, http.StatusInternalServerError, "获取PostgreSQL空间占用失败") } - data := []LoadInfo{ + data := []types.NV{ {"启动时间", carbon.Parse(time).ToDateTimeString()}, {"进程 PID", pid}, {"进程数", process}, diff --git a/app/http/controllers/plugins/postgresql16_controller.go b/app/http/controllers/plugins/postgresql16_controller.go index 0978ab20..1182844d 100644 --- a/app/http/controllers/plugins/postgresql16_controller.go +++ b/app/http/controllers/plugins/postgresql16_controller.go @@ -11,6 +11,7 @@ import ( "panel/internal" "panel/internal/services" "panel/pkg/tools" + "panel/types" ) type Postgresql16Controller struct { @@ -152,7 +153,7 @@ func (r *Postgresql16Controller) Load(ctx http.Context) http.Response { return controllers.Error(ctx, http.StatusInternalServerError, "获取PostgreSQL空间占用失败") } - data := []LoadInfo{ + data := []types.NV{ {"启动时间", carbon.Parse(time).ToDateTimeString()}, {"进程 PID", pid}, {"进程数", process}, diff --git a/app/http/controllers/plugins/redis_controller.go b/app/http/controllers/plugins/redis_controller.go index c678c06a..af33a936 100644 --- a/app/http/controllers/plugins/redis_controller.go +++ b/app/http/controllers/plugins/redis_controller.go @@ -7,6 +7,7 @@ import ( "panel/app/http/controllers" "panel/pkg/tools" + "panel/types" ) type RedisController struct { @@ -103,7 +104,7 @@ func (r *RedisController) Load(ctx http.Context) http.Response { } } - data := []LoadInfo{ + data := []types.NV{ {"TCP 端口", dataRaw["tcp_port"]}, {"已运行天数", dataRaw["uptime_in_days"]}, {"连接的客户端数", dataRaw["connected_clients"]}, diff --git a/app/http/controllers/plugins/rsync_controller.go b/app/http/controllers/plugins/rsync_controller.go index 5d074e49..d8abce33 100644 --- a/app/http/controllers/plugins/rsync_controller.go +++ b/app/http/controllers/plugins/rsync_controller.go @@ -12,6 +12,7 @@ import ( "panel/internal" "panel/internal/services" "panel/pkg/tools" + "panel/types" ) type RsyncController struct { @@ -119,9 +120,9 @@ func (r *RsyncController) List(ctx http.Context) http.Response { return controllers.Error(ctx, http.StatusInternalServerError, err.Error()) } - var modules []RsyncModule + var modules []types.RsyncModule lines := strings.Split(config, "\n") - var currentModule *RsyncModule + var currentModule *types.RsyncModule for _, line := range lines { line = strings.TrimSpace(line) @@ -135,7 +136,7 @@ func (r *RsyncController) List(ctx http.Context) http.Response { modules = append(modules, *currentModule) } moduleName := line[1 : len(line)-1] - currentModule = &RsyncModule{ + currentModule = &types.RsyncModule{ Name: moduleName, } } else if currentModule != nil { @@ -173,7 +174,7 @@ func (r *RsyncController) List(ctx http.Context) http.Response { if startIndex > len(modules) { return controllers.Success(ctx, http.Json{ "total": 0, - "items": []RsyncModule{}, + "items": []types.RsyncModule{}, }) } if endIndex > len(modules) { @@ -181,7 +182,7 @@ func (r *RsyncController) List(ctx http.Context) http.Response { } pagedModules := modules[startIndex:endIndex] if pagedModules == nil { - pagedModules = []RsyncModule{} + pagedModules = []types.RsyncModule{} } return controllers.Success(ctx, http.Json{ diff --git a/app/http/controllers/plugins/s3fs_controller.go b/app/http/controllers/plugins/s3fs_controller.go index 56477cfc..32675570 100644 --- a/app/http/controllers/plugins/s3fs_controller.go +++ b/app/http/controllers/plugins/s3fs_controller.go @@ -12,6 +12,7 @@ import ( "panel/internal" "panel/internal/services" "panel/pkg/tools" + "panel/types" ) type S3fsController struct { @@ -29,7 +30,7 @@ func (r *S3fsController) List(ctx http.Context) http.Response { page := ctx.Request().QueryInt("page", 1) limit := ctx.Request().QueryInt("limit", 10) - var s3fsList []S3fsMount + var s3fsList []types.S3fsMount err := json.UnmarshalString(r.setting.Get("s3fs", "[]"), &s3fsList) if err != nil { return controllers.Error(ctx, http.StatusInternalServerError, "获取 S3fs 挂载失败") @@ -40,7 +41,7 @@ func (r *S3fsController) List(ctx http.Context) http.Response { if startIndex > len(s3fsList) { return controllers.Success(ctx, http.Json{ "total": 0, - "items": []S3fsMount{}, + "items": []types.S3fsMount{}, }) } if endIndex > len(s3fsList) { @@ -48,7 +49,7 @@ func (r *S3fsController) List(ctx http.Context) http.Response { } pagedS3fsList := s3fsList[startIndex:endIndex] if pagedS3fsList == nil { - pagedS3fsList = []S3fsMount{} + pagedS3fsList = []types.S3fsMount{} } return controllers.Success(ctx, http.Json{ @@ -94,7 +95,7 @@ func (r *S3fsController) Add(ctx http.Context) http.Response { return controllers.Error(ctx, http.StatusUnprocessableEntity, "挂载目录必须为空") } - var s3fsList []S3fsMount + var s3fsList []types.S3fsMount err = json.UnmarshalString(r.setting.Get("s3fs", "[]"), &s3fsList) if err != nil { return controllers.Error(ctx, http.StatusInternalServerError, "获取 S3fs 挂载失败") @@ -124,7 +125,7 @@ func (r *S3fsController) Add(ctx http.Context) http.Response { return controllers.Error(ctx, http.StatusInternalServerError, "挂载失败,请检查配置是否正确") } - s3fsList = append(s3fsList, S3fsMount{ + s3fsList = append(s3fsList, types.S3fsMount{ ID: id, Path: path, Bucket: bucket, @@ -149,13 +150,13 @@ func (r *S3fsController) Delete(ctx http.Context) http.Response { return controllers.Error(ctx, http.StatusUnprocessableEntity, "挂载ID不能为空") } - var s3fsList []S3fsMount + var s3fsList []types.S3fsMount err := json.UnmarshalString(r.setting.Get("s3fs", "[]"), &s3fsList) if err != nil { return controllers.Error(ctx, http.StatusInternalServerError, "获取 S3fs 挂载失败") } - var mount S3fsMount + var mount types.S3fsMount for _, s := range s3fsList { if cast.ToString(s.ID) == id { mount = s @@ -182,7 +183,7 @@ func (r *S3fsController) Delete(ctx http.Context) http.Response { return controllers.Error(ctx, http.StatusInternalServerError, err.Error()) } - var newS3fsList []S3fsMount + var newS3fsList []types.S3fsMount for _, s := range s3fsList { if s.ID != mount.ID { newS3fsList = append(newS3fsList, s) diff --git a/internal/constants.go b/internal/constants.go deleted file mode 100644 index afaee5ef..00000000 --- a/internal/constants.go +++ /dev/null @@ -1,11 +0,0 @@ -package internal - -type NV struct { - Name string `json:"name"` - Value string `json:"value"` -} - -type KV struct { - Key string `json:"key"` - Value string `json:"value"` -} diff --git a/types/fail2ban.go b/types/fail2ban.go new file mode 100644 index 00000000..248e47f4 --- /dev/null +++ b/types/fail2ban.go @@ -0,0 +1,10 @@ +package types + +type Fail2banJail struct { + Name string `json:"name"` + Enabled bool `json:"enabled"` + LogPath string `json:"log_path"` + MaxRetry int `json:"max_retry"` + FindTime int `json:"find_time"` + BanTime int `json:"ban_time"` +} diff --git a/types/rsync.go b/types/rsync.go new file mode 100644 index 00000000..0acb339c --- /dev/null +++ b/types/rsync.go @@ -0,0 +1,11 @@ +package types + +type RsyncModule struct { + Name string `json:"name"` + Path string `json:"path"` + Comment string `json:"comment"` + ReadOnly bool `json:"read_only"` + AuthUser string `json:"auth_user"` + Secret string `json:"secret"` + HostsAllow string `json:"hosts_allow"` +} diff --git a/types/s3fs.go b/types/s3fs.go new file mode 100644 index 00000000..f9ef0753 --- /dev/null +++ b/types/s3fs.go @@ -0,0 +1,8 @@ +package types + +type S3fsMount struct { + ID int64 `json:"id"` + Path string `json:"path"` + Bucket string `json:"bucket"` + Url string `json:"url"` +}