generated from LucasCarioca/go-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
datasource.go
40 lines (36 loc) · 1.21 KB
/
datasource.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
package datasource
import (
"fmt"
"github.com/LucasCarioca/wedding-registration-services/pkg/models"
"github.com/spf13/viper"
"gorm.io/driver/postgres"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
)
var db *gorm.DB
// Init initializes the database connection
func Init(config *viper.Viper) {
var err error
driver := config.GetString("data_source.driver")
if driver == "sqlite" {
db, err = gorm.Open(sqlite.Open("test.db"), &gorm.Config{})
} else if driver == "postgres" {
host := config.GetString("data_source.host")
user := config.GetString("data_source.username")
password := config.GetString("data_source.password")
dbname := config.GetString("data_source.database")
port := config.GetString("data_source.port")
sslMode := config.GetString("data_source.ssl_mode")
timeZone := config.GetString("data_source.time_zone")
dsn := fmt.Sprintf("host=%s user=%s password=%s dbname=%s port=%s sslmode=%s TimeZone=%s", host, user, password, dbname, port, sslMode, timeZone)
db, err = gorm.Open(postgres.Open(dsn), &gorm.Config{})
}
if err != nil {
panic("failed to connect database")
}
models.Init(db)
}
// GetDataSource gets the instance of the database connection
func GetDataSource() *gorm.DB {
return db
}