graph TB
A[核心系统] --> B[插件管理器]
B --> C[渠道插件]
B --> D[认证插件]
B --> E[存储插件]
B --> F[监控插件]
G[配置系统] --> H[热更新]
G --> I[环境配置]
G --> J[功能开关]
K[主题系统] --> L[UI组件]
K --> M[样式定制]
K --> N[布局配置]
O[扩展API] --> P[Webhook]
O --> Q[自定义端点]
O --> R[数据导出]
package plugin
import (
"context"
"encoding/json"
)
// 插件接口
type Plugin interface {
// 插件名称
Name() string
// 插件版本
Version() string
// 插件描述
Description() string
// 初始化插件
Initialize(config map[string]interface{}) error
// 启动插件
Start(ctx context.Context) error
// 停止插件
Stop() error
// 插件状态
Status() PluginStatus
}
// 插件状态
type PluginStatus int
const (
PluginStatusStopped PluginStatus = iota
PluginStatusStarting
PluginStatusRunning
PluginStatusStopping
PluginStatusError
)
func (ps PluginStatus) String() string {
switch ps {
case PluginStatusStopped:
return "stopped"
case PluginStatusStarting:
return "starting"
case PluginStatusRunning:
return "running"
case PluginStatusStopping:
return "stopping"
case PluginStatusError:
return "error"
default:
return "unknown"
}
}
// 渠道插件接口
type ChannelPlugin interface {
Plugin
// 支持的模型列表
SupportedModels() []string
// 发送聊天请求
ChatCompletion(ctx context.Context, request *ChatRequest) (*ChatResponse, error)
// 计算配额
CalculateQuota(request *ChatRequest, response *ChatResponse) int64
// 健康检查
HealthCheck(ctx context.Context) error
}
// 聊天请求
type ChatRequest struct {
Model string `json:"model"`
Messages []Message `json:"messages"`
Temperature float64 `json:"temperature,omitempty"`
MaxTokens int `json:"max_tokens,omitempty"`
Stream bool `json:"stream,omitempty"`
}
// 聊天响应
type ChatResponse struct {
ID string `json:"id"`
Object string `json:"object"`
Created int64 `json:"created"`
Model string `json:"model"`
Choices []Choice `json:"choices"`
Usage Usage `json:"usage"`
}
// 消息
type Message struct {
Role string `json:"role"`
Content string `json:"content"`
}
// 选择
type Choice struct {
Index int `json:"index"`
Message Message `json:"message"`
FinishReason string `json:"finish_reason"`
}
// 使用情况
type Usage struct {
PromptTokens int `json:"prompt_tokens"`
CompletionTokens int `json:"completion_tokens"`
TotalTokens int `json:"total_tokens"`
}
// 认证插件接口
type AuthPlugin interface {
Plugin
// 验证用户
Authenticate(ctx context.Context, token string) (*User, error)
// 获取用户权限
GetPermissions(ctx context.Context, userID int) ([]string, error)
// 刷新令牌
RefreshToken(ctx context.Context, refreshToken string) (*TokenPair, error)
}
// 用户信息
type User struct {
ID int `json:"id"`
Username string `json:"username"`
Email string `json:"email"`
Role string `json:"role"`
Status int `json:"status"`
}
// 令牌对
type TokenPair struct {
AccessToken string `json:"access_token"`
RefreshToken string `json:"refresh_token"`
ExpiresIn int64 `json:"expires_in"`
}
// 存储插件接口
type StoragePlugin interface {
Plugin
// 存储数据
Store(ctx context.Context, key string, value interface{}) error
// 获取数据
Get(ctx context.Context, key string, dest interface{}) error
// 删除数据
Delete(ctx context.Context, key string) error
// 检查键是否存在
Exists(ctx context.Context, key string) (bool, error)
// 设置过期时间
SetExpiration(ctx context.Context, key string, expiration int64) error
}
package plugin
import (
"context"
"fmt"
"log"
"sync"
"time"
)
// 插件管理器
type Manager struct {
plugins map[string]Plugin
mutex sync.RWMutex
ctx context.Context
cancel context.CancelFunc
}
// 创建插件管理器
func NewManager() *Manager {
ctx, cancel := context.WithCancel(context.Background())
return &Manager{
plugins: make(map[string]Plugin),
ctx: ctx,
cancel: cancel,
}
}
// 注册插件
func (pm *Manager) Register(plugin Plugin) error {
pm.mutex.Lock()
defer pm.mutex.Unlock()
name := plugin.Name()
if _, exists := pm.plugins[name]; exists {
return fmt.Errorf("plugin %s already registered", name)
}
pm.plugins[name] = plugin
log.Printf("Plugin %s v%s registered", name, plugin.Version())
return nil
}
// 卸载插件
func (pm *Manager) Unregister(name string) error {
pm.mutex.Lock()
defer pm.mutex.Unlock()
plugin, exists := pm.plugins[name]
if !exists {
return fmt.Errorf("plugin %s not found", name)
}
// 停止插件
if plugin.Status() == PluginStatusRunning {
if err := plugin.Stop(); err != nil {
return fmt.Errorf("failed to stop plugin %s: %v", name, err)
}
}
delete(pm.plugins, name)
log.Printf("Plugin %s unregistered", name)
return nil
}
// 启动插件
func (pm *Manager) Start(name string, config map[string]interface{}) error {
pm.mutex.RLock()
plugin, exists := pm.plugins[name]
pm.mutex.RUnlock()
if !exists {
return fmt.Errorf("plugin %s not found", name)
}
if plugin.Status() == PluginStatusRunning {
return fmt.Errorf("plugin %s is already running", name)
}
// 初始化插件
if err := plugin.Initialize(config); err != nil {
return fmt.Errorf("failed to initialize plugin %s: %v", name, err)
}
// 启动插件
if err := plugin.Start(pm.ctx); err != nil {
return fmt.Errorf("failed to start plugin %s: %v", name, err)
}
log.Printf("Plugin %s started", name)
return nil
}
// 停止插件
func (pm *Manager) Stop(name string) error {
pm.mutex.RLock()
plugin, exists := pm.plugins[name]
pm.mutex.RUnlock()
if !exists {
return fmt.Errorf("plugin %s not found", name)
}
if plugin.Status() != PluginStatusRunning {
return fmt.Errorf("plugin %s is not running", name)
}
if err := plugin.Stop(); err != nil {
return fmt.Errorf("failed to stop plugin %s: %v", name, err)
}
log.Printf("Plugin %s stopped", name)
return nil
}
// 获取插件
func (pm *Manager) Get(name string) (Plugin, error) {
pm.mutex.RLock()
defer pm.mutex.RUnlock()
plugin, exists := pm.plugins[name]
if !exists {
return nil, fmt.Errorf("plugin %s not found", name)
}
return plugin, nil
}
// 获取所有插件
func (pm *Manager) List() map[string]Plugin {
pm.mutex.RLock()
defer pm.mutex.RUnlock()
result := make(map[string]Plugin)
for name, plugin := range pm.plugins {
result[name] = plugin
}
return result
}
// 获取渠道插件
func (pm *Manager) GetChannelPlugin(name string) (ChannelPlugin, error) {
plugin, err := pm.Get(name)
if err != nil {
return nil, err
}
channelPlugin, ok := plugin.(ChannelPlugin)
if !ok {
return nil, fmt.Errorf("plugin %s is not a channel plugin", name)
}
return channelPlugin, nil
}
// 获取认证插件
func (pm *Manager) GetAuthPlugin(name string) (AuthPlugin, error) {
plugin, err := pm.Get(name)
if err != nil {
return nil, err
}
authPlugin, ok := plugin.(AuthPlugin)
if !ok {
return nil, fmt.Errorf("plugin %s is not an auth plugin", name)
}
return authPlugin, nil
}
// 获取存储插件
func (pm *Manager) GetStoragePlugin(name string) (StoragePlugin, error) {
plugin, err := pm.Get(name)
if err != nil {
return nil, err
}
storagePlugin, ok := plugin.(StoragePlugin)
if !ok {
return nil, fmt.Errorf("plugin %s is not a storage plugin", name)
}
return storagePlugin, nil
}
// 启动所有插件
func (pm *Manager) StartAll(configs map[string]map[string]interface{}) error {
pm.mutex.RLock()
plugins := make([]Plugin, 0, len(pm.plugins))
for _, plugin := range pm.plugins {
plugins = append(plugins, plugin)
}
pm.mutex.RUnlock()
for _, plugin := range plugins {
name := plugin.Name()
config := configs[name]
if config == nil {
config = make(map[string]interface{})
}
if err := pm.Start(name, config); err != nil {
log.Printf("Failed to start plugin %s: %v", name, err)
}
}
return nil
}
// 停止所有插件
func (pm *Manager) StopAll() error {
pm.mutex.RLock()
plugins := make([]Plugin, 0, len(pm.plugins))
for _, plugin := range pm.plugins {
plugins = append(plugins, plugin)
}
pm.mutex.RUnlock()
for _, plugin := range plugins {
name := plugin.Name()
if err := pm.Stop(name); err != nil {
log.Printf("Failed to stop plugin %s: %v", name, err)
}
}
pm.cancel()
return nil
}
// 健康检查
func (pm *Manager) HealthCheck() map[string]error {
pm.mutex.RLock()
defer pm.mutex.RUnlock()
results := make(map[string]error)
for name, plugin := range pm.plugins {
if plugin.Status() != PluginStatusRunning {
results[name] = fmt.Errorf("plugin is not running")
continue
}
// 如果是渠道插件,执行健康检查
if channelPlugin, ok := plugin.(ChannelPlugin); ok {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
err := channelPlugin.HealthCheck(ctx)
cancel()
results[name] = err
} else {
results[name] = nil
}
}
return results
}
graph TB
A[主题管理器] --> B[主题加载器]
A --> C[样式生成器]
A --> D[资源管理器]
B --> E[主题配置文件]
B --> F[默认主题]
B --> G[自定义主题]
C --> H[CSS生成]
C --> I[JavaScript生成]
C --> J[变量注入]
D --> K[静态资源]
D --> L[字体文件]
D --> M[图片资源]
N[用户请求] --> O[主题中间件]
O --> P[主题选择]
P --> Q[样式应用]
Q --> R[页面渲染]
graph TB
A[扩展管理器] --> B[扩展点注册]
A --> C[扩展实现管理]
A --> D[执行调度]
B --> E[认证扩展点]
B --> F[请求处理扩展点]
B --> G[响应处理扩展点]
B --> H[日志扩展点]
B --> I[监控扩展点]
C --> J[扩展实现1]
C --> K[扩展实现2]
C --> L[扩展实现N]
D --> M[优先级排序]
D --> N[链式执行]
D --> O[结果聚合]
P[业务请求] --> Q[扩展点触发]
Q --> R[扩展执行]
R --> S[结果返回]
flowchart TD
A[API扩展系统] --> B[端点注册器]
A --> C[路由管理器]
A --> D[中间件链]
A --> E[版本控制]
B --> F[自定义端点]
B --> G[端点验证]
B --> H[权限配置]
C --> I[动态路由]
C --> J[路径匹配]
C --> K[参数解析]
D --> L[认证中间件]
D --> M[限流中间件]
D --> N[日志中间件]
D --> O[监控中间件]
E --> P[API版本v1]
E --> Q[API版本v2]
E --> R[向后兼容]
S[客户端请求] --> T[路由匹配]
T --> U[中间件处理]
U --> V[端点执行]
V --> W[响应返回]