Initial commit: LlamaLink Go rewrite
Complete rewrite from Python/FastAPI to Go/Gin: - Go backend: auth (API keys + bcrypt), llama.cpp subprocess manager, hot-swap multi-model, rate limiting, quota system, webhooks - Vue 3 SPA admin panel (src/) with Tailwind CSS - Deployment: Docker multi-stage, docker-compose, nginx, systemd - GORM/SQLite models: ApiKey, Model, UsageLog, Quota, Webhook - REST API: /api/v1/admin/* (keys, models, chat, usage, health) - Embedded frontend via go:embed (build output at web/dist/) Removed legacy Python artifacts (app/, tests/, pyproject.toml, etc.)
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"log/slog"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"gorm.io/driver/sqlite"
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/logger"
|
||||
|
||||
"github.com/llamalink/llamalink/internal/config"
|
||||
)
|
||||
|
||||
func Open(cfg *config.Config) (*gorm.DB, error) {
|
||||
dsn := strings.TrimPrefix(cfg.DatabaseURL, "sqlite://")
|
||||
if dsn == cfg.DatabaseURL {
|
||||
dsn = cfg.DatabaseURL
|
||||
}
|
||||
|
||||
gormConfig := &gorm.Config{
|
||||
Logger: logger.Default.LogMode(logger.Silent),
|
||||
}
|
||||
|
||||
db, err := gorm.Open(sqlite.Open(dsn), gormConfig)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
sqlDB, err := db.DB()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
sqlDB.SetMaxOpenConns(cfg.DatabaseMaxOpenConns)
|
||||
sqlDB.SetMaxIdleConns(cfg.DatabaseMaxIdleConns)
|
||||
sqlDB.SetConnMaxLifetime(time.Duration(cfg.DatabaseConnMaxLifetime) * time.Second)
|
||||
|
||||
return db, nil
|
||||
}
|
||||
|
||||
func Migrate(db *gorm.DB) error {
|
||||
slog.Info("running database migrations")
|
||||
return db.AutoMigrate(
|
||||
&ApiKey{},
|
||||
&Model{},
|
||||
&UsageLog{},
|
||||
&Quota{},
|
||||
&Webhook{},
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user