module

package
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 24, 2026 License: GPL-3.0 Imports: 6 Imported by: 0

Documentation

Overview

Package module 提供模块化架构的核心定义和加载器

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetModuleDescription

func GetModuleDescription(m Module) string

GetModuleDescription 获取模块描述

func IsModuleOptional

func IsModuleOptional(m Module) bool

IsModuleOptional 检查模块是否为可选模块

Types

type BaseOptionalModule

type BaseOptionalModule struct {
	// contains filtered or unexported fields
}

BaseOptionalModule 提供可选模块的默认实现 可选模块可以嵌入此结构体来获得默认行为

func NewBaseOptionalModule

func NewBaseOptionalModule(description string, config []ConfigField) *BaseOptionalModule

NewBaseOptionalModule 创建基础可选模块

func (*BaseOptionalModule) DefaultConfig

func (b *BaseOptionalModule) DefaultConfig() []ConfigField

DefaultConfig 返回模块的默认配置定义

func (*BaseOptionalModule) Description

func (b *BaseOptionalModule) Description() string

Description 返回模块描述

func (*BaseOptionalModule) IsOptional

func (b *BaseOptionalModule) IsOptional() bool

IsOptional 返回模块是否可被用户禁用

func (*BaseOptionalModule) SetDefaultConfig

func (b *BaseOptionalModule) SetDefaultConfig(config []ConfigField)

SetDefaultConfig 设置默认配置

func (*BaseOptionalModule) SetDescription

func (b *BaseOptionalModule) SetDescription(desc string)

SetDescription 设置描述

func (*BaseOptionalModule) SetOptional

func (b *BaseOptionalModule) SetOptional(optional bool)

SetOptional 设置是否可选

type ConfigField

type ConfigField struct {
	Key         string      `json:"key"`
	Label       string      `json:"label"`
	Type        string      `json:"type"` // string/number/bool/select
	Default     interface{} `json:"default"`
	Options     []string    `json:"options,omitempty"` // select 选项
	Description string      `json:"description,omitempty"`
	Required    bool        `json:"required,omitempty"`
}

ConfigField 配置字段定义

func GetModuleDefaultConfig

func GetModuleDefaultConfig(m Module) []ConfigField

GetModuleDefaultConfig 获取模块默认配置

type ConfigProvider

type ConfigProvider interface {
	Get(key string) interface{}
	GetString(key string) string
	GetInt(key string) int
	GetBool(key string) bool
	GetStringSlice(key string) []string
}

ConfigProvider 定义配置读取接口

type Context

type Context struct {
	// DB 是共享的数据库连接
	DB *gorm.DB

	// Config 提供配置读取能力
	Config ConfigProvider

	// EventBus 用于模块间异步通信
	EventBus EventBus

	// Logger 是模块专用的日志记录器
	Logger *zap.Logger

	// GetModule 获取其他已加载的模块
	// 用于模块间同步调用
	GetModule func(id string) Module

	// Extra 扩展数据(用于传递 TokenManager 等核心组件)
	Extra map[string]interface{}
}

Context 是传递给模块的核心上下文 包含模块运行所需的所有核心服务

type Event

type Event struct {
	Type      string      // 事件类型,如 "user.created", "file.uploaded"
	Source    string      // 来源模块 ID
	Data      interface{} // 事件数据
	Timestamp int64       // Unix 时间戳
}

Event 事件结构

type EventBus

type EventBus interface {
	// Publish 发布事件
	Publish(eventType string, data interface{})

	// Subscribe 订阅事件
	Subscribe(eventType string, handler EventHandler)

	// Unsubscribe 取消订阅
	Unsubscribe(eventType string, handler EventHandler)
}

EventBus 定义事件总线接口

type EventHandler

type EventHandler func(event Event)

EventHandler 事件处理函数类型

type Module

type Module interface {
	// ID 返回模块的唯一标识符,如 "files", "users"
	ID() string

	// Name 返回模块的显示名称
	Name() string

	// Version 返回模块版本
	Version() string

	// Dependencies 返回此模块依赖的其他模块 ID 列表
	// 模块加载器会确保依赖的模块先初始化
	Dependencies() []string

	// Init 初始化模块,传入核心上下文
	// 在此阶段应该:创建数据库表、初始化内部状态
	Init(ctx *Context) error

	// Start 启动模块
	// 在所有模块 Init 完成后调用
	// 在此阶段可以:启动后台任务、订阅事件
	Start() error

	// Stop 停止模块
	// 在应用关闭时调用
	// 在此阶段应该:清理资源、停止后台任务
	Stop() error

	// RegisterRoutes 注册模块的 HTTP 路由
	// router 是带有 /api/v1 前缀的路由组
	RegisterRoutes(router *gin.RouterGroup)
}

Module 定义了所有功能模块必须实现的接口

type ModuleInfo

type ModuleInfo struct {
	ID           string   `json:"id"`
	Name         string   `json:"name"`
	Version      string   `json:"version"`
	Dependencies []string `json:"dependencies"`
	Status       string   `json:"status"` // "stopped", "starting", "running", "error"
}

ModuleInfo 模块基本信息(用于不需要完整 Module 接口的场景)

type OptionalModule

type OptionalModule interface {
	Module

	// IsOptional 返回模块是否可被用户禁用
	// 核心模块返回 false,可选模块返回 true
	IsOptional() bool

	// Description 返回模块描述
	Description() string

	// DefaultConfig 返回模块的默认配置定义
	// 用于前端渲染配置表单
	DefaultConfig() []ConfigField
}

OptionalModule 可选模块扩展接口 可选模块应该实现此接口以提供额外的元信息

type Registry

type Registry struct {
	// contains filtered or unexported fields
}

Registry 是模块注册中心,负责管理所有模块的生命周期

func NewRegistry

func NewRegistry(db *gorm.DB, config ConfigProvider, eventBus EventBus, logger *zap.Logger, extra map[string]interface{}) *Registry

NewRegistry 创建新的模块注册中心 extra 参数用于传递额外的组件(如 TokenManager)

func (*Registry) Get

func (r *Registry) Get(id string) Module

Get 获取已注册的模块

func (*Registry) GetAll

func (r *Registry) GetAll() []ModuleInfo

GetAll 获取所有已注册模块的信息

func (*Registry) Register

func (r *Registry) Register(m Module) error

Register 注册一个模块 注意:只能在 Start 调用前注册模块

func (*Registry) RegisterRoutes

func (r *Registry) RegisterRoutes(router *gin.RouterGroup)

RegisterRoutes 注册所有模块的路由

func (*Registry) Start

func (r *Registry) Start() error

Start 按依赖顺序初始化并启动所有模块

func (*Registry) Stop

func (r *Registry) Stop() error

Stop 按依赖逆序停止所有模块

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL