后端: 1. 阶段 2 user/auth 服务边界落地,新增 `cmd/userauth` go-zero zrpc 服务、`services/userauth` 核心实现、gateway user API/zrpc client 与 shared contracts/ports,迁移注册、登录、刷新 token、登出、JWT、黑名单和 token 额度治理 2. gateway 与启动装配切流,`cmd/all` 只保留边缘路由、鉴权和轻量组合,通过 userauth zrpc 访问核心用户能力;拆分 MySQL/Redis 初始化与 AutoMigrate 边界,`userauth` 自迁 `users` 和 token 记账幂等表,`all` 不再迁用户表 3. 清退 Gin 单体旧 user/auth DAO、model、service、router、middleware 和 JWT handler,并同步调整 agent/schedule/cache/outbox 相关调用依赖 4. 补齐 refresh token 防并发重放、MySQL 幂等 token 记账、额度 `>=` 拦截和 RPC 错误映射,避免重复记账与内部错误透出 文档: 1. 新增《学习计划论坛与Token商店PRD》
73 lines
1.8 KiB
Go
73 lines
1.8 KiB
Go
package rpc
|
||
|
||
import (
|
||
"errors"
|
||
"log"
|
||
"strings"
|
||
"time"
|
||
|
||
"github.com/LoveLosita/smartflow/backend/services/userauth/rpc/pb"
|
||
userauthsv "github.com/LoveLosita/smartflow/backend/services/userauth/sv"
|
||
"github.com/zeromicro/go-zero/core/service"
|
||
"github.com/zeromicro/go-zero/zrpc"
|
||
"google.golang.org/grpc"
|
||
)
|
||
|
||
const (
|
||
defaultListenOn = "0.0.0.0:9081"
|
||
defaultTimeout = 2 * time.Second
|
||
)
|
||
|
||
type ServerOptions struct {
|
||
ListenOn string
|
||
Timeout time.Duration
|
||
Service *userauthsv.Service
|
||
}
|
||
|
||
// Start 启动 user/auth zrpc 服务。
|
||
//
|
||
// 职责边界:
|
||
// 1. 只负责装配 gozero zrpc server 和注册 protobuf service;
|
||
// 2. 不创建 DB/Redis 连接,这些依赖由 cmd/userauth 入口注入;
|
||
// 3. 阻塞直到进程收到退出信号,保持一个服务一个独立进程的迁移方向。
|
||
func Start(opts ServerOptions) {
|
||
server, listenOn, err := NewServer(opts)
|
||
if err != nil {
|
||
log.Fatalf("failed to build userauth zrpc server: %v", err)
|
||
}
|
||
defer server.Stop()
|
||
|
||
log.Printf("userauth zrpc service starting on %s", listenOn)
|
||
server.Start()
|
||
}
|
||
|
||
func NewServer(opts ServerOptions) (*zrpc.RpcServer, string, error) {
|
||
if opts.Service == nil {
|
||
return nil, "", errors.New("userauth service dependency not initialized")
|
||
}
|
||
|
||
listenOn := strings.TrimSpace(opts.ListenOn)
|
||
if listenOn == "" {
|
||
listenOn = defaultListenOn
|
||
}
|
||
timeout := opts.Timeout
|
||
if timeout <= 0 {
|
||
timeout = defaultTimeout
|
||
}
|
||
|
||
server, err := zrpc.NewServer(zrpc.RpcServerConf{
|
||
ServiceConf: service.ServiceConf{
|
||
Name: "userauth.rpc",
|
||
Mode: service.DevMode,
|
||
},
|
||
ListenOn: listenOn,
|
||
Timeout: int64(timeout / time.Millisecond),
|
||
}, func(grpcServer *grpc.Server) {
|
||
pb.RegisterUserAuthServer(grpcServer, NewHandler(opts.Service))
|
||
})
|
||
if err != nil {
|
||
return nil, "", err
|
||
}
|
||
return server, listenOn, nil
|
||
}
|