Skip to content

Commit

Permalink
feat: 添加Gitea
Browse files Browse the repository at this point in the history
  • Loading branch information
devhaozi committed Jun 8, 2024
1 parent f7ba602 commit 77aa4be
Show file tree
Hide file tree
Showing 12 changed files with 511 additions and 7 deletions.
5 changes: 3 additions & 2 deletions app/console/commands/monitoring.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,9 @@ func (receiver *Monitoring) Handle(console.Context) error {
}

// 将等待中的任务分发
task := services.NewTaskImpl()
_ = task.DispatchWaiting()
// TODO 有bug,需要设计一个锁机制防止重复分发
//task := services.NewTaskImpl()
//_ = task.DispatchWaiting()

setting := services.NewSettingImpl()
monitor := setting.Get(models.SettingKeyMonitor)
Expand Down
193 changes: 193 additions & 0 deletions app/http/controllers/plugins/gitea_controller.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
package plugins

import (
"github.com/goravel/framework/contracts/http"

"github.com/TheTNB/panel/app/http/controllers"
requests "github.com/TheTNB/panel/app/http/requests/plugins/gitea"
"github.com/TheTNB/panel/pkg/tools"
)

type GiteaController struct {
}

func NewGiteaController() *GiteaController {
return &GiteaController{}
}

// Status
//
// @Summary 服务状态
// @Description 获取 Gitea 服务状态
// @Tags 插件-Gitea
// @Produce json
// @Security BearerToken
// @Success 200 {object} controllers.SuccessResponse
// @Router /plugins/gitea/status [get]
func (r *GiteaController) Status(ctx http.Context) http.Response {
status, err := tools.ServiceStatus("gitea")
if err != nil {
return controllers.Error(ctx, http.StatusInternalServerError, "获取 Gitea 服务运行状态失败")
}

return controllers.Success(ctx, status)
}

// IsEnabled
//
// @Summary 是否启用服务
// @Description 获取是否启用 Gitea 服务
// @Tags 插件-Gitea
// @Produce json
// @Security BearerToken
// @Success 200 {object} controllers.SuccessResponse
// @Router /plugins/gitea/isEnabled [get]
func (r *GiteaController) IsEnabled(ctx http.Context) http.Response {
enabled, err := tools.ServiceIsEnabled("gitea")
if err != nil {
return controllers.Error(ctx, http.StatusInternalServerError, "获取 Gitea 服务启用状态失败")
}

return controllers.Success(ctx, enabled)
}

// Enable
//
// @Summary 启用服务
// @Description 启用 Gitea 服务
// @Tags 插件-Gitea
// @Produce json
// @Security BearerToken
// @Success 200 {object} controllers.SuccessResponse
// @Router /plugins/gitea/enable [post]
func (r *GiteaController) Enable(ctx http.Context) http.Response {
if err := tools.ServiceEnable("gitea"); err != nil {
return controllers.Error(ctx, http.StatusInternalServerError, "启用 Gitea 服务失败")
}

return controllers.Success(ctx, nil)
}

// Disable
//
// @Summary 禁用服务
// @Description 禁用 Gitea 服务
// @Tags 插件-Gitea
// @Produce json
// @Security BearerToken
// @Success 200 {object} controllers.SuccessResponse
// @Router /plugins/gitea/disable [post]
func (r *GiteaController) Disable(ctx http.Context) http.Response {
if err := tools.ServiceDisable("gitea"); err != nil {
return controllers.Error(ctx, http.StatusInternalServerError, "禁用 Gitea 服务失败")
}

return controllers.Success(ctx, nil)
}

// Restart
//
// @Summary 重启服务
// @Description 重启 Gitea 服务
// @Tags 插件-Gitea
// @Produce json
// @Security BearerToken
// @Success 200 {object} controllers.SuccessResponse
// @Router /plugins/gitea/restart [post]
func (r *GiteaController) Restart(ctx http.Context) http.Response {
if err := tools.ServiceRestart("gitea"); err != nil {
return controllers.Error(ctx, http.StatusInternalServerError, "重启 Gitea 服务失败")
}

return controllers.Success(ctx, nil)
}

// Start
//
// @Summary 启动服务
// @Description 启动 Gitea 服务
// @Tags 插件-Gitea
// @Produce json
// @Security BearerToken
// @Success 200 {object} controllers.SuccessResponse
// @Router /plugins/gitea/start [post]
func (r *GiteaController) Start(ctx http.Context) http.Response {
if err := tools.ServiceStart("gitea"); err != nil {
return controllers.Error(ctx, http.StatusInternalServerError, "启动 Gitea 服务失败")
}

status, err := tools.ServiceStatus("gitea")
if err != nil {
return controllers.Error(ctx, http.StatusInternalServerError, "获取 Gitea 服务运行状态失败")
}

return controllers.Success(ctx, status)
}

// Stop
//
// @Summary 停止服务
// @Description 停止 Gitea 服务
// @Tags 插件-Gitea
// @Produce json
// @Security BearerToken
// @Success 200 {object} controllers.SuccessResponse
// @Router /plugins/gitea/stop [post]
func (r *GiteaController) Stop(ctx http.Context) http.Response {
if err := tools.ServiceStop("gitea"); err != nil {
return controllers.Error(ctx, http.StatusInternalServerError, "停止 Gitea 服务失败")
}

status, err := tools.ServiceStatus("gitea")
if err != nil {
return controllers.Error(ctx, http.StatusInternalServerError, "获取 Gitea 服务运行状态失败")
}

return controllers.Success(ctx, !status)
}

// GetConfig
//
// @Summary 获取配置
// @Description 获取 Gitea 配置
// @Tags 插件-Gitea
// @Produce json
// @Security BearerToken
// @Success 200 {object} controllers.SuccessResponse
// @Router /plugins/gitea/config [get]
func (r *GiteaController) GetConfig(ctx http.Context) http.Response {
config, err := tools.Read("/www/server/gitea/app.ini")
if err != nil {
return controllers.Error(ctx, http.StatusInternalServerError, err.Error())
}

return controllers.Success(ctx, config)
}

// UpdateConfig
//
// @Summary 更新配置
// @Description 更新 Gitea 配置
// @Tags 插件-Gitea
// @Produce json
// @Security BearerToken
// @Param data body requests.UpdateConfig true "request"
// @Success 200 {object} controllers.SuccessResponse
// @Router /plugins/gitea/config [post]
func (r *GiteaController) UpdateConfig(ctx http.Context) http.Response {
var updateRequest requests.UpdateConfig
sanitize := controllers.Sanitize(ctx, &updateRequest)
if sanitize != nil {
return sanitize
}

if err := tools.Write("/www/server/gitea/app.ini", updateRequest.Config, 0644); err != nil {
return controllers.Error(ctx, http.StatusInternalServerError, err.Error())
}

if err := tools.ServiceRestart("gitea"); err != nil {
return controllers.Error(ctx, http.StatusInternalServerError, err.Error())
}

return controllers.Success(ctx, nil)
}
32 changes: 32 additions & 0 deletions app/http/requests/plugins/gitea/update_config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package requests

import (
"github.com/goravel/framework/contracts/http"
"github.com/goravel/framework/contracts/validation"
)

type UpdateConfig struct {
Config string `form:"config" json:"config"`
}

func (r *UpdateConfig) Authorize(ctx http.Context) error {
return nil
}

func (r *UpdateConfig) Rules(ctx http.Context) map[string]string {
return map[string]string{
"config": "required|string",
}
}

func (r *UpdateConfig) Messages(ctx http.Context) map[string]string {
return map[string]string{}
}

func (r *UpdateConfig) Attributes(ctx http.Context) map[string]string {
return map[string]string{}
}

func (r *UpdateConfig) PrepareForValidation(ctx http.Context, data validation.Data) error {
return nil
}
1 change: 1 addition & 0 deletions internal/services/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ func (r *PluginImpl) All() []types.Plugin {
types.PluginSupervisor,
types.PluginFail2ban,
types.PluginFrp,
types.PluginGitea,
types.PluginToolBox,
}

Expand Down
12 changes: 12 additions & 0 deletions routes/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,18 @@ func Plugin() {
route.Get("config", frpController.GetConfig)
route.Post("config", frpController.UpdateConfig)
})
r.Prefix("gitea").Group(func(route route.Router) {
giteaController := plugins.NewGiteaController()
route.Get("status", giteaController.Status)
route.Get("isEnabled", giteaController.IsEnabled)
route.Post("enable", giteaController.Enable)
route.Post("disable", giteaController.Disable)
route.Post("start", giteaController.Start)
route.Post("stop", giteaController.Stop)
route.Post("restart", giteaController.Restart)
route.Get("config", giteaController.GetConfig)
route.Post("config", giteaController.UpdateConfig)
})
r.Prefix("toolbox").Group(func(route route.Router) {
toolboxController := plugins.NewToolBoxController()
route.Get("dns", toolboxController.GetDNS)
Expand Down
5 changes: 4 additions & 1 deletion scripts/frp/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,7 @@ systemctl start frps
systemctl start frpc

panel writePlugin frp ${frpVersion}
echo -e "${HR}\nfrp 安装完成\n${HR}"
echo -e ${HR}
echo "frp 安装完成"
echo -e ${HR}

4 changes: 2 additions & 2 deletions scripts/frp/uninstall.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
'

HR="+----------------------------------------------------"
frpPath="/usr/local/frp"
frpPath="/www/server/frp"

systemctl stop frps
systemctl stop frpc
Expand All @@ -34,4 +34,4 @@ systemctl daemon-reload
panel deletePlugin frp
echo -e $HR
echo "frp 卸载完成"
echo -e $HR
echo -e $HR
Loading

0 comments on commit 77aa4be

Please sign in to comment.