0d6c8c7989
CI / test (push) Failing after 13m11s
This is the complete fix for the missing /admin/ route and frontend serving: Backend: - internal/web/web.go: new package with go:embed for web/dist/ - internal/api/router.go: add routes for /admin/, /admin/*, /assets/* - internal/db/db.go: fix SQLite DSN parsing (sqlite:///path -> path) Build system: - Makefile: new 'embed-prep' target copies web/dist to internal/web/dist - make build now runs embed-prep -> frontend/build automatically Deployment: - deploy/llamalink.service: remove invalid --host/--port flags, add EnvironmentFile=/etc/llamalink/env Verified: - /health returns 200 - /admin/ serves Vue SPA HTML - /assets/* serves CSS and JS files from embedded FS - sqlite:///./llamalink.db works correctly
55 lines
1000 B
Go
55 lines
1000 B
Go
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 := cfg.DatabaseURL
|
|
if strings.HasPrefix(dsn, "sqlite://") {
|
|
dsn = strings.TrimPrefix(dsn, "sqlite://")
|
|
if strings.HasPrefix(dsn, "/") {
|
|
dsn = dsn[1:]
|
|
}
|
|
}
|
|
|
|
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{},
|
|
)
|
|
}
|