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

30 lines
626 B
Go
Raw Normal View History

2022-12-31 10:22:38 +01:00
package db
import (
"database/sql"
"fmt"
"github.com/go-sql-driver/mysql"
2023-01-30 19:59:42 +01:00
"github.com/twofas/2fas-server/config"
2022-12-31 10:22:38 +01:00
)
func NewDbConnection(conf config.Configuration) *sql.DB {
cfg := &mysql.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,
MultiStatements: true,
}
conn, err := sql.Open("mysql", cfg.FormatDSN())
if err != nil {
panic(err)
}
return conn
}