123 lines
2.8 KiB
Go
123 lines
2.8 KiB
Go
package router
|
|
|
|
import (
|
|
"encoding/json"
|
|
"log"
|
|
"net/http"
|
|
|
|
"backend/gateway/internal/service"
|
|
"backend/gateway/internal/ws"
|
|
|
|
"github.com/gorilla/websocket"
|
|
)
|
|
|
|
var upgrader = websocket.Upgrader{
|
|
ReadBufferSize: 1024,
|
|
WriteBufferSize: 1024,
|
|
// 允许所有来源的请求
|
|
CheckOrigin: func(r *http.Request) bool {
|
|
return true
|
|
},
|
|
}
|
|
|
|
type Router struct {
|
|
hub *ws.Hub
|
|
userService *service.UserService
|
|
}
|
|
|
|
func NewRouter(hub *ws.Hub, userService *service.UserService) *Router {
|
|
return &Router{
|
|
hub: hub,
|
|
userService: userService,
|
|
}
|
|
}
|
|
|
|
func (r *Router) SetupRoutes() http.Handler {
|
|
mux := http.NewServeMux()
|
|
|
|
// WebSocket 连接处理
|
|
mux.HandleFunc("/ws", r.handleWebSocket)
|
|
|
|
// 健康检查
|
|
mux.HandleFunc("/health", r.handleHealth)
|
|
|
|
// 用户注册
|
|
mux.HandleFunc("/api/user/register", r.handleRegister)
|
|
|
|
return mux
|
|
}
|
|
|
|
// 注册请求结构
|
|
type registerRequest struct {
|
|
Account string `json:"account"`
|
|
Password string `json:"password"`
|
|
}
|
|
|
|
// 注册响应结构
|
|
type registerResponse struct {
|
|
UserID string `json:"user_id"`
|
|
Account string `json:"account"`
|
|
Message string `json:"message"`
|
|
Code int `json:"code"`
|
|
}
|
|
|
|
// handleRegister 处理用户注册请求
|
|
func (r *Router) handleRegister(w http.ResponseWriter, req *http.Request) {
|
|
if req.Method != http.MethodPost {
|
|
w.WriteHeader(http.StatusMethodNotAllowed)
|
|
w.Write([]byte(`{"code": 405, "message": "Method not allowed"}`))
|
|
return
|
|
}
|
|
|
|
// 解析请求体
|
|
var registerReq registerRequest
|
|
if err := json.NewDecoder(req.Body).Decode(®isterReq); err != nil {
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
w.Write([]byte(`{"code": 400, "message": "Invalid request body"}`))
|
|
return
|
|
}
|
|
|
|
// 调用用户服务注册
|
|
resp, err := r.userService.Register(req.Context(), registerReq.Account, registerReq.Password)
|
|
if err != nil {
|
|
log.Printf("Register failed: %v", err)
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
w.Write([]byte(`{"code": 500, "message": "Internal server error"}`))
|
|
return
|
|
}
|
|
|
|
// 构造响应
|
|
response := registerResponse{
|
|
UserID: resp.UserId,
|
|
Account: resp.Account,
|
|
Message: resp.Response.Message,
|
|
Code: int(resp.Response.Code),
|
|
}
|
|
|
|
// 返回响应
|
|
w.Header().Set("Content-Type", "application/json")
|
|
w.WriteHeader(http.StatusOK)
|
|
json.NewEncoder(w).Encode(response)
|
|
}
|
|
|
|
func (r *Router) handleWebSocket(w http.ResponseWriter, req *http.Request) {
|
|
conn, err := upgrader.Upgrade(w, req, nil)
|
|
if err != nil {
|
|
log.Println(err)
|
|
return
|
|
}
|
|
|
|
client := ws.NewClient(r.hub, conn)
|
|
r.hub.register <- client
|
|
|
|
// 启动客户端的读写协程
|
|
go client.writePump()
|
|
go client.readPump()
|
|
}
|
|
|
|
func (r *Router) handleHealth(w http.ResponseWriter, req *http.Request) {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
w.WriteHeader(http.StatusOK)
|
|
w.Write([]byte(`{"status": "ok"}`))
|
|
}
|