Files
gen2d/backend/internal/handler/storage.go
T
wonder 25c11ed649 refactor: 全面集成 slog 日志到 handler、service、middleware
- cmd/main.go: 集成 logger.Init 和日志中间件,替换标准 log 包
- handler 层: 5xx 错误记录完整日志,返回通用消息(防内部信息泄露)
- service 层: LLM/图片生成/存储/认证等关键操作补充结构化日志
- auth 中间件: 记录认证失败原因
- generate.go: 后台管线任务使用带 task_id 的 logger
2026-05-25 14:31:04 +08:00

38 lines
991 B
Go

package handler
import (
"net/http"
"gen2d/internal/logger"
"gen2d/internal/model"
"gen2d/internal/service"
"github.com/gin-gonic/gin"
)
var storageSvc *service.StorageService
// InitStorageService 由 main 在启动时调用,注入七牛云存储服务。
func InitStorageService(svc *service.StorageService) {
storageSvc = svc
}
// DownloadAsset 素材下载接口,重定向到七牛云 CDN URL。
// GET /api/v1/assets/download?key=...
func DownloadAsset(c *gin.Context) {
key := c.Query("key")
if key == "" {
c.JSON(http.StatusBadRequest, model.Fail(http.StatusBadRequest, "缺少 key 参数"))
return
}
downloadURL, err := storageSvc.GetDownloadURL(c.Request.Context(), key)
if err != nil {
logger.FromCtx(c.Request.Context()).Error("生成下载链接失败", "key", key, "error", err)
c.JSON(http.StatusInternalServerError, model.Fail(http.StatusInternalServerError, "生成下载链接失败"))
return
}
c.Redirect(http.StatusFound, downloadURL)
}