-
Notifications
You must be signed in to change notification settings - Fork 0
/
default.go
56 lines (48 loc) · 1.64 KB
/
default.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
51
52
53
54
55
56
package models
import (
"fmt"
// mysql functionality
_ "github.com/go-sql-driver/mysql"
"github.com/jinzhu/gorm"
// postgresql funcationality
_ "github.com/lib/pq"
// sqlite3 functionality
_ "github.com/mattn/go-sqlite3"
"github.com/robxu9/kahinah/common/conf"
"github.com/robxu9/kahinah/common/klog"
)
var (
DBPrefix = conf.GetDefault("database.prefix", "kh_").(string)
DBType = conf.GetDefault("database.type", "sqlite3").(string)
DBName = conf.GetDefault("database.name", "data.sqlite").(string)
DBHost = conf.GetDefault("database.host", "localhost:3306").(string)
DBUser = conf.GetDefault("database.user", "root").(string)
DBPass = conf.GetDefault("database.pass", "toor").(string)
DBDebug = conf.GetDefault("database.debug", false).(bool)
DB *gorm.DB
)
func init() {
var err error
switch DBType {
case "mysql":
DB, err = gorm.Open("mysql", fmt.Sprintf("%s:%s@tcp(%s)/%s", DBUser, DBPass, DBHost, DBName))
case "sqlite3":
DB, err = gorm.Open("sqlite3", "file:"+DBName)
case "postgres":
DB, err = gorm.Open("postgres", fmt.Sprintf("postgres://%s:%s@%s/%s", DBUser, DBPass, DBHost, DBName))
default:
klog.Fatalf("db: I don't know how to handle %v", DBType)
}
DB.LogMode(DBDebug)
DB.SetLogger(gorm.Logger{LogWriter: klog.Logger})
if err = DB.DB().Ping(); err != nil {
klog.Fatalf("db: couldn't ping the database: %v", err)
}
DB.DB().SetMaxIdleConns(10)
DB.DB().SetMaxOpenConns(100)
if err = DB.AutoMigrate(&Advisory{}, &List{}, &ListActivity{},
&ListArtifact{}, &ListLink{}, &ListStage{}, &ListStageProcess{},
&User{}, &UserPermission{}).Error; err != nil {
klog.Fatalf("db: failed to automigrate: %v", err)
}
}