chat/internal/handler/routes.go

113 lines
2.7 KiB
Go
Raw Normal View History

2025-12-31 09:36:02 +00:00
// Code generated by goctl. DO NOT EDIT.
// goctl 1.9.2
package handler
import (
"net/http"
"time"
chat "chat/internal/handler/chat"
"chat/internal/middleware"
"chat/internal/svc"
"github.com/zeromicro/go-zero/rest"
)
func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
// OPTIONS 處理器CORS 預檢請求)
optionsHandler := func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS")
w.Header().Set("Access-Control-Allow-Headers", "Content-Type, Authorization")
w.Header().Set("Access-Control-Max-Age", "3600")
w.WriteHeader(http.StatusNoContent)
}
server.AddRoutes(
rest.WithMiddlewares(
[]rest.Middleware{middleware.CORSMiddleware},
[]rest.Route{
{
// OPTIONS 支持
Method: http.MethodOptions,
Path: "/auth/anon",
Handler: optionsHandler,
},
{
// 匿名登入
Method: http.MethodPost,
Path: "/auth/anon",
Handler: chat.AnonLoginHandler(serverCtx),
},
{
// OPTIONS 支持
Method: http.MethodOptions,
Path: "/auth/refresh",
Handler: optionsHandler,
},
{
// 刷新 Token
Method: http.MethodPost,
Path: "/auth/refresh",
Handler: chat.RefreshTokenHandler(serverCtx),
},
}...,
),
rest.WithPrefix("/api/v1"),
rest.WithTimeout(10000*time.Millisecond),
)
server.AddRoutes(
rest.WithMiddlewares(
[]rest.Middleware{middleware.CORSMiddleware, serverCtx.AnonMiddleware},
[]rest.Route{
{
// OPTIONS 支持
Method: http.MethodOptions,
Path: "/matchmaking/join",
Handler: optionsHandler,
},
{
// 加入等待序列
Method: http.MethodPost,
Path: "/matchmaking/join",
Handler: chat.MatchJoinHandler(serverCtx),
},
{
// OPTIONS 支持
Method: http.MethodOptions,
Path: "/matchmaking/status",
Handler: optionsHandler,
},
{
// 取得房間資訊
Method: http.MethodGet,
Path: "/matchmaking/status",
Handler: chat.MatchStatusHandler(serverCtx),
},
{
// OPTIONS 支持
Method: http.MethodOptions,
Path: "/rooms/:room_id/messages",
Handler: optionsHandler,
},
{
// 傳送訊息
Method: http.MethodPost,
Path: "/rooms/:room_id/messages",
Handler: chat.SendMessageHandler(serverCtx),
},
{
// 取得訊息
Method: http.MethodGet,
Path: "/rooms/:room_id/messages",
Handler: chat.ListMessagesHandler(serverCtx),
},
}...,
),
rest.WithPrefix("/api/v1"),
rest.WithTimeout(10000*time.Millisecond),
)
}