feat: add OpenAPI support for task creation

This commit is contained in:
duorameng
2026-06-01 17:18:38 +08:00
parent f954b3946a
commit 1863ed58cf
3 changed files with 64 additions and 47 deletions
+14 -47
View File
@@ -56,30 +56,19 @@ func resolveWorkDir(workDir string) string {
}
return absPath
}
// CreateTask 创建任务
// @Summary 创建任务
// @Description 创建一个新的任务
// @Tags 任务管理
// @Accept json
// @Produce json
// @Security BearerAuth
// @Param body body vo.TaskCreateReq true "任务创建信息"
// @Success 200 {object} utils.Response{data=vo.TaskVO}
// @Failure 400 {object} utils.Response
// @Router /tasks [post]
func (tc *TaskController) CreateTask(c *gin.Context) {
var req struct {
Name string `json:"name" binding:"required"`
Remark string `json:"remark"`
Command string `json:"command"`
PreCommand string `json:"pre_command"`
PostCommand string `json:"post_command"`
Tags string `json:"tags"`
Type string `json:"type"`
Config string `json:"config"`
Schedule string `json:"schedule"`
Timeout int `json:"timeout"`
WorkDir string `json:"work_dir"`
CleanConfig string `json:"clean_config"`
Envs string `json:"envs"`
Languages models.TaskLanguages `json:"languages"`
AgentID *string `json:"agent_id"`
TriggerType string `json:"trigger_type"`
RetryCount int `json:"retry_count"`
RetryInterval int `json:"retry_interval"`
RandomRange int `json:"random_range"`
PinType string `json:"pin_type"`
}
var req vo.TaskCreateReq
if err := c.ShouldBindJSON(&req); err != nil {
utils.BadRequest(c, err.Error())
@@ -232,7 +221,7 @@ func (tc *TaskController) GetTask(c *gin.Context) {
// @Produce json
// @Security BearerAuth
// @Param id path string true "任务ID"
// @Param body body object true "任务更新信息"
// @Param body body vo.TaskUpdateReq true "任务更新信息"
// @Success 200 {object} utils.Response{data=vo.TaskVO}
// @Failure 404 {object} utils.Response
// @Router /tasks/{id} [put]
@@ -250,29 +239,7 @@ func (tc *TaskController) UpdateTask(c *gin.Context) {
oldAgentID = oldTask.AgentID
}
var req struct {
Name string `json:"name"`
Remark string `json:"remark"`
Command string `json:"command"`
PreCommand string `json:"pre_command"`
PostCommand string `json:"post_command"`
Tags string `json:"tags"`
Type string `json:"type"`
Config string `json:"config"`
Schedule string `json:"schedule"`
Timeout int `json:"timeout"`
WorkDir string `json:"work_dir"`
CleanConfig string `json:"clean_config"`
Envs string `json:"envs"`
Enabled bool `json:"enabled"`
Languages models.TaskLanguages `json:"languages"`
AgentID *string `json:"agent_id"`
TriggerType string `json:"trigger_type"`
RetryCount int `json:"retry_count"`
RetryInterval int `json:"retry_interval"`
RandomRange int `json:"random_range"`
PinType string `json:"pin_type"`
}
var req vo.TaskUpdateReq
if err := c.ShouldBindJSON(&req); err != nil {
utils.BadRequest(c, err.Error())
+49
View File
@@ -6,6 +6,55 @@ import (
"github.com/engigu/baihu-panel/internal/utils"
)
// TaskCreateReq 任务创建请求
type TaskCreateReq struct {
Name string `json:"name" binding:"required" example:"测试任务"`
Remark string `json:"remark" example:"备注信息"`
Command string `json:"command" example:"echo 'Hello World'"`
PreCommand string `json:"pre_command" example:"echo 'pre'"`
PostCommand string `json:"post_command" example:"echo 'post'"`
Tags string `json:"tags" example:"test,dev"`
Type string `json:"type" example:"repo"` // 可以是 common, repo 等
Config string `json:"config" swaggertype:"string" example:"{\"source_url\":\"https://github.com/abc/repo\",\"branch\":\"main\"}"`
Schedule string `json:"schedule" example:"0 0 * * *"`
Timeout int `json:"timeout" example:"3600"`
WorkDir string `json:"work_dir" example:"/tmp"`
CleanConfig string `json:"clean_config" example:"true"`
Envs string `json:"envs" example:"{\"ENV_VAR\":\"value\"}"`
Languages models.TaskLanguages `json:"languages"`
AgentID *string `json:"agent_id" example:"agent-1"`
TriggerType string `json:"trigger_type" example:"cron"`
RetryCount int `json:"retry_count" example:"3"`
RetryInterval int `json:"retry_interval" example:"60"`
RandomRange int `json:"random_range" example:"10"`
PinType string `json:"pin_type" example:"time"`
}
// TaskUpdateReq 任务更新请求
type TaskUpdateReq struct {
Name string `json:"name" example:"测试任务"`
Remark string `json:"remark" example:"备注信息"`
Command string `json:"command" example:"echo 'Hello World'"`
PreCommand string `json:"pre_command" example:"echo 'pre'"`
PostCommand string `json:"post_command" example:"echo 'post'"`
Tags string `json:"tags" example:"test,dev"`
Type string `json:"type" example:"repo"`
Config string `json:"config" swaggertype:"string" example:"{\"source_url\":\"https://github.com/abc/repo\",\"branch\":\"main\"}"`
Schedule string `json:"schedule" example:"0 0 * * *"`
Timeout int `json:"timeout" example:"3600"`
WorkDir string `json:"work_dir" example:"/tmp"`
CleanConfig string `json:"clean_config" example:"true"`
Envs string `json:"envs" example:"{\"ENV_VAR\":\"value\"}"`
Enabled bool `json:"enabled" example:"true"`
Languages models.TaskLanguages `json:"languages"`
AgentID *string `json:"agent_id" example:"agent-1"`
TriggerType string `json:"trigger_type" example:"cron"`
RetryCount int `json:"retry_count" example:"3"`
RetryInterval int `json:"retry_interval" example:"60"`
RandomRange int `json:"random_range" example:"10"`
PinType string `json:"pin_type" example:"time"`
}
// TaskVO 任务视图对象
type TaskVO struct {
ID string `json:"id"`
+1
View File
@@ -29,6 +29,7 @@ func initOpenAPIV1Routes(root *gin.RouterGroup, c *Controllers) {
func registerOpenAPITaskRoutes(g *gin.RouterGroup, c *Controllers) {
tasks := g.Group("/tasks")
{
tasks.POST("", c.Task.CreateTask)
tasks.GET("", c.Task.GetTasks)
tasks.GET("/:id", c.Task.GetTask)
tasks.PUT("/:id", c.Task.UpdateTask)