.
This commit is contained in:
62
internal/module/auth_module.go
Normal file
62
internal/module/auth_module.go
Normal file
@@ -0,0 +1,62 @@
|
||||
package module
|
||||
|
||||
import (
|
||||
"context"
|
||||
"ssq-desk/internal/api"
|
||||
)
|
||||
|
||||
// AuthModule 授权码模块
|
||||
type AuthModule struct {
|
||||
BaseModule
|
||||
authAPI *api.AuthAPI
|
||||
}
|
||||
|
||||
// NewAuthModule 创建授权码模块
|
||||
func NewAuthModule() (*AuthModule, error) {
|
||||
// 延迟初始化,等到 Init() 方法调用时再创建 API(此时数据库已初始化)
|
||||
return &AuthModule{
|
||||
BaseModule: BaseModule{
|
||||
name: "auth",
|
||||
api: nil, // 延迟初始化
|
||||
},
|
||||
authAPI: nil, // 延迟初始化
|
||||
}, nil
|
||||
}
|
||||
|
||||
// Init 初始化模块
|
||||
func (m *AuthModule) Init(ctx context.Context) error {
|
||||
if m.authAPI == nil {
|
||||
authAPI, err := api.NewAuthAPI()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
m.authAPI = authAPI
|
||||
m.api = authAPI
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Start 启动模块(检查授权状态)
|
||||
func (m *AuthModule) Start(ctx context.Context) error {
|
||||
if m.authAPI == nil {
|
||||
return m.Init(ctx)
|
||||
}
|
||||
|
||||
status, err := m.authAPI.GetAuthStatus()
|
||||
if err == nil {
|
||||
if data, ok := status["data"].(map[string]interface{}); ok && data != nil {
|
||||
if isActivated, ok := data["is_activated"].(bool); ok && !isActivated {
|
||||
println("授权未激活,部分功能可能受限")
|
||||
} else {
|
||||
println("授权验证通过")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// AuthAPI 返回 Auth API(类型安全)
|
||||
func (m *AuthModule) AuthAPI() *api.AuthAPI {
|
||||
return m.authAPI
|
||||
}
|
||||
119
internal/module/manager.go
Normal file
119
internal/module/manager.go
Normal file
@@ -0,0 +1,119 @@
|
||||
package module
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"sync"
|
||||
)
|
||||
|
||||
// Manager 模块管理器
|
||||
type Manager struct {
|
||||
modules map[string]Module
|
||||
mu sync.RWMutex
|
||||
}
|
||||
|
||||
// NewManager 创建模块管理器
|
||||
func NewManager() *Manager {
|
||||
return &Manager{
|
||||
modules: make(map[string]Module),
|
||||
}
|
||||
}
|
||||
|
||||
// Register 注册模块
|
||||
func (m *Manager) Register(module Module) error {
|
||||
if module == nil {
|
||||
return fmt.Errorf("模块不能为空")
|
||||
}
|
||||
|
||||
name := module.Name()
|
||||
if name == "" {
|
||||
return fmt.Errorf("模块名称不能为空")
|
||||
}
|
||||
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
|
||||
if _, exists := m.modules[name]; exists {
|
||||
return fmt.Errorf("模块 %s 已存在", name)
|
||||
}
|
||||
|
||||
m.modules[name] = module
|
||||
return nil
|
||||
}
|
||||
|
||||
// Get 获取模块
|
||||
func (m *Manager) Get(name string) (Module, bool) {
|
||||
m.mu.RLock()
|
||||
defer m.mu.RUnlock()
|
||||
|
||||
module, exists := m.modules[name]
|
||||
return module, exists
|
||||
}
|
||||
|
||||
// GetAll 获取所有模块
|
||||
func (m *Manager) GetAll() []Module {
|
||||
m.mu.RLock()
|
||||
defer m.mu.RUnlock()
|
||||
|
||||
modules := make([]Module, 0, len(m.modules))
|
||||
for _, module := range m.modules {
|
||||
modules = append(modules, module)
|
||||
}
|
||||
return modules
|
||||
}
|
||||
|
||||
// InitAll 初始化所有模块
|
||||
func (m *Manager) InitAll(ctx context.Context) error {
|
||||
m.mu.RLock()
|
||||
defer m.mu.RUnlock()
|
||||
|
||||
for name, module := range m.modules {
|
||||
if err := module.Init(ctx); err != nil {
|
||||
return fmt.Errorf("初始化模块 %s 失败: %v", name, err)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// StartAll 启动所有模块
|
||||
func (m *Manager) StartAll(ctx context.Context) error {
|
||||
m.mu.RLock()
|
||||
defer m.mu.RUnlock()
|
||||
|
||||
for name, module := range m.modules {
|
||||
if err := module.Start(ctx); err != nil {
|
||||
return fmt.Errorf("启动模块 %s 失败: %v", name, err)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// StopAll 停止所有模块
|
||||
func (m *Manager) StopAll(ctx context.Context) error {
|
||||
m.mu.RLock()
|
||||
defer m.mu.RUnlock()
|
||||
|
||||
for name, module := range m.modules {
|
||||
if err := module.Stop(ctx); err != nil {
|
||||
return fmt.Errorf("停止模块 %s 失败: %v", name, err)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetAPIs 获取所有模块的 API
|
||||
func (m *Manager) GetAPIs() []interface{} {
|
||||
m.mu.RLock()
|
||||
defer m.mu.RUnlock()
|
||||
|
||||
apis := make([]interface{}, 0, len(m.modules))
|
||||
for _, module := range m.modules {
|
||||
if api := module.GetAPI(); api != nil {
|
||||
apis = append(apis, api)
|
||||
}
|
||||
}
|
||||
return apis
|
||||
}
|
||||
52
internal/module/module.go
Normal file
52
internal/module/module.go
Normal file
@@ -0,0 +1,52 @@
|
||||
package module
|
||||
|
||||
import "context"
|
||||
|
||||
// Module 模块接口
|
||||
type Module interface {
|
||||
// Name 返回模块名称
|
||||
Name() string
|
||||
|
||||
// Init 初始化模块
|
||||
Init(ctx context.Context) error
|
||||
|
||||
// Start 启动模块
|
||||
Start(ctx context.Context) error
|
||||
|
||||
// Stop 停止模块
|
||||
Stop(ctx context.Context) error
|
||||
|
||||
// GetAPI 获取模块的 API 接口(供前端调用)
|
||||
GetAPI() interface{}
|
||||
}
|
||||
|
||||
// BaseModule 基础模块实现
|
||||
type BaseModule struct {
|
||||
name string
|
||||
api interface{}
|
||||
}
|
||||
|
||||
// Name 返回模块名称
|
||||
func (m *BaseModule) Name() string {
|
||||
return m.name
|
||||
}
|
||||
|
||||
// GetAPI 获取模块的 API 接口
|
||||
func (m *BaseModule) GetAPI() interface{} {
|
||||
return m.api
|
||||
}
|
||||
|
||||
// Init 初始化模块(默认实现)
|
||||
func (m *BaseModule) Init(ctx context.Context) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Start 启动模块(默认实现)
|
||||
func (m *BaseModule) Start(ctx context.Context) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Stop 停止模块(默认实现)
|
||||
func (m *BaseModule) Stop(ctx context.Context) error {
|
||||
return nil
|
||||
}
|
||||
42
internal/module/ssq_module.go
Normal file
42
internal/module/ssq_module.go
Normal file
@@ -0,0 +1,42 @@
|
||||
package module
|
||||
|
||||
import (
|
||||
"context"
|
||||
"ssq-desk/internal/api"
|
||||
)
|
||||
|
||||
// SsqModule 双色球查询模块
|
||||
type SsqModule struct {
|
||||
BaseModule
|
||||
ssqAPI *api.SsqAPI
|
||||
}
|
||||
|
||||
// NewSsqModule 创建双色球查询模块
|
||||
func NewSsqModule() (*SsqModule, error) {
|
||||
// 延迟初始化,等到 Init() 方法调用时再创建 API(此时数据库已初始化)
|
||||
return &SsqModule{
|
||||
BaseModule: BaseModule{
|
||||
name: "ssq",
|
||||
api: nil, // 延迟初始化
|
||||
},
|
||||
ssqAPI: nil, // 延迟初始化
|
||||
}, nil
|
||||
}
|
||||
|
||||
// Init 初始化模块
|
||||
func (m *SsqModule) Init(ctx context.Context) error {
|
||||
if m.ssqAPI == nil {
|
||||
ssqAPI, err := api.NewSsqAPI()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
m.ssqAPI = ssqAPI
|
||||
m.api = ssqAPI
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// SsqAPI 返回 SSQ API(类型安全)
|
||||
func (m *SsqModule) SsqAPI() *api.SsqAPI {
|
||||
return m.ssqAPI
|
||||
}
|
||||
94
internal/module/update_module.go
Normal file
94
internal/module/update_module.go
Normal file
@@ -0,0 +1,94 @@
|
||||
package module
|
||||
|
||||
import (
|
||||
"context"
|
||||
"ssq-desk/internal/api"
|
||||
"ssq-desk/internal/service"
|
||||
)
|
||||
|
||||
// UpdateModule 版本更新模块
|
||||
type UpdateModule struct {
|
||||
BaseModule
|
||||
updateAPI *api.UpdateAPI
|
||||
checkURL string // 版本检查接口 URL
|
||||
}
|
||||
|
||||
// NewUpdateModule 创建版本更新模块
|
||||
func NewUpdateModule() (*UpdateModule, error) {
|
||||
// 从配置文件读取检查 URL
|
||||
config, err := service.LoadUpdateConfig()
|
||||
if err != nil {
|
||||
// 配置加载失败,使用默认值
|
||||
config = &service.UpdateConfig{}
|
||||
}
|
||||
|
||||
checkURL := config.CheckURL
|
||||
if checkURL == "" {
|
||||
// 如果配置中没有,使用默认地址
|
||||
checkURL = "https://img.1216.top/ssq/last-version.json"
|
||||
}
|
||||
|
||||
updateAPI, err := api.NewUpdateAPI(checkURL)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &UpdateModule{
|
||||
BaseModule: BaseModule{
|
||||
name: "update",
|
||||
api: updateAPI,
|
||||
},
|
||||
updateAPI: updateAPI,
|
||||
checkURL: checkURL,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// Init 初始化模块
|
||||
func (m *UpdateModule) Init(ctx context.Context) error {
|
||||
if m.updateAPI == nil {
|
||||
updateAPI, err := api.NewUpdateAPI(m.checkURL)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
m.updateAPI = updateAPI
|
||||
m.api = updateAPI
|
||||
}
|
||||
// 设置 context 以便推送事件
|
||||
if m.updateAPI != nil {
|
||||
m.updateAPI.SetContext(ctx)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Start 启动模块(检查更新配置,决定是否自动检查)
|
||||
func (m *UpdateModule) Start(ctx context.Context) error {
|
||||
if m.updateAPI == nil {
|
||||
return m.Init(ctx)
|
||||
}
|
||||
|
||||
// 加载配置
|
||||
config, err := service.LoadUpdateConfig()
|
||||
if err != nil {
|
||||
// 配置加载失败不影响启动,只记录日志
|
||||
return nil
|
||||
}
|
||||
|
||||
// 如果启用了自动检查且满足检查条件,则检查更新
|
||||
if config.ShouldCheckUpdate() && config.CheckURL != "" {
|
||||
// 异步检查更新,不阻塞启动流程
|
||||
go func() {
|
||||
_, err := m.updateAPI.CheckUpdate()
|
||||
if err == nil {
|
||||
// 更新最后检查时间
|
||||
config.UpdateLastCheckTime()
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// UpdateAPI 返回 Update API(类型安全)
|
||||
func (m *UpdateModule) UpdateAPI() *api.UpdateAPI {
|
||||
return m.updateAPI
|
||||
}
|
||||
Reference in New Issue
Block a user