/
init.go
42 lines (35 loc) · 1 KB
/
init.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
package db
import (
"database/sql"
"fmt"
_ "github.com/lib/pq"
"gorm.io/driver/postgres"
"gorm.io/gorm"
)
type DbConfig struct {
Host string `yaml:"host"`
Port uint64 `yaml:"port"`
DbName string `yaml:"dbName"`
Username string `yaml:"username"`
Password string `yaml:"password"`
}
func (conf *DbConfig) InitConn() (*Queries, *gorm.DB, *sql.DB, error) {
// driver := "postgres"
connectstring := fmt.Sprintf("host=%s user=%s password=%s dbname=%s port=%d sslmode=disable", conf.Host, conf.Username, conf.Password, conf.DbName, conf.Port)
// Setting up casbin adapter
gormDb, err := gorm.Open(postgres.Open(connectstring), &gorm.Config{})
if err != nil {
return nil, nil, nil, err
}
// Setting up sqlc connection
conn, err := gormDb.DB()
if err != nil {
return nil, nil, nil, err
}
// Pinging the connection to make sure that the db is alive
if err := conn.Ping(); err != nil {
return nil, nil, nil, err
}
db := New(conn)
return db, gormDb, conn, nil
}