This commit is contained in:
2025-07-28 01:46:39 +08:00
parent b03129667a
commit 357639462a
219 changed files with 21634 additions and 8138 deletions

View File

@@ -26,25 +26,35 @@ func SetupGormCache(db *gorm.DB, cacheService interfaces.CacheService, cfg *conf
BloomFilter: false,
AutoInvalidate: true,
InvalidateDelay: 100 * time.Millisecond,
// 配置启用缓存的表
EnabledTables: []string{
"users",
"products",
"product_categories",
"product",
"product_category",
"enterprise_info_submit_records",
"sms_codes",
"wallets",
"subscription",
"product_category",
"product_documentation",
"enterprise_infos",
"api_users",
// 添加更多需要缓存的表
},
// 配置禁用缓存的表(日志表等)
DisabledTables: []string{
"sms_codes", // 短信验证码变化频繁
"audit_logs", // 审计日志
"system_logs", // 系统日志
"operation_logs", // 操作日志
"audit_logs", // 审计日志
"system_logs", // 系统日志
"operation_logs", // 操作日志
"api_calls", // API调用日志表变化频繁不适合缓存
},
}
// 初始化全局缓存配置管理器
cache.InitCacheConfigManager(cacheConfig)
// 创建缓存插件
cachePlugin := cache.NewGormCachePlugin(cacheService, logger, cacheConfig)
@@ -54,7 +64,7 @@ func SetupGormCache(db *gorm.DB, cacheService interfaces.CacheService, cfg *conf
return err
}
logger.Info("GORM缓存插件已成功注册",
logger.Info("GORM缓存插件已成功注册",
zap.Duration("default_ttl", cacheConfig.DefaultTTL),
zap.Strings("enabled_tables", cacheConfig.EnabledTables),
zap.Strings("disabled_tables", cacheConfig.DisabledTables),
@@ -67,52 +77,67 @@ func SetupGormCache(db *gorm.DB, cacheService interfaces.CacheService, cfg *conf
func GetCacheConfig(cfg *config.Config) cache.CacheConfig {
// 生产环境配置
if cfg.Server.Mode == "release" {
return cache.CacheConfig{
DefaultTTL: 60 * time.Minute, // 生产环境延长缓存时间
cacheConfig := cache.CacheConfig{
DefaultTTL: 60 * time.Minute, // 生产环境延长缓存时间
TablePrefix: "prod_cache",
MaxCacheSize: 5000, // 生产环境增加缓存大小
CacheComplexSQL: false, // 生产环境不缓存复杂SQL
MaxCacheSize: 5000, // 生产环境增加缓存大小
CacheComplexSQL: false, // 生产环境不缓存复杂SQL
EnableStats: true,
EnableWarmup: true,
PenetrationGuard: true,
BloomFilter: true, // 生产环境启用布隆过滤器
BloomFilter: true, // 生产环境启用布隆过滤器
AutoInvalidate: true,
InvalidateDelay: 50 * time.Millisecond,
EnabledTables: []string{
"users", "products", "product_categories",
"enterprise_info_submit_records", "certifications",
"product_documentations",
"users",
"product",
"product_category",
"enterprise_info_submit_records",
"sms_codes",
},
DisabledTables: []string{
"sms_codes", "audit_logs", "system_logs",
"operation_logs", "sessions", "api_keys",
"api_calls", // API调用日志表变化频繁不适合缓存
},
}
// 初始化全局缓存配置管理器
cache.InitCacheConfigManager(cacheConfig)
return cacheConfig
}
// 开发环境配置
return cache.CacheConfig{
DefaultTTL: 10 * time.Minute, // 开发环境缩短缓存时间,便于测试
cacheConfig := cache.CacheConfig{
DefaultTTL: 10 * time.Minute, // 开发环境缩短缓存时间,便于测试
TablePrefix: "dev_cache",
MaxCacheSize: 500,
CacheComplexSQL: true, // 开发环境允许缓存复杂SQL便于调试
CacheComplexSQL: true, // 开发环境允许缓存复杂SQL便于调试
EnableStats: true,
EnableWarmup: false, // 开发环境关闭预热
PenetrationGuard: false, // 开发环境关闭穿透保护
EnableWarmup: false, // 开发环境关闭预热
PenetrationGuard: false, // 开发环境关闭穿透保护
BloomFilter: false,
AutoInvalidate: true,
InvalidateDelay: 200 * time.Millisecond,
EnabledTables: []string{
"users", "products", "product_categories",
"users",
"product",
"product_category",
"enterprise_info_submit_records",
"sms_codes",
},
DisabledTables: []string{
"sms_codes", "audit_logs",
"api_calls", // API调用日志表变化频繁不适合缓存
},
}
// 初始化全局缓存配置管理器
cache.InitCacheConfigManager(cacheConfig)
return cacheConfig
}
// CacheMetrics 缓存性能指标
@@ -136,7 +161,7 @@ func GetCacheMetrics(cacheService interfaces.CacheService) (*CacheMetrics, error
total := stats.Hits + stats.Misses
hitRate := float64(0)
missRate := float64(0)
if total > 0 {
hitRate = float64(stats.Hits) / float64(total) * 100
missRate = float64(stats.Misses) / float64(total) * 100
@@ -150,4 +175,4 @@ func GetCacheMetrics(cacheService interfaces.CacheService) (*CacheMetrics, error
CacheSize: stats.Memory,
CachedTables: int(stats.Keys),
}, nil
}
}

View File

@@ -2,7 +2,6 @@ package container
import (
"context"
"fmt"
"time"
"go.uber.org/fx"
@@ -15,6 +14,7 @@ import (
"tyapi-server/internal/application/user"
"tyapi-server/internal/config"
domain_certification_repo "tyapi-server/internal/domains/certification/repositories"
certification_service "tyapi-server/internal/domains/certification/services"
domain_finance_repo "tyapi-server/internal/domains/finance/repositories"
finance_service "tyapi-server/internal/domains/finance/services"
domain_product_repo "tyapi-server/internal/domains/product/repositories"
@@ -28,6 +28,8 @@ import (
"tyapi-server/internal/infrastructure/external/ocr"
"tyapi-server/internal/infrastructure/external/sms"
"tyapi-server/internal/infrastructure/external/storage"
"tyapi-server/internal/infrastructure/external/westdex"
"tyapi-server/internal/infrastructure/external/yushan"
"tyapi-server/internal/infrastructure/http/handlers"
"tyapi-server/internal/infrastructure/http/routes"
shared_database "tyapi-server/internal/shared/database"
@@ -41,9 +43,9 @@ import (
"tyapi-server/internal/shared/metrics"
"tyapi-server/internal/shared/middleware"
sharedOCR "tyapi-server/internal/shared/ocr"
"tyapi-server/internal/shared/payment"
"tyapi-server/internal/shared/resilience"
"tyapi-server/internal/shared/saga"
sharedStorage "tyapi-server/internal/shared/storage"
"tyapi-server/internal/shared/tracing"
"tyapi-server/internal/shared/validator"
@@ -51,6 +53,11 @@ import (
user_repo "tyapi-server/internal/infrastructure/database/repositories/user"
"github.com/redis/go-redis/v9"
api_app "tyapi-server/internal/application/api"
domain_api_repo "tyapi-server/internal/domains/api/repositories"
api_service "tyapi-server/internal/domains/api/services"
api_repo "tyapi-server/internal/infrastructure/database/repositories/api"
)
// Container 应用容器
@@ -156,7 +163,6 @@ func NewContainer() *Container {
logger,
)
},
fx.As(new(sharedStorage.StorageService)),
),
// OCR服务
fx.Annotate(
@@ -169,19 +175,49 @@ func NewContainer() *Container {
},
fx.As(new(sharedOCR.OCRService)),
),
// e签宝服务
func(cfg *config.Config) *esign.Client {
esignConfig, err := esign.NewConfig(
// e签宝配置
func(cfg *config.Config) (*esign.Config, error) {
return esign.NewConfig(
cfg.Esign.AppID,
cfg.Esign.AppSecret,
cfg.Esign.ServerURL,
cfg.Esign.TemplateID,
&esign.EsignContractConfig{
Name: cfg.Esign.Contract.Name,
ExpireDays: cfg.Esign.Contract.ExpireDays,
RetryCount: cfg.Esign.Contract.RetryCount,
},
&esign.EsignAuthConfig{
OrgAuthModes: cfg.Esign.Auth.OrgAuthModes,
DefaultAuthMode: cfg.Esign.Auth.DefaultAuthMode,
PsnAuthModes: cfg.Esign.Auth.PsnAuthModes,
WillingnessAuthModes: cfg.Esign.Auth.WillingnessAuthModes,
RedirectUrl: cfg.Esign.Auth.RedirectURL,
},
&esign.EsignSignConfig{
AutoFinish: cfg.Esign.Sign.AutoFinish,
SignFieldStyle: cfg.Esign.Sign.SignFieldStyle,
ClientType: cfg.Esign.Sign.ClientType,
RedirectUrl: cfg.Esign.Sign.RedirectURL,
},
)
if err != nil {
panic(fmt.Sprintf("e签宝配置创建失败: %v", err))
}
},
// e签宝服务
func(esignConfig *esign.Config) *esign.Client {
return esign.NewClient(esignConfig)
},
// 支付宝支付服务
func(cfg *config.Config) *payment.AliPayService {
config := payment.AlipayConfig{
AppID: cfg.AliPay.AppID,
PrivateKey: cfg.AliPay.PrivateKey,
AlipayPublicKey: cfg.AliPay.AlipayPublicKey,
IsProduction: cfg.AliPay.IsProduction,
NotifyUrl: cfg.AliPay.NotifyURL,
ReturnURL: cfg.AliPay.ReturnURL,
}
return payment.NewAliPayService(config)
},
),
// 高级特性模块
@@ -210,6 +246,22 @@ func NewContainer() *Container {
fx.Provide(
sharedhttp.NewResponseBuilder,
validator.NewRequestValidator,
// WestDexService - 需要从配置中获取参数
func(cfg *config.Config) *westdex.WestDexService {
return westdex.NewWestDexService(
cfg.WestDex.URL,
cfg.WestDex.Key,
cfg.WestDex.SecretId,
cfg.WestDex.SecretSecondId,
)
},
func(cfg *config.Config) *yushan.YushanService {
return yushan.NewYushanService(
cfg.Yushan.URL,
cfg.Yushan.APIKey,
cfg.Yushan.AcctID,
)
},
sharedhttp.NewGinRouter,
),
@@ -224,6 +276,7 @@ func NewContainer() *Container {
middleware.NewJWTAuthMiddleware,
middleware.NewOptionalAuthMiddleware,
middleware.NewAdminAuthMiddleware,
middleware.NewDomainAuthMiddleware,
middleware.NewTraceIDMiddleware,
middleware.NewErrorTrackingMiddleware,
NewRequestBodyLoggerMiddlewareWrapper,
@@ -236,16 +289,22 @@ func NewContainer() *Container {
user_repo.NewGormUserRepository,
fx.As(new(domain_user_repo.UserRepository)),
),
// 企业信息仓储 - 同时注册具体类型和接口类型
fx.Annotate(
user_repo.NewGormEnterpriseInfoRepository,
fx.As(new(domain_user_repo.EnterpriseInfoRepository)),
),
// 短信验证码仓储 - 同时注册具体类型和接口类型
fx.Annotate(
user_repo.NewGormSMSCodeRepository,
fx.As(new(domain_user_repo.SMSCodeRepository)),
),
// 用户信息仓储 - 同时注册具体类型和接口类型
fx.Annotate(
user_repo.NewGormEnterpriseInfoRepository,
fx.As(new(domain_user_repo.EnterpriseInfoRepository)),
),
// 合同信息仓储 - 同时注册具体类型和接口类型
fx.Annotate(
user_repo.NewGormContractInfoRepository,
fx.As(new(domain_user_repo.ContractInfoRepository)),
),
),
// 仓储层 - 认证域
@@ -260,6 +319,11 @@ func NewContainer() *Container {
certification_repo.NewGormCertificationQueryRepository,
fx.As(new(domain_certification_repo.CertificationQueryRepository)),
),
// 企业信息提交记录仓储
fx.Annotate(
certification_repo.NewGormEnterpriseInfoSubmitRecordRepository,
fx.As(new(domain_certification_repo.EnterpriseInfoSubmitRecordRepository)),
),
),
// 仓储层 - 财务域
@@ -269,10 +333,20 @@ func NewContainer() *Container {
finance_repo.NewGormWalletRepository,
fx.As(new(domain_finance_repo.WalletRepository)),
),
// 用户密钥仓储
// 钱包交易记录仓储
fx.Annotate(
finance_repo.NewGormUserSecretsRepository,
fx.As(new(domain_finance_repo.UserSecretsRepository)),
finance_repo.NewGormWalletTransactionRepository,
fx.As(new(domain_finance_repo.WalletTransactionRepository)),
),
// 充值记录仓储
fx.Annotate(
finance_repo.NewGormRechargeRecordRepository,
fx.As(new(domain_finance_repo.RechargeRecordRepository)),
),
// 支付宝订单仓储
fx.Annotate(
finance_repo.NewGormAlipayOrderRepository,
fx.As(new(domain_finance_repo.AlipayOrderRepository)),
),
),
@@ -293,18 +367,50 @@ func NewContainer() *Container {
product_repo.NewGormSubscriptionRepository,
fx.As(new(domain_product_repo.SubscriptionRepository)),
),
// 产品API配置仓储 - 同时注册具体类型和接口类型
fx.Annotate(
product_repo.NewGormProductApiConfigRepository,
fx.As(new(domain_product_repo.ProductApiConfigRepository)),
),
),
// API域仓储层
fx.Provide(
fx.Annotate(
api_repo.NewGormApiUserRepository,
fx.As(new(domain_api_repo.ApiUserRepository)),
),
fx.Annotate(
api_repo.NewGormApiCallRepository,
fx.As(new(domain_api_repo.ApiCallRepository)),
),
),
// 领域服务
fx.Provide(
user_service.NewUserManagementService,
user_service.NewUserAggregateService,
user_service.NewUserAuthService,
user_service.NewSMSCodeService,
user_service.NewEnterpriseService,
user_service.NewContractAggregateService,
product_service.NewProductManagementService,
product_service.NewProductSubscriptionService,
// 认证域的领域服务已经整合到应用服务中
finance_service.NewFinanceService,
product_service.NewProductApiConfigService,
finance_service.NewWalletAggregateService,
finance_service.NewRechargeRecordService,
certification_service.NewCertificationAggregateService,
certification_service.NewEnterpriseInfoSubmitRecordService,
),
// API域服务层
fx.Provide(
api_service.NewApiUserAggregateService,
api_service.NewApiCallAggregateService,
api_service.NewApiRequestService,
),
// API域应用服务
fx.Provide(
api_app.NewApiApplicationService,
),
// 应用服务
@@ -329,6 +435,11 @@ func NewContainer() *Container {
product.NewProductApplicationService,
fx.As(new(product.ProductApplicationService)),
),
// 产品API配置应用服务 - 绑定到接口
fx.Annotate(
product.NewProductApiConfigApplicationService,
fx.As(new(product.ProductApiConfigApplicationService)),
),
// 分类应用服务 - 绑定到接口
fx.Annotate(
product.NewCategoryApplicationService,
@@ -353,6 +464,8 @@ func NewContainer() *Container {
handlers.NewProductHandler,
// 产品管理员HTTP处理器
handlers.NewProductAdminHandler,
// API Handler
handlers.NewApiHandler,
),
// 路由注册
@@ -367,6 +480,8 @@ func NewContainer() *Container {
routes.NewProductRoutes,
// 产品管理员路由
routes.NewProductAdminRoutes,
// API路由
routes.NewApiRoutes,
),
// 应用生命周期
@@ -445,12 +560,16 @@ func RegisterRoutes(
financeRoutes *routes.FinanceRoutes,
productRoutes *routes.ProductRoutes,
productAdminRoutes *routes.ProductAdminRoutes,
apiRoutes *routes.ApiRoutes,
cfg *config.Config,
logger *zap.Logger,
) {
router.SetupDefaultRoutes()
// 注册所有路由
// api域名路由
apiRoutes.Register(router)
// 所有域名路由路由
userRoutes.Register(router)
certificationRoutes.Register(router)
financeRoutes.Register(router)