feat: Go 重写后端,替换 Python FastAPI
用 Go (Gin + GORM + SQLite) 重写整个后端: - 单二进制部署,不依赖 Python/pip/SDK - net/http 原生客户端,无 Cloudflare TLS 指纹问题 - 多阶段 Dockerfile:Node 构建前端 + Go 构建后端 + Alpine 运行 - 内存占用从 ~95MB 降至 ~3MB - 完整保留所有 API 路由、JWT 认证、API Key 权限、审计日志
This commit is contained in:
46
models/db.go
Normal file
46
models/db.go
Normal file
@@ -0,0 +1,46 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"airwallex-admin/config"
|
||||
|
||||
"github.com/glebarez/sqlite"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
var DB *gorm.DB
|
||||
|
||||
func InitDB() *gorm.DB {
|
||||
dbPath := config.Cfg.DatabaseURL
|
||||
|
||||
// Ensure the directory exists
|
||||
dir := filepath.Dir(dbPath)
|
||||
if dir != "." && dir != "" {
|
||||
if err := os.MkdirAll(dir, 0755); err != nil {
|
||||
log.Fatalf("failed to create database directory: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
db, err := gorm.Open(sqlite.Open(dbPath), &gorm.Config{})
|
||||
if err != nil {
|
||||
log.Fatalf("failed to connect to database: %v", err)
|
||||
}
|
||||
|
||||
// Create tables only if they don't exist (skip AutoMigrate to avoid
|
||||
// SQLite ALTER TABLE issues with existing Python-created schemas)
|
||||
migrator := db.Migrator()
|
||||
models := []interface{}{&SystemSetting{}, &ApiToken{}, &CardLog{}, &AuditLog{}}
|
||||
for _, model := range models {
|
||||
if !migrator.HasTable(model) {
|
||||
if err := migrator.CreateTable(model); err != nil {
|
||||
log.Fatalf("failed to create table: %v", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
DB = db
|
||||
return db
|
||||
}
|
||||
Reference in New Issue
Block a user