Files
OpenFlare/backend/plugins/domain/upload/shared/context_services.go
T
ryan 528240026d chore(lint): unify formatting on golangci-lint fmt (gofumpt), uncap issue reporting, fix gofumpt drift
- make format 现在与 code-check 使用同一格式化器(golangci-lint fmt),消除 goimports -local 与 gofumpt 的格式拉锯
- .golangci.yml 关闭默认 50/3 截断,完整上报所有问题(只增强不弱化)
- 全库 gofumpt 规范化(203 files, 纯格式无行为变更)
2026-08-28 16:31:53 +08:00

131 lines
2.7 KiB
Go

// Copyright 2026 Arctel.net
// SPDX-License-Identifier: Apache-2.0
package shared
import (
"Wavelet/core"
"Wavelet/core/contracts"
"context"
"sync"
"gorm.io/gorm"
)
var (
svcMu sync.RWMutex
dbSvc contracts.DBService
cacheSvc contracts.CacheService
storageSvc contracts.StorageService
taskSvc contracts.TaskService
authSvc contracts.AuthService
)
// SetDBService configures the DBService.
func SetDBService(s contracts.DBService) {
svcMu.Lock()
defer svcMu.Unlock()
dbSvc = s
}
// SetCacheService configures the CacheService.
func SetCacheService(s contracts.CacheService) {
svcMu.Lock()
defer svcMu.Unlock()
cacheSvc = s
}
// SetStorageService configures the StorageService.
func SetStorageService(s contracts.StorageService) {
svcMu.Lock()
defer svcMu.Unlock()
storageSvc = s
}
// SetTaskService configures the TaskService.
func SetTaskService(s contracts.TaskService) {
svcMu.Lock()
defer svcMu.Unlock()
taskSvc = s
}
// SetAuthService configures the AuthService.
func SetAuthService(s contracts.AuthService) {
svcMu.Lock()
defer svcMu.Unlock()
authSvc = s
}
// ResetServices clears all injected services.
func ResetServices() {
svcMu.Lock()
defer svcMu.Unlock()
dbSvc = nil
cacheSvc = nil
storageSvc = nil
taskSvc = nil
authSvc = nil
}
// GetDB resolves the GORM DB instance.
func GetDB(ctx context.Context) *gorm.DB {
if c, ok := ctx.(*core.Context); ok && c != nil {
if s, err := core.Inject[contracts.DBService](c); err == nil && s != nil {
return s.DB(ctx)
}
}
svcMu.RLock()
s := dbSvc
svcMu.RUnlock()
if s != nil {
return s.DB(ctx)
}
return nil
}
// GetCache resolves the CacheService instance.
func GetCache(ctx context.Context) contracts.CacheService {
if c, ok := ctx.(*core.Context); ok && c != nil {
if s, err := core.Inject[contracts.CacheService](c); err == nil && s != nil {
return s
}
}
svcMu.RLock()
s := cacheSvc
svcMu.RUnlock()
return s
}
// GetStorage resolves the StorageService instance.
func GetStorage(ctx context.Context) contracts.StorageService {
if c, ok := ctx.(*core.Context); ok && c != nil {
if s, err := core.Inject[contracts.StorageService](c); err == nil && s != nil {
return s
}
}
svcMu.RLock()
s := storageSvc
svcMu.RUnlock()
return s
}
// GetTaskService resolves the TaskService instance.
func GetTaskService() contracts.TaskService {
svcMu.RLock()
defer svcMu.RUnlock()
return taskSvc
}
// GetAuthService resolves the AuthService instance.
func GetAuthService(ctx context.Context) contracts.AuthService {
if c, ok := ctx.(*core.Context); ok && c != nil {
if s, err := core.Inject[contracts.AuthService](c); err == nil && s != nil {
return s
}
}
svcMu.RLock()
s := authSvc
svcMu.RUnlock()
return s
}