Files
2026-08-17 13:19:15 +08:00

85 lines
2.5 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package main
import (
"context"
"net/http"
"os"
"os/signal"
"path/filepath"
"strings"
"syscall"
"time"
commonHttp "slogan-agent/common"
"slogan-agent/styleagent/controller"
"slogan-agent/styleagent/service"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/net/ghttp"
_ "github.com/gogf/gf/contrib/drivers/sqlite/v2"
)
func main() {
// ==================== Web 静态资源(uni-app H5 构建产物) ====================
// 静态目录不存在时优雅跳过(仅 API 模式运行);注册顺序在 Auth 之后,history 回退先经鉴权放行
if dir := commonHttp.WebStaticDir(); dir != "" {
commonHttp.Httpserver.SetServerRoot(dir)
commonHttp.Httpserver.BindMiddlewareDefault(func(r *ghttp.Request) {
r.Middleware.Next()
if r.Response.Status == http.StatusNotFound && commonHttp.IsWebHistoryRoute(r.URL.Path) {
r.Response.ClearBuffer()
r.Response.ServeFile(filepath.Join(dir, "index.html"))
}
})
}
// ==================== API 路由(RouteRegister 反射注册,kebab-case 前缀) ====================
commonHttp.RouteRegister([]interface{}{
controller.User,
controller.UserPhoto,
controller.Wardrobe,
controller.BodyMeasurement,
controller.Avatar,
controller.Hairstyle,
controller.Outfit,
controller.PartnerStore,
controller.Member,
controller.Ad,
controller.Cps,
})
// 虎皮棋支付回调:经 RouteRegister 由 dto g.Meta 注册(/member/order/notify
// Auth 白名单放行;裸文本 "success" 由 controller 直接写响应体,不走统一 JSON 包装)
// ==================== Workspace 文件服务(鉴权保护) ====================
commonHttp.Httpserver.BindHandler("/workspace/*", func(r *ghttp.Request) {
relPath := strings.TrimPrefix(r.URL.Path, "/workspace/")
if relPath == "" || strings.Contains(relPath, "..") {
r.Response.WriteStatus(http.StatusForbidden)
return
}
filePath := filepath.Join("workspace", relPath)
if _, err := os.Stat(filePath); os.IsNotExist(err) {
r.Response.WriteStatus(http.StatusNotFound)
return
}
r.Response.ServeFile(filePath)
})
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
defer stop()
// 恢复未完成的生成任务(重启后标记失败,避免重复消耗 LLM 费用)
service.OutfitService.StartWorker(ctx)
// CPS 联盟商品定时同步(未配置 key 时空转)
service.CpsProductService.StartSyncLoop(ctx)
<-ctx.Done()
g.Log().Info(ctx, "shutting down...")
time.Sleep(1 * time.Second)
g.Log().Info(ctx, "bye")
}