/
db.go
50 lines (38 loc) · 1.12 KB
/
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
41
42
43
44
45
46
47
48
49
50
package database
import (
"errors"
"sync"
"github.com/Usable-Security-and-Privacy-Lab/lets-auth-ca/models"
"gorm.io/driver/mysql"
"gorm.io/gorm"
)
// TODO: Import this from the config file, not hard coded.
const dsn = "gorm_test:ThisIsAReallySecureTestPassword@tcp(127.0.0.1:3306)/gorm?charset=utf8mb4&parseTime=True&loc=Local"
var once sync.Once
// Singleton instance
var dbWrapper *DBWrapper
// This wrapper exists solely so that member functions can be written to work on the database.
type DBWrapper struct {
db *gorm.DB
}
// The singleton pattern for a database connection.
func GetDB() (*DBWrapper, error) {
// Singleton pattern
once.Do(func() {
// Open Database Connection
temp_db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{})
if err != nil {
dbWrapper = nil
panic("Cannot connect to database")
}
// Set persistent pointer
dbWrapper = &DBWrapper{db: temp_db}
// Create/Migrate tables from models
dbWrapper.db.AutoMigrate(&models.User{}, &models.Credential{})
})
// Error Check
if dbWrapper == nil {
return nil, errors.New("database connection not initialized")
}
return dbWrapper, nil
}