2026-01-18 18:20:40 +08:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
|
|
import (
|
2026-01-18 19:07:41 +08:00
|
|
|
"TrangleAgent/internal/controller"
|
|
|
|
|
"TrangleAgent/internal/controller/websocket"
|
2026-01-18 18:20:40 +08:00
|
|
|
"context"
|
2026-01-18 19:07:41 +08:00
|
|
|
|
2026-01-18 18:20:40 +08:00
|
|
|
"github.com/gogf/gf/v2/frame/g"
|
|
|
|
|
"github.com/gogf/gf/v2/net/ghttp"
|
|
|
|
|
"github.com/gogf/gf/v2/os/gcmd"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var (
|
|
|
|
|
Main = gcmd.Command{
|
|
|
|
|
Name: "main",
|
|
|
|
|
Usage: "main",
|
|
|
|
|
Brief: "start http server",
|
|
|
|
|
Func: func(ctx context.Context, parser *gcmd.Parser) (err error) {
|
|
|
|
|
s := g.Server()
|
|
|
|
|
|
2026-01-18 19:07:41 +08:00
|
|
|
// CORS 中间件 - 排除 swagger 和 openapi 路径
|
2026-01-18 18:20:40 +08:00
|
|
|
s.Use(func(r *ghttp.Request) {
|
2026-01-18 19:07:41 +08:00
|
|
|
// 排除 swagger 相关路径
|
|
|
|
|
path := r.URL.Path
|
|
|
|
|
if path == "/swagger" || path == "/api.json" || path == "/swagger/" || path == "/api.json/" {
|
|
|
|
|
r.Middleware.Next()
|
|
|
|
|
return
|
|
|
|
|
}
|
2026-01-18 21:52:04 +08:00
|
|
|
|
2026-01-18 18:20:40 +08:00
|
|
|
r.Response.CORS(ghttp.CORSOptions{
|
|
|
|
|
AllowOrigin: "http://localhost:3000,http://localhost:8080",
|
|
|
|
|
AllowMethods: "GET,POST,PUT,DELETE,OPTIONS",
|
|
|
|
|
AllowHeaders: "Content-Type,Authorization,X-Requested-With",
|
|
|
|
|
AllowCredentials: "true",
|
|
|
|
|
MaxAge: 3600,
|
|
|
|
|
})
|
|
|
|
|
if r.Method == "OPTIONS" {
|
|
|
|
|
r.Response.WriteStatusExit(200)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
r.Middleware.Next()
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// 注册 API 路由组
|
|
|
|
|
s.Group("/api", func(group *ghttp.RouterGroup) {
|
|
|
|
|
group.Middleware(ghttp.MiddlewareHandlerResponse)
|
|
|
|
|
controller.RegisterControllers(group)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// 注册 WebSocket 聊天室路由(不在 /api 组下,因为 WebSocket 不需要中间件)
|
|
|
|
|
s.BindHandler("/ws/chat", websocket.HandleChatConnections)
|
|
|
|
|
|
2026-01-18 19:07:41 +08:00
|
|
|
s.SetDumpRouterMap(false)
|
2026-01-18 18:20:40 +08:00
|
|
|
|
|
|
|
|
s.Run()
|
|
|
|
|
return nil
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
)
|