Skip to content

Commit

Permalink
feat: 移除部分软件服务接口
Browse files Browse the repository at this point in the history
  • Loading branch information
devhaozi committed Jun 14, 2024
1 parent 6ba7d01 commit 10145da
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 172 deletions.
50 changes: 0 additions & 50 deletions app/http/controllers/plugins/fail2ban_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,56 +26,6 @@ func NewFail2banController() *Fail2banController {
}
}

// Status 获取运行状态
func (r *Fail2banController) Status(ctx http.Context) http.Response {
status, err := tools.ServiceStatus("fail2ban")
if err != nil {
return controllers.Error(ctx, http.StatusInternalServerError, "获取服务运行状态失败")
}

return controllers.Success(ctx, status)
}

// Reload 重载配置
func (r *Fail2banController) Reload(ctx http.Context) http.Response {
if err := tools.ServiceReload("fail2ban"); err != nil {
return controllers.Error(ctx, http.StatusInternalServerError, "重载配置失败")
}

return controllers.Success(ctx, nil)
}

// Restart 重启服务
func (r *Fail2banController) Restart(ctx http.Context) http.Response {
if err := tools.ServiceRestart("fail2ban"); err != nil {
return controllers.Error(ctx, http.StatusInternalServerError, "重启服务失败")
}

return controllers.Success(ctx, nil)
}

// Start 启动服务
func (r *Fail2banController) Start(ctx http.Context) http.Response {
if err := tools.ServiceStart("fail2ban"); err != nil {
return controllers.Error(ctx, http.StatusInternalServerError, "启动服务失败")
}

return controllers.Success(ctx, nil)
}

// Stop 停止服务
func (r *Fail2banController) Stop(ctx http.Context) http.Response {
if err := tools.ServiceStop("fail2ban"); err != nil {
return nil
}
status, err := tools.ServiceStatus("fail2ban")
if err != nil {
return controllers.Error(ctx, http.StatusInternalServerError, "获取服务运行状态失败")
}

return controllers.Success(ctx, !status)
}

// List 所有 Fail2ban 规则
func (r *Fail2banController) List(ctx http.Context) http.Response {
page := ctx.Request().QueryInt("page", 1)
Expand Down
52 changes: 5 additions & 47 deletions app/http/controllers/plugins/mysql_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,52 +28,6 @@ func NewMySQLController() *MySQLController {
}
}

// Status 获取运行状态
func (r *MySQLController) Status(ctx http.Context) http.Response {
status, err := tools.ServiceStatus("mysqld")
if err != nil {
return controllers.Error(ctx, http.StatusInternalServerError, "获取MySQL状态失败")
}

return controllers.Success(ctx, status)
}

// Reload 重载配置
func (r *MySQLController) Reload(ctx http.Context) http.Response {
if err := tools.ServiceReload("mysqld"); err != nil {
return controllers.Error(ctx, http.StatusInternalServerError, "重载MySQL配置失败")
}

return controllers.Success(ctx, nil)
}

// Restart 重启服务
func (r *MySQLController) Restart(ctx http.Context) http.Response {
if err := tools.ServiceRestart("mysqld"); err != nil {
return controllers.Error(ctx, http.StatusInternalServerError, "重启MySQL服务失败")
}

return controllers.Success(ctx, nil)
}

// Start 启动服务
func (r *MySQLController) Start(ctx http.Context) http.Response {
if err := tools.ServiceStart("mysqld"); err != nil {
return controllers.Error(ctx, http.StatusInternalServerError, "启动MySQL服务失败")
}

return controllers.Success(ctx, nil)
}

// Stop 停止服务
func (r *MySQLController) Stop(ctx http.Context) http.Response {
if err := tools.ServiceStop("mysqld"); err != nil {
return controllers.Error(ctx, http.StatusInternalServerError, "停止MySQL服务失败")
}

return controllers.Success(ctx, nil)
}

// GetConfig 获取配置
func (r *MySQLController) GetConfig(ctx http.Context) http.Response {
config, err := tools.Read("/www/server/mysql/conf/my.cnf")
Expand All @@ -95,7 +49,11 @@ func (r *MySQLController) SaveConfig(ctx http.Context) http.Response {
return controllers.Error(ctx, http.StatusInternalServerError, "写入MySQL配置失败")
}

return r.Restart(ctx)
if err := tools.ServiceReload("mysqld"); err != nil {
return controllers.Error(ctx, http.StatusInternalServerError, "重载MySQL失败")
}

return controllers.Success(ctx, nil)
}

// Load 获取负载
Expand Down
64 changes: 14 additions & 50 deletions app/http/controllers/plugins/supervisor_controller.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package plugins

import (
"fmt"
"strconv"
"strings"

Expand All @@ -11,66 +12,25 @@ import (
)

type SupervisorController struct {
ServiceName string
service string
}

func NewSupervisorController() *SupervisorController {
var serviceName string
var service string
if tools.IsRHEL() {
serviceName = "supervisord"
service = "supervisord"
} else {
serviceName = "supervisor"
service = "supervisor"
}

return &SupervisorController{
ServiceName: serviceName,
service: service,
}
}

// Status 状态
func (r *SupervisorController) Status(ctx http.Context) http.Response {
status, err := tools.ServiceStatus(r.ServiceName)
if err != nil {
return controllers.Error(ctx, http.StatusInternalServerError, "获取Supervisor状态失败")
}

return controllers.Success(ctx, status)
}

// Start 启动
func (r *SupervisorController) Start(ctx http.Context) http.Response {
if err := tools.ServiceStart(r.ServiceName); err != nil {
return controllers.Error(ctx, http.StatusInternalServerError, "启动Supervisor失败")
}

return controllers.Success(ctx, nil)
}

// Stop 停止
func (r *SupervisorController) Stop(ctx http.Context) http.Response {
if err := tools.ServiceStop(r.ServiceName); err != nil {
return controllers.Error(ctx, http.StatusInternalServerError, "停止Supervisor失败")
}

return controllers.Success(ctx, nil)
}

// Restart 重启
func (r *SupervisorController) Restart(ctx http.Context) http.Response {
if err := tools.ServiceRestart(r.ServiceName); err != nil {
return controllers.Error(ctx, http.StatusInternalServerError, "重启Supervisor失败")
}

return controllers.Success(ctx, nil)
}

// Reload 重载
func (r *SupervisorController) Reload(ctx http.Context) http.Response {
if err := tools.ServiceReload(r.ServiceName); err != nil {
return controllers.Error(ctx, http.StatusInternalServerError, "重载Supervisor失败")
}

return controllers.Success(ctx, nil)
// Service 获取服务名称
func (r *SupervisorController) Service(ctx http.Context) http.Response {
return controllers.Success(ctx, r.service)
}

// Log 日志
Expand Down Expand Up @@ -123,7 +83,11 @@ func (r *SupervisorController) SaveConfig(ctx http.Context) http.Response {
return controllers.Error(ctx, http.StatusInternalServerError, err.Error())
}

return r.Restart(ctx)
if err = tools.ServiceRestart(r.service); err != nil {
return controllers.Error(ctx, http.StatusInternalServerError, fmt.Sprintf("重启 %s 服务失败", r.service))
}

return controllers.Success(ctx, nil)
}

// Processes 进程列表
Expand Down
26 changes: 1 addition & 25 deletions routes/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,6 @@ func Plugin() {
})
r.Prefix("mysql57").Group(func(route route.Router) {
mySQLController := plugins.NewMySQLController()
route.Get("status", mySQLController.Status)
route.Post("reload", mySQLController.Reload)
route.Post("start", mySQLController.Start)
route.Post("stop", mySQLController.Stop)
route.Post("restart", mySQLController.Restart)
route.Get("load", mySQLController.Load)
route.Get("config", mySQLController.GetConfig)
route.Post("config", mySQLController.SaveConfig)
Expand All @@ -56,11 +51,6 @@ func Plugin() {
})
r.Prefix("mysql80").Group(func(route route.Router) {
mySQLController := plugins.NewMySQLController()
route.Get("status", mySQLController.Status)
route.Post("reload", mySQLController.Reload)
route.Post("start", mySQLController.Start)
route.Post("stop", mySQLController.Stop)
route.Post("restart", mySQLController.Restart)
route.Get("load", mySQLController.Load)
route.Get("config", mySQLController.GetConfig)
route.Post("config", mySQLController.SaveConfig)
Expand All @@ -86,11 +76,6 @@ func Plugin() {
})
r.Prefix("mysql84").Group(func(route route.Router) {
mySQLController := plugins.NewMySQLController()
route.Get("status", mySQLController.Status)
route.Post("reload", mySQLController.Reload)
route.Post("start", mySQLController.Start)
route.Post("stop", mySQLController.Stop)
route.Post("restart", mySQLController.Restart)
route.Get("load", mySQLController.Load)
route.Get("config", mySQLController.GetConfig)
route.Post("config", mySQLController.SaveConfig)
Expand Down Expand Up @@ -304,11 +289,7 @@ func Plugin() {
})
r.Prefix("supervisor").Group(func(route route.Router) {
supervisorController := plugins.NewSupervisorController()
route.Get("status", supervisorController.Status)
route.Post("start", supervisorController.Start)
route.Post("stop", supervisorController.Stop)
route.Post("restart", supervisorController.Restart)
route.Post("reload", supervisorController.Reload)
route.Get("service", supervisorController.Service)
route.Get("log", supervisorController.Log)
route.Post("clearLog", supervisorController.ClearLog)
route.Get("config", supervisorController.Config)
Expand All @@ -327,11 +308,6 @@ func Plugin() {
})
r.Prefix("fail2ban").Group(func(route route.Router) {
fail2banController := plugins.NewFail2banController()
route.Get("status", fail2banController.Status)
route.Post("start", fail2banController.Start)
route.Post("stop", fail2banController.Stop)
route.Post("restart", fail2banController.Restart)
route.Post("reload", fail2banController.Reload)
route.Get("jails", fail2banController.List)
route.Post("jails", fail2banController.Add)
route.Delete("jails", fail2banController.Delete)
Expand Down

0 comments on commit 10145da

Please sign in to comment.