Compare commits
6 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
59018c5269 | ||
|
851e2bd23c | ||
|
40aca1e117 | ||
|
b120742220 | ||
|
6a7da70791 | ||
|
99d09d35b7 |
10
README.md
10
README.md
@ -1,3 +1,13 @@
|
||||
# beegoinit
|
||||
|
||||
统一初始beego mvc 的方法
|
||||
|
||||
|
||||
```go
|
||||
import "golib.gaore.com/GaoreGo/beegoinit"
|
||||
```
|
||||
|
||||
必须显式调用初始化
|
||||
```go
|
||||
db.ReloadByDefault()
|
||||
```
|
27
db/db.go
27
db/db.go
@ -41,10 +41,14 @@ func (d *DbItem) Dsn() string {
|
||||
}
|
||||
|
||||
func init() {
|
||||
Reload()
|
||||
// Reload() 不再自动链接数据库了, 需要自己手动
|
||||
}
|
||||
|
||||
func Reload(dir ...string) {
|
||||
func ReloadByDefault() {
|
||||
Reload(nil)
|
||||
}
|
||||
|
||||
func Reload(files []string, dir ...string) {
|
||||
var err error
|
||||
var configer *grconfig.Configer
|
||||
logger := grlogs.GetAli("sql")
|
||||
@ -61,16 +65,27 @@ func Reload(dir ...string) {
|
||||
dir = append(dir, "conf/")
|
||||
configer = grconfig.New(path.Join(dir...))
|
||||
}
|
||||
dsns, err := GetDsn(configer)
|
||||
dsns, err := GetDsn(configer, files...)
|
||||
if err = LoadXorm(dsns); err != nil {
|
||||
logger.Critical(err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func GetDsn(configer *grconfig.Configer) (DsnList map[string]dsnConfig, err error) {
|
||||
func GetDsn(configer *grconfig.Configer, dbfiles ...string) (DsnList map[string]dsnConfig, err error) {
|
||||
var items map[string]*DbItem
|
||||
DsnList = make(map[string]dsnConfig)
|
||||
err = configer.Item("db", &items)
|
||||
|
||||
if nil == dbfiles || len(dbfiles) == 0 {
|
||||
dbfiles = []string{"db"}
|
||||
}
|
||||
|
||||
for _, dbfile := range dbfiles {
|
||||
|
||||
if dbfile == "" {
|
||||
continue
|
||||
}
|
||||
|
||||
err = configer.Item(dbfile, &items)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
@ -82,5 +97,7 @@ func GetDsn(configer *grconfig.Configer) (DsnList map[string]dsnConfig, err erro
|
||||
}
|
||||
DsnList[alias] = dsnConfig{Dsn: dsn, Driver: driver, Item: v}
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
3
go.mod
3
go.mod
@ -1,6 +1,6 @@
|
||||
module golib.gaore.com/GaoreGo/beegoinit
|
||||
|
||||
go 1.13
|
||||
go 1.15
|
||||
|
||||
require (
|
||||
github.com/OwnLocal/goes v1.0.0 // indirect
|
||||
@ -8,6 +8,7 @@ require (
|
||||
github.com/astaxie/beego v1.12.2
|
||||
github.com/go-check/check v0.0.0-20200227125254-8fa46927fb4f // indirect
|
||||
github.com/go-sql-driver/mysql v1.5.0
|
||||
github.com/lib/pq v1.0.0
|
||||
golib.gaore.com/GaoreGo/cast v1.3.2
|
||||
golib.gaore.com/GaoreGo/goes v1.2.2 // indirect
|
||||
golib.gaore.com/GaoreGo/grconfig v1.1.0
|
||||
|
@ -7,16 +7,40 @@ import (
|
||||
|
||||
const (
|
||||
JsonMsgDefaultOk = "ok"
|
||||
JsonMsgDefaultFailed = "unknown"
|
||||
JsonMsgDefaultFailed = "unknown error"
|
||||
)
|
||||
|
||||
type Json struct {
|
||||
Code int `json:"code"`
|
||||
Data interface{} `json:"data"`
|
||||
Data interface{} `json:"data,omitempty"`
|
||||
Msg string `json:"msg"`
|
||||
Status bool `json:"status"`
|
||||
}
|
||||
|
||||
func (j *Json) GetDefaultSuccessCode() int {
|
||||
return 0
|
||||
}
|
||||
|
||||
func (j *Json) GetDefaultFailedCode() int {
|
||||
return 10086
|
||||
}
|
||||
|
||||
func (j *Json) SetCode(code int) {
|
||||
j.Code = code
|
||||
}
|
||||
|
||||
func (j *Json) SetMessage(msg string) {
|
||||
j.Msg = msg
|
||||
}
|
||||
|
||||
func (j *Json) SetData(data interface{}) {
|
||||
j.Data = data
|
||||
}
|
||||
|
||||
func (j *Json) SetStatus(status bool) {
|
||||
j.Status = status
|
||||
}
|
||||
|
||||
func (j *Json) String() string {
|
||||
s, _ := j.ToString()
|
||||
return s
|
||||
@ -28,15 +52,19 @@ func (j *Json) ToString() (str string, err error) {
|
||||
return bytes.NewBuffer(b).String(), err
|
||||
}
|
||||
|
||||
func NewJsonByDefaultSuccess(data ...interface{}) *Json {
|
||||
func NewJsonByDefaultSuccess(data ...interface{}) JsonInterface {
|
||||
return NewJson(true, 0, JsonMsgDefaultOk, data...)
|
||||
}
|
||||
|
||||
func NewJsonByDefaultFailed(data ...interface{}) *Json {
|
||||
func NewJsonByDefaultFailed(data ...interface{}) JsonInterface {
|
||||
return NewJson(false, 1, JsonMsgDefaultFailed, data...)
|
||||
}
|
||||
|
||||
func NewJson(status bool, code int, msg string, data ...interface{}) *Json {
|
||||
func NewJonsByFailed(code int, msg string) JsonInterface {
|
||||
return NewJson(false, code, msg)
|
||||
}
|
||||
|
||||
func NewJson(status bool, code int, msg string, data ...interface{}) JsonInterface {
|
||||
if len(data) > 0 {
|
||||
return &Json{
|
||||
Code: code,
|
||||
|
11
response/json_interface.go
Normal file
11
response/json_interface.go
Normal file
@ -0,0 +1,11 @@
|
||||
package response
|
||||
|
||||
type JsonInterface interface {
|
||||
SetCode(code int)
|
||||
SetMessage(msg string)
|
||||
SetData(data interface{})
|
||||
SetStatus(status bool)
|
||||
//
|
||||
GetDefaultSuccessCode() int
|
||||
GetDefaultFailedCode() int
|
||||
}
|
@ -1,10 +1,42 @@
|
||||
package response
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestNewJsonByDefaultSuccess(t *testing.T) {
|
||||
fmt.Println(NewJsonByDefaultSuccess([]int{1, 2, 3}).String())
|
||||
type A struct {
|
||||
Code int `json:"ret"`
|
||||
Json
|
||||
}
|
||||
|
||||
func (j *A) GetDefaultSuccessCode() int {
|
||||
return 0
|
||||
}
|
||||
|
||||
func (j *A) GetDefaultFailedCode() int {
|
||||
return -1
|
||||
}
|
||||
|
||||
func (j *A) String() string {
|
||||
b, err := json.Marshal(j)
|
||||
if err == nil {
|
||||
return bytes.NewBuffer(b).String()
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (a *A) SetCode(code int) {
|
||||
a.Code = code
|
||||
}
|
||||
|
||||
func TestNewJsonByDefaultSuccess(t *testing.T) {
|
||||
fmt.Println(NewJsonByDefaultSuccess())
|
||||
}
|
||||
|
||||
func TestName(t *testing.T) {
|
||||
resp := JsonResponseGenerator{&A{Code: 232}}
|
||||
fmt.Println(fmt.Sprintf("%+v", resp.NewJsonByDefaultFailed()))
|
||||
}
|
||||
|
@ -1 +1,41 @@
|
||||
package response
|
||||
|
||||
import "reflect"
|
||||
|
||||
type JsonResponseGenerator struct {
|
||||
Interface JsonInterface
|
||||
}
|
||||
|
||||
func (j JsonResponseGenerator) NewJsonByDefaultSuccess(data ...interface{}) JsonInterface {
|
||||
ptr := j.NewJson(true, 0, JsonMsgDefaultOk, data...)
|
||||
ptr.SetCode(ptr.GetDefaultSuccessCode())
|
||||
return ptr
|
||||
}
|
||||
|
||||
func (j JsonResponseGenerator) NewJsonByDefaultFailed(data ...interface{}) JsonInterface {
|
||||
ptr := j.NewJson(false, 1, JsonMsgDefaultFailed, data...)
|
||||
ptr.SetCode(ptr.GetDefaultFailedCode())
|
||||
return ptr
|
||||
}
|
||||
|
||||
func (j JsonResponseGenerator) NewJsonByFailed(code int, msg string) JsonInterface {
|
||||
return j.NewJson(false, code, msg)
|
||||
}
|
||||
|
||||
func (j JsonResponseGenerator) NewJsonByFailedWithData(code int, msg string, data interface{}) JsonInterface {
|
||||
return j.NewJson(false, code, msg, data)
|
||||
}
|
||||
|
||||
func (j JsonResponseGenerator) NewJson(status bool, code int, msg string, data ...interface{}) JsonInterface {
|
||||
jPtr := reflect.New(reflect.TypeOf(j.Interface).Elem())
|
||||
if b, ok := jPtr.Interface().(JsonInterface); ok {
|
||||
b.SetStatus(status)
|
||||
b.SetCode(code)
|
||||
b.SetMessage(msg)
|
||||
if len(data) > 0 {
|
||||
b.SetData(data[0])
|
||||
}
|
||||
return b
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user