go-backend-starter-project/initializers/database.go

20 lines
303 B
Go
Raw Normal View History

2024-02-15 00:36:17 +01:00
package initializers
import (
"gorm.io/driver/mysql"
"gorm.io/gorm"
"log"
"os"
)
var DB *gorm.DB
func ConnectToDB() {
dsn := os.Getenv("DSN")
var err error
DB, err = gorm.Open(mysql.Open(dsn), &gorm.Config{})
if err != nil {
log.Fatal("Could not connect to database:\n" + err.Error())
}
}