18c9354095
- 注入数据库、运行时配置、统一响应、文件存储和平台 AI 能力,补充业务读写工具与客户快捷操作契约。 - 移除模块内重复的组织、客户、工单、标签、技能、旧工作流、MCP 和迁移实现,将身份权限与业务主体交由宿主管理。 - 使用 libSQL 重构向量存储,并完善图片消息、访客身份、排队调度、企业微信和支持聊天页面。 - 统一 HTTP、DTO 与 WebSocket 的 snake_case 协议,补齐模块初始化、业务动作和公共载荷等回归测试。
103 lines
2.5 KiB
Go
103 lines
2.5 KiB
Go
package bootstrap
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
"os"
|
|
"strings"
|
|
"time"
|
|
|
|
"code.tczkiot.com/wlw/ai-agent/internal/pkg/config"
|
|
|
|
"github.com/mlogclub/simple/sqls"
|
|
"gorm.io/driver/postgres"
|
|
"gorm.io/gorm"
|
|
|
|
// "gorm.io/gorm/logger"
|
|
"gorm.io/gorm/logger"
|
|
"gorm.io/gorm/schema"
|
|
)
|
|
|
|
func InitDB(cfg config.DBConfig) (*gorm.DB, error) {
|
|
dialector, err := newDialector(cfg)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
db, err := gorm.Open(dialector, &gorm.Config{
|
|
Logger: logger.New(
|
|
log.New(os.Stdout, "\r\n", log.LstdFlags),
|
|
logger.Config{
|
|
SlowThreshold: time.Second,
|
|
LogLevel: logger.Warn,
|
|
IgnoreRecordNotFoundError: true,
|
|
Colorful: true,
|
|
},
|
|
),
|
|
NamingStrategy: schema.NamingStrategy{
|
|
TablePrefix: "t_",
|
|
SingularTable: true,
|
|
},
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
sqlDB, err := db.DB()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if cfg.MaxIdleConns > 0 {
|
|
sqlDB.SetMaxIdleConns(cfg.MaxIdleConns)
|
|
}
|
|
if cfg.MaxOpenConns > 0 {
|
|
sqlDB.SetMaxOpenConns(cfg.MaxOpenConns)
|
|
}
|
|
if cfg.ConnMaxIdleTimeSeconds > 0 {
|
|
sqlDB.SetConnMaxIdleTime(time.Duration(cfg.ConnMaxIdleTimeSeconds) * time.Second)
|
|
}
|
|
if cfg.ConnMaxLifetimeSeconds > 0 {
|
|
sqlDB.SetConnMaxLifetime(time.Duration(cfg.ConnMaxLifetimeSeconds) * time.Second)
|
|
}
|
|
|
|
sqls.SetDB(db)
|
|
return db, nil
|
|
}
|
|
|
|
// UseDatabase scopes the host application's connection to AI Agent tables.
|
|
// The connection pool is shared, while the naming strategy is copied so the
|
|
// host database naming rules remain untouched.
|
|
func UseDatabase(database *gorm.DB, tablePrefix string) error {
|
|
moduleDB, err := ScopedDatabase(database, tablePrefix)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
sqls.SetDB(moduleDB)
|
|
return nil
|
|
}
|
|
|
|
// ScopedDatabase shares the host connection pool while applying the AI Agent
|
|
// table prefix to an isolated GORM session.
|
|
func ScopedDatabase(database *gorm.DB, tablePrefix string) (*gorm.DB, error) {
|
|
if database == nil {
|
|
return nil, fmt.Errorf("database is required")
|
|
}
|
|
moduleDB := database.Session(&gorm.Session{NewDB: true})
|
|
moduleConfig := *moduleDB.Config
|
|
moduleConfig.NamingStrategy = schema.NamingStrategy{
|
|
TablePrefix: tablePrefix,
|
|
SingularTable: true,
|
|
}
|
|
moduleDB.Config = &moduleConfig
|
|
return moduleDB, nil
|
|
}
|
|
|
|
func newDialector(cfg config.DBConfig) (gorm.Dialector, error) {
|
|
switch strings.ToLower(strings.TrimSpace(cfg.Type)) {
|
|
case "postgres", "postgresql":
|
|
return postgres.Open(cfg.DSN), nil
|
|
default:
|
|
return nil, fmt.Errorf("unsupported db type %q: only postgres is supported", cfg.Type)
|
|
}
|
|
}
|