修改domain包

This commit is contained in:
2025-11-05 21:40:19 +08:00
parent 203be4307d
commit 07d8c719ac
28 changed files with 943 additions and 793 deletions

View File

@@ -26,12 +26,11 @@ import (
"git.huangwc.com/pig/pig-farm-controller/internal/app/controller/user"
"git.huangwc.com/pig/pig-farm-controller/internal/app/service"
"git.huangwc.com/pig/pig-farm-controller/internal/app/webhook"
"git.huangwc.com/pig/pig-farm-controller/internal/domain/audit"
domain_plan "git.huangwc.com/pig/pig-farm-controller/internal/domain/plan"
"git.huangwc.com/pig/pig-farm-controller/internal/domain/token"
"git.huangwc.com/pig/pig-farm-controller/internal/infra/config"
"git.huangwc.com/pig/pig-farm-controller/internal/infra/logs"
"git.huangwc.com/pig/pig-farm-controller/internal/infra/repository"
"git.huangwc.com/pig/pig-farm-controller/internal/infra/utils/token"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
@@ -42,8 +41,8 @@ type API struct {
echo *echo.Echo // Echo 引擎实例,用于处理 HTTP 请求
Ctx context.Context // API 组件的上下文,包含日志记录器
userRepo repository.UserRepository // 用户数据仓库接口,用于用户数据操作
tokenService token.Service // Token 服务接口,用于 JWT token 的生成和解析
auditService audit.Service // 审计服务,用于记录用户操作
tokenGenerator token.Generator // Token 服务接口,用于 JWT token 的生成和解析
auditService service.AuditService // 审计服务,用于记录用户操作
httpServer *http.Server // 标准库的 HTTP 服务器实例,用于启动和停止服务
config config.ServerConfig // API 服务器的配置,使用 infra/config 包中的 ServerConfig
userController *user.Controller // 用户控制器实例
@@ -67,8 +66,8 @@ func NewAPI(cfg config.ServerConfig,
deviceService service.DeviceService,
planService service.PlanService,
userService service.UserService,
tokenService token.Service,
auditService audit.Service,
auditService service.AuditService,
tokenGenerator token.Generator,
listenHandler webhook.ListenHandler,
) *API {
// 使用 echo.New() 创建一个 Echo 引擎实例
@@ -84,13 +83,13 @@ func NewAPI(cfg config.ServerConfig,
// 初始化 API 结构体
baseCtx := context.Background()
api := &API{
echo: e,
Ctx: ctx,
userRepo: userRepo,
tokenService: tokenService,
auditService: auditService,
config: cfg,
listenHandler: listenHandler,
echo: e,
Ctx: ctx,
userRepo: userRepo,
tokenGenerator: tokenGenerator,
auditService: auditService,
config: cfg,
listenHandler: listenHandler,
// 在 NewAPI 中初始化用户控制器,并将其作为 API 结构体的成员
userController: user.NewController(baseCtx, userService),
// 在 NewAPI 中初始化设备控制器,并将其作为 API 结构体的成员

View File

@@ -54,8 +54,8 @@ func (a *API) setupRoutes() {
// --- Authenticated Routes ---
// 所有在此注册的路由都需要通过 JWT 身份验证
authGroup := a.echo.Group("/api/v1")
authGroup.Use(middleware.AuthMiddleware(logs.AddCompName(context.Background(), "AuthMiddleware"), a.tokenService, a.userRepo)) // 1. 身份认证中间件
authGroup.Use(middleware.AuditLogMiddleware(logs.AddCompName(context.Background(), "AuditLogMiddleware"), a.auditService)) // 2. 审计日志中间件
authGroup.Use(middleware.AuthMiddleware(logs.AddCompName(context.Background(), "AuthMiddleware"), a.tokenGenerator, a.userRepo)) // 1. 身份认证中间件
authGroup.Use(middleware.AuditLogMiddleware(logs.AddCompName(context.Background(), "AuditLogMiddleware"), a.auditService)) // 2. 审计日志中间件
{
// 用户相关路由组
userGroup := authGroup.Group("/users")