-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth-db.go
40 lines (35 loc) · 942 Bytes
/
auth-db.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 webmgmt
import (
"fmt"
"github.com/glebarez/sqlite"
"gorm.io/gorm"
"log"
)
//GetDatabase database connection
func (a *Authr) GetDatabase() *gorm.DB {
connection, err := gorm.Open(sqlite.Open("gorm.db"), &gorm.Config{})
if err != nil {
log.Fatalf("database init err: %v", err)
}
sqldb, err := connection.DB()
if err != nil {
log.Fatalf("database connection err: %v", err)
}
err = sqldb.Ping()
if err != nil {
log.Fatalf("database ping err: %v", err)
}
fmt.Println("Database connection successful.")
return connection
}
//InitialMigration user table in userdb
func (a *Authr) InitialMigration() {
connection := a.GetDatabase()
defer a.CloseDatabase(connection)
connection.AutoMigrate(User{})
}
//CloseDatabase database connection
func (a *Authr) CloseDatabase(connection *gorm.DB) {
sqldb, _ := connection.DB()
sqldb.Close()
}