-
Notifications
You must be signed in to change notification settings - Fork 3
/
db.go
63 lines (57 loc) · 1.55 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
51
52
53
54
55
56
57
58
59
60
61
62
63
package dao
import (
"database/sql"
"log"
"os"
"path/filepath"
"github.com/gomsx/goms/pkg/conf"
e "github.com/gomsx/goms/pkg/err"
_ "github.com/go-sql-driver/mysql" // for init()
)
// dbcfg config of db.
type dbcfg struct {
DSN string `yaml:"dsn"`
}
// getDBConfig get db config from file and env.
func getDBConfig(cfgpath string) (*dbcfg, error) {
var err error
cfg := &dbcfg{}
// file
path := filepath.Join(cfgpath, "mysql.yaml")
if err = conf.GetConf(path, &cfg); err != nil {
log.Printf("get db config file error: %v", err)
} else if cfg.DSN == "" {
log.Printf("get db config file succeeded, but DSN IS EMPTY")
} else {
log.Printf("get db config file succeeded, DSN: %v", "***")
return cfg, nil
}
// env
if dsn, exist := os.LookupEnv("MYSQL_SVC_DSN"); exist == false {
log.Printf("get db config env error: %v", e.ErrNotFoundData)
} else if dsn == "" {
log.Printf("get db config env succeeded, but DSN IS EMPTY")
} else {
log.Printf("get db config env succeeded, DSN: %v", "***")
cfg.DSN = dsn
return cfg, nil
}
return nil, e.ErrNotFoundData
}
// newDB new database and return.
func newDB(cfgpath string) (*sql.DB, func(), error) {
if df, err := getDBConfig(cfgpath); err != nil {
log.Printf("get db config error: %v", err)
return nil, nil, err
} else if db, err := sql.Open("mysql", df.DSN); err != nil {
log.Printf("open db error: %v", err)
return nil, nil, err
} else if err := db.Ping(); err != nil {
log.Printf("ping db error: %v", err)
return nil, nil, err
} else {
return db, func() {
db.Close()
}, nil
}
}