Skip to content

Commit d6f4896

Browse files
author
XieBiao
committed
http server one shot
1 parent c2b8107 commit d6f4896

File tree

14 files changed

+144
-60
lines changed

14 files changed

+144
-60
lines changed

config/http.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
listen = ":9010"
Lines changed: 1 addition & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,12 @@
1-
package core
1+
package db
22

33
import (
4-
"github.com/go-xorm/xorm"
5-
"os"
64
"runtime"
75
"path"
86
"github.com/BurntSushi/toml"
97
"errors"
10-
xcore "github.com/go-xorm/core"
118
)
129

13-
type DB struct {
14-
Cfg Connection
15-
Engine *xorm.Engine
16-
}
17-
1810
type Connection struct {
1911
Driver string
2012
Dsn string
@@ -45,31 +37,3 @@ func GetDbConfig(id string) Connection {
4537
}
4638
return conn
4739
}
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-
}

db/db.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package db
2+
3+
import (
4+
"github.com/go-xorm/xorm"
5+
"os"
6+
"github.com/go-xorm/core"
7+
)
8+
9+
type DB struct {
10+
Cfg Connection
11+
Engine *xorm.Engine
12+
}
13+
14+
func NewDB(conn string) *DB {
15+
cfg := GetDbConfig(conn)
16+
db := &DB{cfg, nil}
17+
18+
var err error
19+
db.Engine, err = xorm.NewEngine(cfg.Driver, cfg.Dsn)
20+
if err != nil {
21+
panic(err)
22+
}
23+
24+
if cfg.Log {
25+
//log into file
26+
if len(cfg.LogFile) > 0 {
27+
f, err := os.OpenFile(cfg.LogFile, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0666)
28+
if err != nil {
29+
panic(err)
30+
}
31+
db.Engine.SetLogger(xorm.NewSimpleLogger(f))
32+
} //else log into console
33+
34+
db.Engine.ShowSQL(true)
35+
db.Engine.ShowExecTime(true)
36+
db.Engine.Logger().SetLevel(core.LogLevel(cfg.LogLevel))
37+
}
38+
39+
return db
40+
}

models/base.go renamed to db/models/base.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ package models
22

33
import (
44
xcore "github.com/go-xorm/core"
5-
"github.com/hhxsv5/go-db-proxy-api/core"
5+
"github.com/hhxsv5/go-db-proxy-api/db"
66
)
77

8-
func NewDB(conn string) *core.DB {
9-
db := core.NewDB(conn)
8+
func NewDB(conn string) *db.DB {
9+
db := db.NewDB(conn)
1010

1111
//...some settings for xorm
1212
db.Engine.SetMapper(xcore.GonicMapper{})

examples/main.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
package main
22

33
import (
4-
"fmt"
5-
"github.com/hhxsv5/go-db-proxy-api/models/mdefault"
4+
"github.com/hhxsv5/go-db-proxy-api"
65
)
76

87
func main() {
9-
user := mdefault.CreateUser("18780207350")
10-
fmt.Println(user)
11-
fmt.Printf("%p", user)
8+
//user := mydefault.CreateUser("18780207350")
9+
//fmt.Println(user)
10+
//fmt.Printf("%p", user)
11+
12+
p := godpa.NewProxy()
13+
p.Run()
1214
}

models/mdb1/base.go renamed to examples/models/db1/base.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
package mdb1
1+
package db1
22

33
import (
44
_ "github.com/go-sql-driver/mysql" //remember: import xxx driver to init
5-
"github.com/hhxsv5/go-db-proxy-api/models"
6-
"github.com/hhxsv5/go-db-proxy-api/core"
5+
"github.com/hhxsv5/go-db-proxy-api/db/models"
6+
mydb "github.com/hhxsv5/go-db-proxy-api/db"
77
"github.com/go-xorm/xorm"
88
)
99

1010
//db1 connection in file db.toml
1111
const conn = "db1"
1212

1313
var (
14-
db *core.DB
14+
db *mydb.DB
1515
orm *xorm.Engine
1616
)
1717

models/mdb2/user.go renamed to examples/models/db1/user.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package mdb2
1+
package db1
22

33
import (
44
"github.com/go-xorm/builder"

models/mdb2/base.go renamed to examples/models/db2/base.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
package mdb2
1+
package db2
22

33
import (
44
_ "github.com/go-sql-driver/mysql" //remember: import xxx driver to init
5-
"github.com/hhxsv5/go-db-proxy-api/models"
6-
"github.com/hhxsv5/go-db-proxy-api/core"
5+
"github.com/hhxsv5/go-db-proxy-api/db/models"
6+
mydb "github.com/hhxsv5/go-db-proxy-api/db"
77
"github.com/go-xorm/xorm"
88
)
99

1010
//db2 connection in file db.toml
1111
const conn = "db2"
1212

1313
var (
14-
db *core.DB
14+
db *mydb.DB
1515
orm *xorm.Engine
1616
)
1717

models/mdb1/user.go renamed to examples/models/db2/user.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package mdb1
1+
package db2
22

33
import (
44
"github.com/go-xorm/builder"

models/mdefault/base.go renamed to examples/models/mydefault/base.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
package mdefault
1+
package mydefault
22

33
import (
44
_ "github.com/go-sql-driver/mysql" //remember: import xxx driver to init
5-
"github.com/hhxsv5/go-db-proxy-api/models"
6-
"github.com/hhxsv5/go-db-proxy-api/core"
75
"github.com/go-xorm/xorm"
6+
"github.com/hhxsv5/go-db-proxy-api/db/models"
7+
mydb "github.com/hhxsv5/go-db-proxy-api/db"
88
)
99

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

1313
var (
14-
db *core.DB
14+
db *mydb.DB
1515
orm *xorm.Engine
1616
)
1717

0 commit comments

Comments
 (0)