3d47227fbd
Remove embedded frontend and workflow editor assets, add standalone API deployment configuration, and retain the current backend service updates.
119 lines
2.5 KiB
Go
119 lines
2.5 KiB
Go
package bootstrap
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"time"
|
|
|
|
"agent-desk/internal/pkg/config"
|
|
|
|
"github.com/mlogclub/simple/sqls"
|
|
"gorm.io/driver/mysql"
|
|
"gorm.io/driver/postgres"
|
|
|
|
// "gorm.io/driver/sqlite" // Sqlite driver based on CGO
|
|
"github.com/glebarez/sqlite" // Pure go SQLite driver, checkout https://github.com/glebarez/sqlite for details
|
|
"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
|
|
}
|
|
|
|
func newDialector(cfg config.DBConfig) (gorm.Dialector, error) {
|
|
switch strings.ToLower(strings.TrimSpace(cfg.Type)) {
|
|
case "sqlite":
|
|
if err := ensureSQLiteDir(cfg.DSN); err != nil {
|
|
return nil, err
|
|
}
|
|
return sqlite.Open(cfg.DSN), nil
|
|
case "mysql":
|
|
return mysql.Open(cfg.DSN), nil
|
|
case "postgres", "postgresql":
|
|
return postgres.Open(cfg.DSN), nil
|
|
default:
|
|
return nil, fmt.Errorf("unsupported db type: %s", cfg.Type)
|
|
}
|
|
}
|
|
|
|
func ensureSQLiteDir(dsn string) error {
|
|
dbPath := sqliteFilePath(dsn)
|
|
if dbPath == "" {
|
|
return nil
|
|
}
|
|
dir := filepath.Dir(dbPath)
|
|
if dir == "." || dir == "" {
|
|
return nil
|
|
}
|
|
return os.MkdirAll(dir, 0o755)
|
|
}
|
|
|
|
func sqliteFilePath(dsn string) string {
|
|
if dsn == "" {
|
|
return ""
|
|
}
|
|
|
|
path := dsn
|
|
if after, ok := strings.CutPrefix(path, "file:"); ok {
|
|
path = after
|
|
}
|
|
if idx := strings.Index(path, "?"); idx >= 0 {
|
|
path = path[:idx]
|
|
}
|
|
|
|
normalized := strings.TrimSpace(path)
|
|
if normalized == "" || normalized == ":memory:" || strings.Contains(normalized, "mode=memory") {
|
|
return ""
|
|
}
|
|
return normalized
|
|
}
|