example seems ok
This commit is contained in:
parent
3257e04322
commit
c019a2d4be
@ -11,6 +11,8 @@ import (
|
|||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type M = map[string]interface{}
|
||||||
|
|
||||||
func OrString(ss ...string) string {
|
func OrString(ss ...string) string {
|
||||||
for _, s := range ss {
|
for _, s := range ss {
|
||||||
if s != "" {
|
if s != "" {
|
||||||
@ -94,3 +96,22 @@ func GetGinApp() *gin.Engine {
|
|||||||
})
|
})
|
||||||
return app
|
return app
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func WrapHandler(fn func(*gin.Context) (interface{}, error)) gin.HandlerFunc {
|
||||||
|
return func(c *gin.Context) {
|
||||||
|
r, err := fn(c)
|
||||||
|
var b = []byte{}
|
||||||
|
if err == nil {
|
||||||
|
b, err = json.Marshal(r)
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
logrus.Printf("status: 500, code: 500 message: %s", err.Error())
|
||||||
|
c.JSON(500, M{"code": 500, "message": err.Error()})
|
||||||
|
} else {
|
||||||
|
logrus.Printf("status: 200, content: %s", string(b))
|
||||||
|
c.Status(200)
|
||||||
|
c.Writer.Header().Add("Content-Type", "application/json")
|
||||||
|
c.Writer.Write(b)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@ -1,11 +1,19 @@
|
|||||||
package examples
|
package examples
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
"github.com/yedf/dtm/common"
|
"github.com/yedf/dtm/common"
|
||||||
"github.com/yedf/dtm/dtm"
|
"github.com/yedf/dtm/dtm"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func Main() {
|
||||||
|
go StartSvr()
|
||||||
|
FireRequest()
|
||||||
|
time.Sleep(1000 * time.Second)
|
||||||
|
}
|
||||||
|
|
||||||
func FireRequest() {
|
func FireRequest() {
|
||||||
gid := common.GenGid()
|
gid := common.GenGid()
|
||||||
logrus.Printf("busi transaction begin: %s", gid)
|
logrus.Printf("busi transaction begin: %s", gid)
|
||||||
@ -26,11 +34,6 @@ func FireRequest() {
|
|||||||
func StartSvr() {
|
func StartSvr() {
|
||||||
logrus.Printf("examples starting")
|
logrus.Printf("examples starting")
|
||||||
app := common.GetGinApp()
|
app := common.GetGinApp()
|
||||||
app.POST(BusiApi+"/TransIn", TransIn)
|
AddRoute(app)
|
||||||
app.POST(BusiApi+"/TransInCompensate", TransInCompensate)
|
|
||||||
app.POST(BusiApi+"/TransOut", TransOut)
|
|
||||||
app.POST(BusiApi+"/TransOutCompensate", TransOutCompensate)
|
|
||||||
app.GET(BusiApi+"/TransQuery", TransQuery)
|
|
||||||
logrus.Printf("examples istening at %d", BusiPort)
|
|
||||||
app.Run(":8081")
|
app.Run(":8081")
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,13 +1,20 @@
|
|||||||
package examples
|
package examples
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
"github.com/yedf/dtm/common"
|
"github.com/yedf/dtm/common"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func AddRoute(app *gin.Engine) {
|
||||||
|
app.POST(BusiApi+"/TransIn", common.WrapHandler(TransIn))
|
||||||
|
app.POST(BusiApi+"/TransInCompensate", common.WrapHandler(TransInCompensate))
|
||||||
|
app.POST(BusiApi+"/TransOut", common.WrapHandler(TransOut))
|
||||||
|
app.POST(BusiApi+"/TransOutCompensate", common.WrapHandler(TransOutCompensate))
|
||||||
|
app.GET(BusiApi+"/TransQuery", common.WrapHandler(TransQuery))
|
||||||
|
logrus.Printf("examples istening at %d", BusiPort)
|
||||||
|
}
|
||||||
|
|
||||||
type M = map[string]interface{}
|
type M = map[string]interface{}
|
||||||
|
|
||||||
var TransInResult = ""
|
var TransInResult = ""
|
||||||
@ -22,63 +29,61 @@ type TransReq struct {
|
|||||||
TransOutFailed bool `json:"transOutFailed"`
|
TransOutFailed bool `json:"transOutFailed"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func TransIn(c *gin.Context) {
|
func TransIn(c *gin.Context) (interface{}, error) {
|
||||||
gid := c.Query("gid")
|
gid := c.Query("gid")
|
||||||
req := TransReq{}
|
req := TransReq{}
|
||||||
if err := c.BindJSON(&req); err != nil {
|
if err := c.BindJSON(&req); err != nil {
|
||||||
return
|
return nil, err
|
||||||
}
|
}
|
||||||
if req.TransInFailed {
|
if req.TransInFailed {
|
||||||
logrus.Printf("%s TransIn %v failed", req)
|
logrus.Printf("%s TransIn %v failed", gid, req)
|
||||||
c.Error(fmt.Errorf("TransIn failed for gid: %s", gid))
|
return M{"result": "FAIL"}, nil
|
||||||
return
|
|
||||||
}
|
}
|
||||||
res := common.OrString(TransInResult, "SUCCESS")
|
res := common.OrString(TransInResult, "SUCCESS")
|
||||||
logrus.Printf("%s TransIn: %v result: %s", gid, req, res)
|
logrus.Printf("%s TransIn: %v result: %s", gid, req, res)
|
||||||
c.JSON(200, M{"result": res})
|
return M{"result": res}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func TransInCompensate(c *gin.Context) {
|
func TransInCompensate(c *gin.Context) (interface{}, error) {
|
||||||
gid := c.Query("gid")
|
gid := c.Query("gid")
|
||||||
req := TransReq{}
|
req := TransReq{}
|
||||||
if err := c.BindJSON(&req); err != nil {
|
if err := c.BindJSON(&req); err != nil {
|
||||||
return
|
return nil, err
|
||||||
}
|
}
|
||||||
res := common.OrString(TransInCompensateResult, "SUCCESS")
|
res := common.OrString(TransInCompensateResult, "SUCCESS")
|
||||||
logrus.Printf("%s TransInCompensate: %v result: %s", gid, req, res)
|
logrus.Printf("%s TransInCompensate: %v result: %s", gid, req, res)
|
||||||
c.JSON(200, M{"result": res})
|
return M{"result": res}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func TransOut(c *gin.Context) {
|
func TransOut(c *gin.Context) (interface{}, error) {
|
||||||
gid := c.Query("gid")
|
gid := c.Query("gid")
|
||||||
req := TransReq{}
|
req := TransReq{}
|
||||||
if err := c.BindJSON(&req); err != nil {
|
if err := c.BindJSON(&req); err != nil {
|
||||||
return
|
return nil, err
|
||||||
}
|
}
|
||||||
if req.TransOutFailed {
|
if req.TransOutFailed {
|
||||||
logrus.Printf("%s TransOut %v failed", gid, req)
|
logrus.Printf("%s TransOut %v failed", gid, req)
|
||||||
c.JSON(500, M{"result": "FAIL"})
|
return M{"result": "FAIL"}, nil
|
||||||
return
|
|
||||||
}
|
}
|
||||||
res := common.OrString(TransOutResult, "SUCCESS")
|
res := common.OrString(TransOutResult, "SUCCESS")
|
||||||
logrus.Printf("%s TransOut: %v result: %s", gid, req, res)
|
logrus.Printf("%s TransOut: %v result: %s", gid, req, res)
|
||||||
c.JSON(200, M{"result": res})
|
return M{"result": res}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func TransOutCompensate(c *gin.Context) {
|
func TransOutCompensate(c *gin.Context) (interface{}, error) {
|
||||||
gid := c.Query("gid")
|
gid := c.Query("gid")
|
||||||
req := TransReq{}
|
req := TransReq{}
|
||||||
if err := c.BindJSON(&req); err != nil {
|
if err := c.BindJSON(&req); err != nil {
|
||||||
return
|
return nil, err
|
||||||
}
|
}
|
||||||
res := common.OrString(TransOutCompensateResult, "SUCCESS")
|
res := common.OrString(TransOutCompensateResult, "SUCCESS")
|
||||||
logrus.Printf("%s TransOutCompensate: %v result: %s", gid, req, res)
|
logrus.Printf("%s TransOutCompensate: %v result: %s", gid, req, res)
|
||||||
c.JSON(200, M{"result": res})
|
return M{"result": res}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func TransQuery(c *gin.Context) {
|
func TransQuery(c *gin.Context) (interface{}, error) {
|
||||||
gid := c.Query("gid")
|
gid := c.Query("gid")
|
||||||
logrus.Printf("%s TransQuery", gid)
|
logrus.Printf("%s TransQuery", gid)
|
||||||
res := common.OrString(TransQueryResult, "SUCCESS")
|
res := common.OrString(TransQueryResult, "SUCCESS")
|
||||||
c.JSON(200, M{"result": res})
|
return M{"result": res}, nil
|
||||||
}
|
}
|
||||||
|
|||||||
3
main.go
3
main.go
@ -12,7 +12,6 @@ type M = map[string]interface{}
|
|||||||
func main() {
|
func main() {
|
||||||
dtmsvr.LoadConfig()
|
dtmsvr.LoadConfig()
|
||||||
go dtmsvr.StartSvr()
|
go dtmsvr.StartSvr()
|
||||||
go examples.StartSvr()
|
go examples.Main()
|
||||||
examples.FireRequest()
|
|
||||||
time.Sleep(1000 * 1000 * 1000 * 1000)
|
time.Sleep(1000 * 1000 * 1000 * 1000)
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user