-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.go
46 lines (41 loc) · 1.17 KB
/
database.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package database
import (
"fmt"
"log"
"github.com/joho/godotenv"
"gorm.io/driver/mysql"
"gorm.io/gorm"
)
// GlobalDB is a global db object that will be used across different packages
var GlobalDB *gorm.DB
// InitDatabase creates a mysql db connecntion and stores it in the GlobalDB variable
// It reads the environment variables from the .env file and uses them to create the connection
// It returns an error if the connection fails
func InitDatabase() (err error) {
// Read the environment variables from the .env file
config, err := godotenv.Read()
if err != nil {
log.Fatal("Error reading .env file")
}
// Get database connection details from environment variables
dbHost := config["DB_HOST"]
dbPort := config["DB_PORT"]
dbUser := config["DB_USER"]
dbPass := config["DB_PASS"]
dbName := config["DB_NAME"]
// Creatre the data source name (DSN) using the environment variables
dsn := fmt.Sprintf(
"%s:%s@tcp(%s:%s)/%s?charset=utf8&parseTime=True&loc=Local",
dbUser,
dbPass,
dbHost,
dbPort,
dbName,
)
// Create the connection and store it in the GlobalDB variable
GlobalDB, err = gorm.Open(mysql.Open(dsn), &gorm.Config{})
if err != nil {
return
}
return
}