Files
backend/internal/wire/grpc.go
lan d9aa4b46c3
All checks were successful
Build Backend / build (push) Successful in 4m55s
Build Backend / build-docker (push) Successful in 10m34s
refactor(server): decouple services and improve architecture
- Introduce interfaces for all major services (JWT, PostAI, Comment, Message, Notification, QRCodeLogin, Upload, Vote, etc.) to support dependency inversion.
- Move query parameters from `internal/dto` to a new `internal/query` package to separate request payloads from data transfer objects.
- Refactor `internal/router` to use embedded `RouterDeps` for cleaner dependency management.
- Decouple handlers from repositories by injecting services instead of direct repository access, ensuring proper layering.
- Improve database initialization by moving it from `internal/model` to `internal/database`.
- Optimize message decryption by implementing a more efficient `BatchDecrypt` method in `MessageEncryptor` using a worker pool.
- Enhance error handling and security by implementing fail-fast checks for encryption key length during startup.
- Clean up unused code, including the `avatar` package and several unused DTOs.
2026-06-15 03:41:59 +08:00

110 lines
2.7 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package wire
import (
"time"
"with_you/internal/config"
"with_you/internal/grpc/runner"
"with_you/internal/pkg/redis"
"github.com/google/uuid"
"github.com/google/wire"
"go.uber.org/zap"
)
// GRPCSet gRPC 相关的 Provider Set
var GRPCSet = wire.NewSet(
ProvideGRPCServer,
ProvideRunnerHub,
ProvideTaskManager,
ProvideRunnerRegistry,
ProvideTaskDispatcher,
)
// ProvideGRPCServer 提供 gRPC 服务器
func ProvideGRPCServer(cfg *config.Config, logger *zap.Logger, hub *runner.RunnerHub) *runner.Server {
return runner.NewServerWithDeps(&cfg.GRPC, logger, hub)
}
// ProvideRunnerHub 创建 RunnerHub
func ProvideRunnerHub(logger *zap.Logger) *runner.RunnerHub {
return runner.NewRunnerHub(nil, logger)
}
// ProvideTaskManager 提供 TaskManager
func ProvideTaskManager(hub *runner.RunnerHub, logger *zap.Logger) *runner.TaskManager {
tm := runner.NewTaskManager(nil, 60*time.Second, logger)
tm.SetDispatcher(hub)
hub.SetTaskManager(tm)
return tm
}
// ProvideRunnerRegistry 提供 Runner 注册中心
// standalone 模式或 Redis 不可用时返回 nil
func ProvideRunnerRegistry(cfg *config.Config, redisClient *redis.Client) *runner.RunnerRegistry {
if cfg.Runner.Mode != "cluster" || redisClient == nil {
return nil
}
instanceID := cfg.Runner.Cluster.InstanceID
if instanceID == "" {
instanceID = uuid.New().String()
}
rdb := redisClient.GetClient()
return runner.NewRunnerRegistry(
rdb,
instanceID,
cfg.Runner.Cluster.RegistryPrefix,
cfg.Runner.Cluster.CapPrefix,
time.Duration(cfg.Runner.Cluster.RegistryTTL)*time.Second,
time.Duration(cfg.Runner.Cluster.HeartbeatInterval)*time.Second,
zap.L(),
)
}
// ProvideTaskDispatcher 提供任务分发器
// standalone 模式返回 *RunnerHubcluster 模式返回 *TaskBus
func ProvideTaskDispatcher(
hub *runner.RunnerHub,
registry *runner.RunnerRegistry,
cfg *config.Config,
redisClient *redis.Client,
logger *zap.Logger,
) runner.TaskDispatcher {
if cfg.Runner.Mode != "cluster" || redisClient == nil || registry == nil {
logger.Info("Runner running in standalone mode")
return hub
}
instanceID := cfg.Runner.Cluster.InstanceID
if instanceID == "" {
instanceID = uuid.New().String()
}
rdb := redisClient.GetClient()
bus := runner.NewTaskBus(
hub,
rdb,
registry,
instanceID,
cfg.Runner.Cluster.TaskChannel,
cfg.Runner.Cluster.ResultChannel,
logger,
)
if err := bus.Start(); err != nil {
logger.Error("Failed to start Runner Bus, falling back to standalone", zap.Error(err))
return hub
}
// 注入 Bus 和 Registry 到 Hub
hub.SetBus(bus)
hub.SetRegistry(registry)
registry.StartHeartbeat()
logger.Info("Runner running in cluster mode", zap.String("instance_id", bus.InstanceID()))
return bus
}