|
|
|
@ -3,96 +3,96 @@ package dtmsvr |
|
|
|
import ( |
|
|
|
"encoding/json" |
|
|
|
"fmt" |
|
|
|
"net/http" |
|
|
|
|
|
|
|
"github.com/dtm-labs/dtm/dtmcli" |
|
|
|
"github.com/dtm-labs/dtm/dtmcli/logger" |
|
|
|
"github.com/gin-gonic/gin" |
|
|
|
"net/http" |
|
|
|
) |
|
|
|
|
|
|
|
type jsonRpcHttpReq struct { |
|
|
|
type jsonRPCReq struct { |
|
|
|
Method string `json:"method"` |
|
|
|
Jsonrpc string `json:"jsonrpc"` |
|
|
|
Params interface{} `json:"params"` |
|
|
|
Id string `json:"id"` |
|
|
|
ID string `json:"id"` |
|
|
|
} |
|
|
|
|
|
|
|
func addJsonRpcHttpRouter(engine *gin.Engine) { |
|
|
|
func addJSONRPCRouter(engine *gin.Engine) { |
|
|
|
engine.POST("/", dispatcher) |
|
|
|
} |
|
|
|
|
|
|
|
func dispatcher(c *gin.Context) { |
|
|
|
req := new(jsonRpcHttpReq) |
|
|
|
req := new(jsonRPCReq) |
|
|
|
err := c.BindJSON(req) |
|
|
|
logger.Infof("request:%s\n", req) |
|
|
|
if err != nil { |
|
|
|
c.JSON(http.StatusOK, gin.H{"id": req.Id, "result": nil, "error": map[string]interface{}{"code": -32700, "message": "Parse error"}}) |
|
|
|
c.JSON(http.StatusOK, gin.H{"id": req.ID, "result": nil, "error": map[string]interface{}{"code": -32700, "message": "Parse error"}}) |
|
|
|
return |
|
|
|
} |
|
|
|
if req.Method == "dtmserver.NewGid" { |
|
|
|
res, err := jsonRpcHttpNewGid() |
|
|
|
c.JSON(http.StatusOK, gin.H{"id": req.Id, "result": res, "error": err}) |
|
|
|
res := jsonRPCNewGid() |
|
|
|
c.JSON(http.StatusOK, gin.H{"id": req.ID, "result": res, "error": err}) |
|
|
|
return |
|
|
|
} |
|
|
|
|
|
|
|
if req.Method == "dtmserver.Prepare" { |
|
|
|
res := jsonRpcHttpPrepare(req.Params) |
|
|
|
c.JSON(http.StatusOK, gin.H{"id": req.Id, "result": res, "error": nil}) |
|
|
|
res := jsonRPCPrepare(req.Params) |
|
|
|
c.JSON(http.StatusOK, gin.H{"id": req.ID, "result": res, "error": nil}) |
|
|
|
return |
|
|
|
} |
|
|
|
|
|
|
|
if req.Method == "dtmserver.Submit" { |
|
|
|
res := jsonRpcHttpSubmit(req.Params) |
|
|
|
c.JSON(http.StatusOK, gin.H{"id": req.Id, "result": res, "error": nil}) |
|
|
|
res := jsonRPCSubmit(req.Params) |
|
|
|
c.JSON(http.StatusOK, gin.H{"id": req.ID, "result": res, "error": nil}) |
|
|
|
return |
|
|
|
} |
|
|
|
|
|
|
|
if req.Method == "dtmserver.Abort" { |
|
|
|
res := jsonRpcHttpAbort(req.Params) |
|
|
|
c.JSON(http.StatusOK, gin.H{"id": req.Id, "result": res, "error": nil}) |
|
|
|
res := jsonRPCAbort(req.Params) |
|
|
|
c.JSON(http.StatusOK, gin.H{"id": req.ID, "result": res, "error": nil}) |
|
|
|
return |
|
|
|
} |
|
|
|
|
|
|
|
if req.Method == "dtmserver.RegisterBranch" { |
|
|
|
res := jsonRpcHttpRegisterBranch(req.Params) |
|
|
|
c.JSON(http.StatusOK, gin.H{"id": req.Id, "result": res, "error": nil}) |
|
|
|
res := jsonRPCRegisterBranch(req.Params) |
|
|
|
c.JSON(http.StatusOK, gin.H{"id": req.ID, "result": res, "error": nil}) |
|
|
|
return |
|
|
|
} |
|
|
|
c.JSON(http.StatusOK, gin.H{"id": req.Id, "result": nil, "error": map[string]interface{}{"code": -32601, "message": "Method not found"}}) |
|
|
|
return |
|
|
|
c.JSON(http.StatusOK, gin.H{"id": req.ID, "result": nil, "error": map[string]interface{}{"code": -32601, "message": "Method not found"}}) |
|
|
|
} |
|
|
|
|
|
|
|
func jsonRpcHttpNewGid() (interface{}, error) { |
|
|
|
return map[string]interface{}{"gid": GenGid(), "dtm_result": dtmcli.ResultSuccess}, nil |
|
|
|
func jsonRPCNewGid() interface{} { |
|
|
|
return map[string]interface{}{"gid": GenGid(), "dtm_result": dtmcli.ResultSuccess} |
|
|
|
} |
|
|
|
|
|
|
|
func jsonRpcHttpPrepare(params interface{}) interface{} { |
|
|
|
res := svcPrepare(TransFromJsonRpcHttpContext(params)) |
|
|
|
func jsonRPCPrepare(params interface{}) interface{} { |
|
|
|
res := svcPrepare(TransFromJSONRPCContext(params)) |
|
|
|
if res == nil { |
|
|
|
return map[string]string{"dtm_result": "SUCCESS"} |
|
|
|
} |
|
|
|
return map[string]string{"dtm_result": "FAILURE", "message": fmt.Sprintf("%v", res)} |
|
|
|
} |
|
|
|
|
|
|
|
func jsonRpcHttpSubmit(params interface{}) interface{} { |
|
|
|
res := svcSubmit(TransFromJsonRpcHttpContext(params)) |
|
|
|
func jsonRPCSubmit(params interface{}) interface{} { |
|
|
|
res := svcSubmit(TransFromJSONRPCContext(params)) |
|
|
|
if res == nil { |
|
|
|
return map[string]string{"dtm_result": "SUCCESS"} |
|
|
|
} |
|
|
|
return map[string]string{"dtm_result": "FAILURE", "message": fmt.Sprintf("%v", res)} |
|
|
|
} |
|
|
|
|
|
|
|
func jsonRpcHttpAbort(params interface{}) interface{} { |
|
|
|
res := svcAbort(TransFromJsonRpcHttpContext(params)) |
|
|
|
func jsonRPCAbort(params interface{}) interface{} { |
|
|
|
res := svcAbort(TransFromJSONRPCContext(params)) |
|
|
|
if res == nil { |
|
|
|
return map[string]string{"dtm_result": "SUCCESS"} |
|
|
|
} |
|
|
|
return map[string]string{"dtm_result": "FAILURE", "message": fmt.Sprintf("%v", res)} |
|
|
|
} |
|
|
|
|
|
|
|
func jsonRpcHttpRegisterBranch(params interface{}) interface{} { |
|
|
|
func jsonRPCRegisterBranch(params interface{}) interface{} { |
|
|
|
data := map[string]string{} |
|
|
|
paramsJson, _ := json.Marshal(params) |
|
|
|
err := json.Unmarshal(paramsJson, &data) |
|
|
|
paramsJSON, _ := json.Marshal(params) |
|
|
|
err := json.Unmarshal(paramsJSON, &data) |
|
|
|
if err != nil { |
|
|
|
return map[string]string{"dtm_result": "FAILURE", "message": err.Error()} |
|
|
|
} |
|
|
|
|