mirror of
https://github.com/keven1024/015.git
synced 2026-05-26 07:08:02 +00:00
feat(backend): implement task management endpoints for image compression and task retrieval
This commit is contained in:
@@ -1,40 +1,39 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"backend/internal/controllers/task"
|
||||
"backend/internal/utils"
|
||||
"backend/middleware"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"pkg/models"
|
||||
|
||||
u "pkg/utils"
|
||||
|
||||
"github.com/hibiken/asynq"
|
||||
"github.com/labstack/echo/v4"
|
||||
)
|
||||
|
||||
type GenCompressImageRequest struct {
|
||||
FileId string `json:"file_id"`
|
||||
var handleTaskMap = map[string]func(c *middleware.CustomContext) ([]byte, error){
|
||||
"image:compress": task.HandleImageCompress,
|
||||
}
|
||||
|
||||
func GenCompressImage(c echo.Context) error {
|
||||
func CreateTask(c echo.Context) error {
|
||||
cc := c.(*middleware.CustomContext)
|
||||
r := new(GenCompressImageRequest)
|
||||
if err := cc.Bind(r); err != nil {
|
||||
return utils.HTTPErrorHandler(c, err)
|
||||
}
|
||||
|
||||
if r.FileId == "" {
|
||||
taskType := cc.Param("type")
|
||||
if taskType == "" {
|
||||
return utils.HTTPErrorHandler(c, errors.New("调用接口参数错误"))
|
||||
}
|
||||
client := u.GetQueueClient()
|
||||
json, err := json.Marshal(map[string]any{
|
||||
"file_id": r.FileId,
|
||||
})
|
||||
handleTask, ok := handleTaskMap[taskType]
|
||||
if !ok {
|
||||
return utils.HTTPErrorHandler(c, errors.New("任务不存在"))
|
||||
}
|
||||
json, err := handleTask(cc)
|
||||
if err != nil {
|
||||
return utils.HTTPErrorHandler(c, err)
|
||||
}
|
||||
info, err := client.Enqueue(asynq.NewTask("image:compress", json), asynq.MaxRetry(3))
|
||||
|
||||
client := u.GetQueueClient()
|
||||
info, err := client.Enqueue(asynq.NewTask(taskType, json), asynq.MaxRetry(3))
|
||||
if err != nil {
|
||||
return utils.HTTPErrorHandler(c, err)
|
||||
}
|
||||
@@ -44,7 +43,7 @@ func GenCompressImage(c echo.Context) error {
|
||||
})
|
||||
}
|
||||
|
||||
func GetCompressImage(c echo.Context) error {
|
||||
func GetTask(c echo.Context) error {
|
||||
cc := c.(*middleware.CustomContext)
|
||||
taskId := cc.Param("id")
|
||||
if taskId == "" {
|
||||
32
backend/internal/controllers/task/image_compress.go
Normal file
32
backend/internal/controllers/task/image_compress.go
Normal file
@@ -0,0 +1,32 @@
|
||||
package task
|
||||
|
||||
import (
|
||||
"backend/middleware"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
)
|
||||
|
||||
type GenCompressImageRequest struct {
|
||||
FileId string `json:"file_id"`
|
||||
}
|
||||
|
||||
func HandleImageCompress(c *middleware.CustomContext) ([]byte, error) {
|
||||
|
||||
r := new(GenCompressImageRequest)
|
||||
if err := c.Bind(r); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if r.FileId == "" {
|
||||
return nil, errors.New("调用接口参数错误")
|
||||
}
|
||||
|
||||
json, err := json.Marshal(map[string]any{
|
||||
"file_id": r.FileId,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return json, nil
|
||||
}
|
||||
@@ -28,6 +28,6 @@ var routes = []Route{
|
||||
{Method: []string{"GET"}, Path: "/config", Handler: controllers.GetConfig},
|
||||
{Method: []string{"GET"}, Path: "/about", Handler: controllers.GetAbout},
|
||||
|
||||
{Method: []string{"POST"}, Path: "/image/compress", Handler: controllers.GenCompressImage},
|
||||
{Method: []string{"GET"}, Path: "/image/compress/:id", Handler: controllers.GetCompressImage},
|
||||
{Method: []string{"POST"}, Path: "/task/:type", Handler: controllers.CreateTask},
|
||||
{Method: []string{"GET"}, Path: "/task/:id", Handler: controllers.GetTask},
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user