-
Notifications
You must be signed in to change notification settings - Fork 17
/
config.go
47 lines (36 loc) · 1.04 KB
/
config.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
47
package student
import (
"github.com/sirupsen/logrus"
"github.com/spf13/viper"
_ "github.com/spo-iitk/ras-backend/config"
"gorm.io/driver/postgres"
"gorm.io/gorm"
"gorm.io/gorm/logger"
)
var db *gorm.DB
func openConnection() {
host := viper.GetString("DATABASE.HOST")
port := viper.GetString("DATABASE.PORT")
password := viper.GetString("DATABASE.PASSWORD")
dbName := viper.GetString("DBNAME.STUDENT")
user := dbName + viper.GetString("DATABASE.USER")
dsn := "host=" + host + " user=" + user + " password=" + password
dsn += " dbname=" + dbName + " port=" + port + " sslmode=disable TimeZone=Asia/Kolkata"
database, err := gorm.Open(postgres.Open(dsn), &gorm.Config{
Logger: logger.Default.LogMode(logger.Info),
})
if err != nil {
logrus.Fatal("Failed to connect to student database: ", err)
panic(err)
}
db = database
err = db.AutoMigrate(&Student{})
if err != nil {
logrus.Fatal("Failed to migrate student database: ", err)
panic(err)
}
logrus.Info("Connected to student database")
}
func init() {
openConnection()
}