两项优化
1. 优化json类 2. 优化数据库默认连接配置
This commit is contained in:
parent
42bbd3f0f5
commit
eba7a63f4b
9
db/db.go
9
db/db.go
@ -27,10 +27,17 @@ type DbItem struct {
|
||||
Name string `yaml:"name"`
|
||||
Driver string `yaml:"driver"`
|
||||
Prefix string `yaml:"prefix"`
|
||||
MaxIdleConns int `yaml:"max_idle_conns"`
|
||||
MaxOpenConns int `yaml:"max_open_conns"`
|
||||
ConnMaxLifetime int64 `yaml:"conn_max_lifetime"`
|
||||
Timeout int `yaml:"timeout"`
|
||||
}
|
||||
|
||||
func (d *DbItem) Dsn() string {
|
||||
return fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8&loc=Local&autocommit=true&timeout=5s", d.User, d.Pass, d.Host, d.Port, d.Name)
|
||||
if d.Timeout <= 0 {
|
||||
d.Timeout = 5
|
||||
}
|
||||
return fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8&loc=Local&autocommit=true&timeout=%ds", d.User, d.Pass, d.Host, d.Port, d.Name, d.Timeout)
|
||||
}
|
||||
|
||||
func init() {
|
||||
|
@ -45,8 +45,23 @@ func LoadXorm(dsns map[string]dsnConfig) (err error) {
|
||||
tbMapper := core.NewPrefixMapper(core.SnakeMapper{}, cast.ToString(dsn.Item.Prefix))
|
||||
engine.SetTableMapper(tbMapper)
|
||||
engine.SetLogger(logger)
|
||||
engine.SetMaxIdleConns(1000)
|
||||
engine.SetMaxOpenConns(1000)
|
||||
|
||||
if dsn.Item.MaxIdleConns <= 0 {
|
||||
engine.SetMaxIdleConns(200)
|
||||
} else {
|
||||
engine.SetMaxIdleConns(dsn.Item.MaxIdleConns)
|
||||
}
|
||||
|
||||
if dsn.Item.MaxOpenConns <= 0 {
|
||||
engine.SetMaxOpenConns(200)
|
||||
} else {
|
||||
engine.SetMaxOpenConns(dsn.Item.MaxOpenConns)
|
||||
}
|
||||
|
||||
if dsn.Item.ConnMaxLifetime > 0 {
|
||||
engine.SetConnMaxLifetime(time.Second * time.Duration(dsn.Item.ConnMaxLifetime))
|
||||
}
|
||||
|
||||
engine.TZLocation, _ = time.LoadLocation("Asia/Shanghai")
|
||||
if runmode == "dev" {
|
||||
engine.ShowSQL(true)
|
||||
|
@ -3,7 +3,6 @@ package response
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"golib.gaore.com/GaoreGo/grlogs"
|
||||
)
|
||||
|
||||
const (
|
||||
@ -19,9 +18,14 @@ type Json struct {
|
||||
}
|
||||
|
||||
func (j *Json) String() string {
|
||||
b, err := json.Marshal(j)
|
||||
grlogs.Debug(err)
|
||||
return bytes.NewBuffer(b).String()
|
||||
s, _ := j.ToString()
|
||||
return s
|
||||
}
|
||||
|
||||
func (j *Json) ToString() (str string, err error) {
|
||||
var b []byte
|
||||
b, err = json.Marshal(j)
|
||||
return bytes.NewBuffer(b).String(), err
|
||||
}
|
||||
|
||||
func NewJsonByDefaultSuccess(data ...interface{}) *Json {
|
||||
|
@ -1,8 +1,48 @@
|
||||
package response
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/xml"
|
||||
"log"
|
||||
)
|
||||
|
||||
type Xml struct {
|
||||
Code int `xml:"code"`
|
||||
Data interface{} `xml:"data"`
|
||||
Msg string `xml:"msg"`
|
||||
Status bool `xml:"status"`
|
||||
}
|
||||
|
||||
func (x *Xml) String() string {
|
||||
b, err := xml.MarshalIndent(x, "", " ")
|
||||
if err != nil {
|
||||
log.Println("xml encode", err)
|
||||
}
|
||||
return xml.Header + bytes.NewBuffer(b).String()
|
||||
}
|
||||
|
||||
func NewXmlByDefaultSuccess(data ...interface{}) Xml {
|
||||
return NewXml(true, 0, JsonMsgDefaultOk, data...)
|
||||
}
|
||||
|
||||
func NewXmlByDefaultFailed(data ...interface{}) Xml {
|
||||
return NewXml(false, 1, JsonMsgDefaultFailed, data...)
|
||||
}
|
||||
|
||||
func NewXml(status bool, code int, msg string, data ...interface{}) Xml {
|
||||
if len(data) > 0 {
|
||||
return Xml{
|
||||
Code: code,
|
||||
Data: data[0],
|
||||
Msg: msg,
|
||||
Status: status,
|
||||
}
|
||||
} else {
|
||||
return Xml{
|
||||
Code: code,
|
||||
Data: nil,
|
||||
Msg: msg,
|
||||
Status: status,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
14
response/xml_test.go
Normal file
14
response/xml_test.go
Normal file
@ -0,0 +1,14 @@
|
||||
package response
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type XmlResponse struct {
|
||||
Xml
|
||||
}
|
||||
|
||||
func TestNewXmlByDefaultSuccess(t *testing.T) {
|
||||
fmt.Println(&XmlResponse{Xml: NewXmlByDefaultSuccess()})
|
||||
}
|
Loading…
Reference in New Issue
Block a user