2fas-server/internal/common/db/gorm.go

44 lines
834 B
Go
Raw Permalink Normal View History

2022-12-31 10:22:38 +01:00
package db
import (
"fmt"
2023-05-29 20:03:01 +02:00
2023-01-30 19:59:42 +01:00
"github.com/twofas/2fas-server/config"
2022-12-31 10:22:38 +01:00
"gorm.io/gorm/logger"
gosql "github.com/go-sql-driver/mysql"
"gorm.io/driver/mysql"
"gorm.io/gorm"
)
func NewGormConnection(conf config.Configuration) *gorm.DB {
cfg := &gosql.Config{
Net: "tcp",
Addr: fmt.Sprintf("%v:%v", conf.Db.Host, conf.Db.Port),
DBName: conf.Db.Database,
User: conf.Db.Username,
Passwd: conf.Db.Password,
AllowNativePasswords: true,
ParseTime: true,
}
var logLevel logger.LogLevel
if conf.Debug {
logLevel = logger.Info
} else {
logLevel = logger.Error
}
conn, err := gorm.Open(mysql.Open(cfg.FormatDSN()), &gorm.Config{
Logger: logger.Default.LogMode(logLevel),
})
if err != nil {
panic(err)
}
return conn
}