Skip to content

Commit bd6c31d

Browse files
author
XieBiao
committed
dynamic config path
1 parent 5ca7ba7 commit bd6c31d

File tree

4 files changed

+80
-82
lines changed

4 files changed

+80
-82
lines changed

config/db.go

Lines changed: 0 additions & 36 deletions
This file was deleted.

core/db.go

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package core
2+
3+
import (
4+
"github.com/go-xorm/xorm"
5+
"os"
6+
"runtime"
7+
"path"
8+
"github.com/BurntSushi/toml"
9+
"errors"
10+
xcore "github.com/go-xorm/core"
11+
)
12+
13+
type DB struct {
14+
Cfg Connection
15+
Engine *xorm.Engine
16+
}
17+
18+
type Connection struct {
19+
Driver string
20+
Dsn string
21+
Log bool
22+
LogLevel int
23+
LogFile string
24+
}
25+
26+
type Connections struct {
27+
Connections map[string]Connection
28+
}
29+
30+
func GetDbConfigs() map[string]Connection {
31+
var conns Connections
32+
_, filename, _, _ := runtime.Caller(1)
33+
cfg := path.Join(path.Dir(filename), "/../config/db.toml")
34+
if _, err := toml.DecodeFile(cfg, &conns); err != nil {
35+
panic(errors.New("parse db.toml fail: " + err.Error()))
36+
}
37+
return conns.Connections
38+
}
39+
40+
func GetDbConfig(id string) Connection {
41+
conns := GetDbConfigs()
42+
conn, err := conns[id]
43+
if !err {
44+
panic(errors.New("connection " + id + " is not available"))
45+
}
46+
return conn
47+
}
48+
49+
func NewDB(conn string) *DB {
50+
cfg := GetDbConfig(conn)
51+
db := &DB{cfg, nil}
52+
53+
var err error
54+
db.Engine, err = xorm.NewEngine(cfg.Driver, cfg.Dsn)
55+
if err != nil {
56+
panic(err)
57+
}
58+
59+
if cfg.Log {
60+
//log into file
61+
if len(cfg.LogFile) > 0 {
62+
f, err := os.OpenFile(cfg.LogFile, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0666)
63+
if err != nil {
64+
panic(err)
65+
}
66+
db.Engine.SetLogger(xorm.NewSimpleLogger(f))
67+
} //else log into console
68+
69+
db.Engine.ShowSQL(true)
70+
db.Engine.ShowExecTime(true)
71+
db.Engine.Logger().SetLevel(xcore.LogLevel(cfg.LogLevel))
72+
}
73+
74+
return db
75+
}

core/model/db.go

Lines changed: 0 additions & 41 deletions
This file was deleted.

models/base.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,21 @@ package models
33
import (
44
_ "github.com/go-sql-driver/mysql" //remember: import xxx driver to init
55
"github.com/go-xorm/xorm"
6-
"github.com/hhxsv5/go-db-proxy-api/core/model"
7-
"github.com/go-xorm/core"
6+
xcore "github.com/go-xorm/core"
7+
"github.com/hhxsv5/go-db-proxy-api/core"
88
)
99

1010
const connection = "default" //default connection in file db.toml
1111

1212
var (
13-
Db *model.DB
13+
Db *core.DB
1414
Orm *xorm.Engine
1515
)
1616

1717
func init() {
18-
Db = model.NewDB(connection)
18+
Db = core.NewDB(connection)
1919
Orm = Db.Engine
2020

2121
//...some settings for xorm
22-
Orm.SetMapper(core.GonicMapper{})
22+
Orm.SetMapper(xcore.GonicMapper{})
2323
}

0 commit comments

Comments
 (0)