first commit

This commit is contained in:
William Bouzourène 2024-12-20 19:58:37 +01:00
commit 7cec3bb263
Signed by: bouzoure
SSH key fingerprint: SHA256:19MbXpLua4rUtk8tunMesD8KUKb91LXLHg8E/qTooww
27 changed files with 597 additions and 0 deletions

39
helpers/database.go Normal file
View file

@ -0,0 +1,39 @@
package helpers
import (
"log"
"git.readonly.ch/bouzoure/popvaud-people/models"
"github.com/glebarez/sqlite"
"gorm.io/gorm"
)
var database *gorm.DB
var connected bool
func GetDatabase() (*gorm.DB, error) {
if connected {
return database, nil
}
return connectDatabase()
}
func connectDatabase() (*gorm.DB, error) {
config, err := GetConfig()
if err != nil {
// TODO: Handle exception
log.Fatal(err)
}
database, err = gorm.Open(
sqlite.Open(config.Database.Location),
&gorm.Config{},
)
database.AutoMigrate(&models.User{})
connected = true
return database, err
}