tlm-login-server/internal/app/db.go

35 lines
583 B
Go

package app
import (
"code.lila.network/lauralani/tlm-login-server/internal/models"
"github.com/glebarez/sqlite"
//"gorm.io/driver/sqlite" with CGo
"gorm.io/gorm"
"log/slog"
"os"
)
func InitializeDB() error {
var err error
var db *gorm.DB
var dbpath = os.Getenv("TLM_SQLITE_PATH")
db, err = gorm.Open(sqlite.Open(dbpath), &gorm.Config{})
if err != nil {
slog.Error("Can't open DB")
os.Exit(1)
}
models.DB = db
err = models.DB.AutoMigrate(&models.User{})
if err != nil {
slog.Error("Can't do DB Migration", "Model", "User")
return err
}
return nil
}