add config

This commit is contained in:
Fran Jurmanović
2024-09-30 23:08:06 +02:00
parent 86804e04d2
commit f5a7749ad9
9 changed files with 79 additions and 27 deletions

View File

@@ -1,6 +1,7 @@
package db
import (
"log"
"os"
"rockhu-bot/local/model"
"time"
@@ -11,8 +12,9 @@ import (
)
func Start(di *dig.Container) {
dbConn := os.Getenv("DB_CONNECTION")
db, err := gorm.Open(postgres.New(postgres.Config{
DSN: os.Getenv("CONNECTION_STRINGS"),
DSN: dbConn,
PreferSimpleProtocol: true,
}), &gorm.Config{
NowFunc: func() time.Time {
@@ -20,13 +22,13 @@ func Start(di *dig.Container) {
return time.Now().In(utc)
}})
if err != nil {
panic("failed to connect database")
log.Panic("failed to connect database")
}
err = di.Provide(func() *gorm.DB {
return db
})
if err != nil {
panic("failed to bind database")
log.Panic("failed to bind database")
}
Migrate(db)
}
@@ -34,6 +36,6 @@ func Start(di *dig.Container) {
func Migrate(db *gorm.DB) {
err := db.AutoMigrate(&model.ConcertModel{})
if err != nil {
panic("failed to migrate model.ApiModel")
log.Panic("failed to migrate model.ApiModel")
}
}