refactor: Remove Token management and integrate Redis for authentication
- Deleted the Token model and its repository, transitioning to a Redis-based token management system. - Updated the service layer to utilize Redis for token storage, enhancing performance and scalability. - Refactored the container to remove TokenRepository and integrate the new token service. - Cleaned up the Dockerfile and other files by removing unnecessary whitespace and comments. - Enhanced error handling and logging for Redis initialization and usage.
This commit is contained in:
@@ -12,6 +12,7 @@ import (
|
||||
"carrotskin/internal/container"
|
||||
"carrotskin/internal/handler"
|
||||
"carrotskin/internal/middleware"
|
||||
"carrotskin/internal/task"
|
||||
"carrotskin/pkg/auth"
|
||||
"carrotskin/pkg/config"
|
||||
"carrotskin/pkg/database"
|
||||
@@ -59,11 +60,18 @@ func main() {
|
||||
loggerInstance.Fatal("JWT服务初始化失败", zap.Error(err))
|
||||
}
|
||||
|
||||
// 初始化Redis
|
||||
// 初始化Redis(开发/测试环境失败时会自动回退到miniredis)
|
||||
if err := redis.Init(cfg.Redis, loggerInstance); err != nil {
|
||||
loggerInstance.Fatal("Redis连接失败", zap.Error(err))
|
||||
loggerInstance.Fatal("Redis初始化失败", zap.Error(err))
|
||||
}
|
||||
defer redis.Close()
|
||||
|
||||
// 记录Redis模式
|
||||
if redis.IsUsingMiniRedis() {
|
||||
loggerInstance.Info("使用miniredis进行开发/测试")
|
||||
} else {
|
||||
loggerInstance.Info("使用生产Redis")
|
||||
}
|
||||
defer redis.MustGetClient().Close()
|
||||
|
||||
// 初始化对象存储 (RustFS - S3兼容)
|
||||
var storageClient *storage.StorageClient
|
||||
@@ -110,6 +118,13 @@ func main() {
|
||||
// 使用依赖注入方式注册路由
|
||||
handler.RegisterRoutesWithDI(router, c)
|
||||
|
||||
// 启动后台任务(Token已迁移到Redis,不再需要清理任务)
|
||||
// 如需使用数据库Token存储,可以恢复TokenCleanupTask
|
||||
taskRunner := task.NewRunner(loggerInstance)
|
||||
taskCtx, taskCancel := context.WithCancel(context.Background())
|
||||
defer taskCancel()
|
||||
taskRunner.Start(taskCtx)
|
||||
|
||||
// 创建HTTP服务器
|
||||
srv := &http.Server{
|
||||
Addr: cfg.Server.Port,
|
||||
@@ -132,6 +147,10 @@ func main() {
|
||||
<-quit
|
||||
loggerInstance.Info("正在关闭服务器...")
|
||||
|
||||
// 停止后台任务
|
||||
taskCancel()
|
||||
taskRunner.Wait()
|
||||
|
||||
// 设置关闭超时
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
||||
defer cancel()
|
||||
|
||||
Reference in New Issue
Block a user