update qs example
This commit is contained in:
parent
e80f68c387
commit
c5e3bc370d
@ -12,11 +12,10 @@ type testConfig struct {
|
|||||||
|
|
||||||
var config = testConfig{}
|
var config = testConfig{}
|
||||||
|
|
||||||
var myinit int = func() int {
|
func init() {
|
||||||
InitApp(GetProjectDir(), &config)
|
InitConfig(GetProjectDir(), &config)
|
||||||
config.Mysql["database"] = ""
|
config.Mysql["database"] = ""
|
||||||
return 0
|
}
|
||||||
}()
|
|
||||||
|
|
||||||
func TestDb(t *testing.T) {
|
func TestDb(t *testing.T) {
|
||||||
db := DbGet(config.Mysql)
|
db := DbGet(config.Mysql)
|
||||||
|
|||||||
@ -207,8 +207,8 @@ func (f *formatter) Format(entry *logrus.Entry) ([]byte, error) {
|
|||||||
return b.Bytes(), nil
|
return b.Bytes(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// InitApp init config
|
// InitConfig init config
|
||||||
func InitApp(dir string, config interface{}) {
|
func InitConfig(dir string, config interface{}) {
|
||||||
logrus.SetFormatter(&formatter{})
|
logrus.SetFormatter(&formatter{})
|
||||||
cont, err := ioutil.ReadFile(dir + "/conf.yml")
|
cont, err := ioutil.ReadFile(dir + "/conf.yml")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@ -1,5 +1,7 @@
|
|||||||
package dtmsvr
|
package dtmsvr
|
||||||
|
|
||||||
|
import "github.com/yedf/dtm/common"
|
||||||
|
|
||||||
type dtmsvrConfig struct {
|
type dtmsvrConfig struct {
|
||||||
TransCronInterval int64 `yaml:"TransCronInterval"` // 单位秒 当事务等待这个时间之后,还没有变化,则进行一轮处理,包括prepared中的任务和committed的任务
|
TransCronInterval int64 `yaml:"TransCronInterval"` // 单位秒 当事务等待这个时间之后,还没有变化,则进行一轮处理,包括prepared中的任务和committed的任务
|
||||||
Mysql map[string]string `yaml:"Mysql"`
|
Mysql map[string]string `yaml:"Mysql"`
|
||||||
@ -10,3 +12,8 @@ var config = &dtmsvrConfig{
|
|||||||
}
|
}
|
||||||
|
|
||||||
var dbName = "dtm"
|
var dbName = "dtm"
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
common.InitConfig(common.GetProjectDir(), &config)
|
||||||
|
config.Mysql["database"] = ""
|
||||||
|
}
|
||||||
|
|||||||
@ -19,7 +19,7 @@ var app *gin.Engine
|
|||||||
|
|
||||||
func TestMain(m *testing.M) {
|
func TestMain(m *testing.M) {
|
||||||
TransProcessedTestChan = make(chan string, 1)
|
TransProcessedTestChan = make(chan string, 1)
|
||||||
common.InitApp(common.GetProjectDir(), &config)
|
common.InitConfig(common.GetProjectDir(), &config)
|
||||||
config.Mysql["database"] = dbName
|
config.Mysql["database"] = dbName
|
||||||
PopulateMysql(false)
|
PopulateMysql(false)
|
||||||
examples.PopulateMysql(false)
|
examples.PopulateMysql(false)
|
||||||
|
|||||||
@ -20,8 +20,6 @@ func MainStart() {
|
|||||||
// StartSvr StartSvr
|
// StartSvr StartSvr
|
||||||
func StartSvr() {
|
func StartSvr() {
|
||||||
logrus.Printf("start dtmsvr")
|
logrus.Printf("start dtmsvr")
|
||||||
common.InitApp(common.GetProjectDir(), &config)
|
|
||||||
config.Mysql["database"] = dbName
|
|
||||||
app := common.GetGinApp()
|
app := common.GetGinApp()
|
||||||
addRoute(app)
|
addRoute(app)
|
||||||
logrus.Printf("dtmsvr listen at: %d", dtmsvrPort)
|
logrus.Printf("dtmsvr listen at: %d", dtmsvrPort)
|
||||||
@ -31,7 +29,5 @@ func StartSvr() {
|
|||||||
|
|
||||||
// PopulateMysql setup mysql data
|
// PopulateMysql setup mysql data
|
||||||
func PopulateMysql(skipDrop bool) {
|
func PopulateMysql(skipDrop bool) {
|
||||||
common.InitApp(common.GetProjectDir(), &config)
|
|
||||||
config.Mysql["database"] = ""
|
|
||||||
examples.RunSQLScript(config.Mysql, common.GetCurrentCodeDir()+"/dtmsvr.sql", skipDrop)
|
examples.RunSQLScript(config.Mysql, common.GetCurrentCodeDir()+"/dtmsvr.sql", skipDrop)
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,5 +1,7 @@
|
|||||||
package examples
|
package examples
|
||||||
|
|
||||||
|
import "github.com/yedf/dtm/common"
|
||||||
|
|
||||||
type exampleConfig struct {
|
type exampleConfig struct {
|
||||||
Mysql map[string]string `yaml:"Mysql"`
|
Mysql map[string]string `yaml:"Mysql"`
|
||||||
}
|
}
|
||||||
@ -7,3 +9,8 @@ type exampleConfig struct {
|
|||||||
var config = exampleConfig{}
|
var config = exampleConfig{}
|
||||||
|
|
||||||
var dbName = "dtm_busi"
|
var dbName = "dtm_busi"
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
common.InitConfig(common.GetProjectDir(), &config)
|
||||||
|
config.Mysql["database"] = dbName
|
||||||
|
}
|
||||||
|
|||||||
@ -32,7 +32,5 @@ func RunSQLScript(mysql map[string]string, script string, skipDrop bool) {
|
|||||||
|
|
||||||
// PopulateMysql populate example mysql data
|
// PopulateMysql populate example mysql data
|
||||||
func PopulateMysql(skipDrop bool) {
|
func PopulateMysql(skipDrop bool) {
|
||||||
common.InitApp(common.GetProjectDir(), &config)
|
|
||||||
config.Mysql["database"] = dbName
|
|
||||||
RunSQLScript(config.Mysql, common.GetCurrentCodeDir()+"/examples.sql", skipDrop)
|
RunSQLScript(config.Mysql, common.GetCurrentCodeDir()+"/examples.sql", skipDrop)
|
||||||
}
|
}
|
||||||
|
|||||||
@ -8,6 +8,7 @@ import (
|
|||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
"github.com/yedf/dtm/common"
|
"github.com/yedf/dtm/common"
|
||||||
"github.com/yedf/dtm/dtmcli"
|
"github.com/yedf/dtm/dtmcli"
|
||||||
|
"gorm.io/gorm"
|
||||||
)
|
)
|
||||||
|
|
||||||
// 启动命令:go run app/main.go qs
|
// 启动命令:go run app/main.go qs
|
||||||
@ -42,17 +43,27 @@ func QsFireRequest() string {
|
|||||||
return saga.Gid
|
return saga.Gid
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func qsAdjustBalance(uid int, amount int) (interface{}, error) {
|
||||||
|
err := dbGet().Transaction(func(tx *gorm.DB) error {
|
||||||
|
return tx.Model(&UserAccount{}).Where("user_id = ?", uid).Update("balance", gorm.Expr("balance + ?", amount)).Error
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return M{"dtm_result": "SUCCESS"}, nil
|
||||||
|
}
|
||||||
|
|
||||||
func qsAddRoute(app *gin.Engine) {
|
func qsAddRoute(app *gin.Engine) {
|
||||||
app.POST(qsBusiAPI+"/TransIn", common.WrapHandler(func(c *gin.Context) (interface{}, error) {
|
app.POST(qsBusiAPI+"/TransIn", common.WrapHandler(func(c *gin.Context) (interface{}, error) {
|
||||||
return M{"dtm_result": "SUCCESS"}, nil
|
return qsAdjustBalance(2, 30)
|
||||||
}))
|
}))
|
||||||
app.POST(qsBusiAPI+"/TransInCompensate", common.WrapHandler(func(c *gin.Context) (interface{}, error) {
|
app.POST(qsBusiAPI+"/TransInCompensate", common.WrapHandler(func(c *gin.Context) (interface{}, error) {
|
||||||
return M{"dtm_result": "SUCCESS"}, nil
|
return qsAdjustBalance(2, -30)
|
||||||
}))
|
}))
|
||||||
app.POST(qsBusiAPI+"/TransOut", common.WrapHandler(func(c *gin.Context) (interface{}, error) {
|
app.POST(qsBusiAPI+"/TransOut", common.WrapHandler(func(c *gin.Context) (interface{}, error) {
|
||||||
return M{"dtm_result": "SUCCESS"}, nil
|
return qsAdjustBalance(1, -30)
|
||||||
}))
|
}))
|
||||||
app.POST(qsBusiAPI+"/TransOutCompensate", common.WrapHandler(func(c *gin.Context) (interface{}, error) {
|
app.POST(qsBusiAPI+"/TransOutCompensate", common.WrapHandler(func(c *gin.Context) (interface{}, error) {
|
||||||
return M{"dtm_result": "SUCCESS"}, nil
|
return qsAdjustBalance(1, 30)
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user