mirror of
https://github.com/Rain-kl/OpenFlare.git
synced 2026-09-29 22:06:38 +08:00
299ac30ee4
- Purify core micro-kernel by removing context hardcoded helpers and reverse dependencies - Eliminate init() side effects in infra plugins with reversible lifecycle disposal - Completely isolate plugins by removing cross-plugin imports and using core/contracts - Introduce TaskService and RiskControlService contracts for unified cross-plugin APIs - Regenerate Swagger documentation and update developer guide matrix - Achieve 0 violations in check_cordis_architecture.sh and 100% test pass
132 lines
2.7 KiB
Go
132 lines
2.7 KiB
Go
// Copyright 2026 Arctel.net
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
package shared
|
|
|
|
import (
|
|
"context"
|
|
"sync"
|
|
|
|
"gorm.io/gorm"
|
|
|
|
"Wavelet/core"
|
|
"Wavelet/core/contracts"
|
|
)
|
|
|
|
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
|
|
}
|