feat: 打通网关和用户注册的逻辑,添加用户注册 HTTP 端点
This commit is contained in:
@@ -1,9 +1,11 @@
|
||||
package router
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
"backend/gateway/internal/service"
|
||||
"backend/gateway/internal/ws"
|
||||
|
||||
"github.com/gorilla/websocket"
|
||||
@@ -19,12 +21,14 @@ var upgrader = websocket.Upgrader{
|
||||
}
|
||||
|
||||
type Router struct {
|
||||
hub *ws.Hub
|
||||
hub *ws.Hub
|
||||
userService *service.UserService
|
||||
}
|
||||
|
||||
func NewRouter(hub *ws.Hub) *Router {
|
||||
func NewRouter(hub *ws.Hub, userService *service.UserService) *Router {
|
||||
return &Router{
|
||||
hub: hub,
|
||||
hub: hub,
|
||||
userService: userService,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -37,9 +41,65 @@ 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(®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 {
|
||||
|
||||
42
backend/gateway/internal/service/user_service.go
Normal file
42
backend/gateway/internal/service/user_service.go
Normal file
@@ -0,0 +1,42 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"backend/gateway/internal/config"
|
||||
"backend/shared/pkg/logger"
|
||||
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/grpc/credentials/insecure"
|
||||
|
||||
// 导入生成的 proto 代码
|
||||
userpb "backend/services/user-svc/proto"
|
||||
)
|
||||
|
||||
type UserService struct {
|
||||
client userpb.UserServiceClient
|
||||
}
|
||||
|
||||
func NewUserService(cfg *config.Config) (*UserService, error) {
|
||||
// 连接到用户服务
|
||||
conn, err := grpc.Dial(cfg.Services.UserService.Addr, grpc.WithTransportCredentials(insecure.NewCredentials()))
|
||||
if err != nil {
|
||||
logger.Error("Failed to connect to user service: %v", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 创建 gRPC 客户端
|
||||
client := userpb.NewUserServiceClient(conn)
|
||||
|
||||
return &UserService{client: client}, nil
|
||||
}
|
||||
|
||||
// Register 用户注册
|
||||
func (s *UserService) Register(ctx context.Context, account, password string) (*userpb.RegisterResponse, error) {
|
||||
req := &userpb.RegisterRequest{
|
||||
Account: account,
|
||||
Password: password,
|
||||
}
|
||||
|
||||
return s.client.Register(ctx, req)
|
||||
}
|
||||
Reference in New Issue
Block a user