fix: 客户端和网关只能通过长连接方式通信,移除 HTTP 注册端点

This commit is contained in:
fish
2026-03-28 20:26:24 +08:00
parent b6efd99ea3
commit 4f1c0ca9b6
4 changed files with 73 additions and 65 deletions

View File

@@ -41,64 +41,10 @@ func (r *Router) SetupRoutes() http.Handler {
// 健康检查
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(&registerReq); 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)