-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Completed Features: Client, Routers, Controllers
Initial Commit
- Loading branch information
dormao
committed
Feb 1, 2020
0 parents
commit b3500e8
Showing
18 changed files
with
861 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
# Food Transport Rest Server | ||
##### FoodTransport Manager RestAPI Server for Hyperledger Fabric v1.0.0 network | ||
|
||
This is the http server for hyperledger network, it works with Fabric SDK Go, invoke and query chaincode functions by Fabric SDK Client. | ||
|
||
## Compatiable Libraries and Environments | ||
* Golang (1.10.1)(no go mod) | ||
* Hyperledger Fabric SDK Go (79343ba)(use dep) | ||
``` | ||
git clone https://github.com/hyperledger/fabric-sdk-go | ||
cd fabric-sdk-go | ||
git checkout 79343ba | ||
# This sdk version uses dep | ||
dep ensure | ||
``` | ||
* GIN (v1.3.0)(no go mod)(use govendor) | ||
``` | ||
git clone https://github.com/gin-gonic/gin | ||
cd gin | ||
git checkout v1.3.0 | ||
govendor sync | ||
``` | ||
|
||
## Quick Launch | ||
Launch the server must config some files first, the Fabric SDK config files can be found in directory `github.com/hyperledger/fabric-sdk-go/pkg/core/config/testdata/` | ||
|
||
It must be configured with Channel Name, Organization Name, crypto-path, etc.. | ||
|
||
The fastest launch code is in `/test/gin_integration_test.go` | ||
|
||
``` shell script | ||
echo "Edit the integration code with network config, crypto config..." | ||
echo "Launch Quick Launch" | ||
cd $GOPATH/src/github.com/dormao/restgo_foodmanage/test | ||
go test | ||
|
||
echo "Stop the server" | ||
^C | ||
``` | ||
|
||
## License | ||
[MIT](https://opensource.org/licenses/MIT) | ||
|
||
## Other Language | ||
[Chinese](./README_cn.md) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# Food Transport Rest Server | ||
##### 食品运输管理(智能合约)网络,Fabric SDK 与 GIN 混合交互的后端服务器 | ||
|
||
这个服务器用在区块链 Hyperledger Fabric v1.0.0 的网络上,它通过Fabric SDK与区块链交互,用GIN与浏览器交互 | ||
|
||
## 兼容的测试环境与版本 | ||
* Golang (1.10.1)(没有go mod) | ||
* Hyperledger Fabric SDK Go (79343ba)(用依赖管理工具 dep) | ||
``` | ||
git clone https://github.com/hyperledger/fabric-sdk-go | ||
cd fabric-sdk-go | ||
git checkout 79343ba | ||
# This sdk version uses dep | ||
dep ensure | ||
``` | ||
* GIN (v1.3.0)(没有go mod)(用govendor解决依赖) | ||
``` | ||
git clone https://github.com/gin-gonic/gin | ||
cd gin | ||
git checkout v1.3.0 | ||
govendor sync | ||
``` | ||
|
||
## 快速部署 | ||
配置区块链网络需要配置文件,这个配置文件可以在Fabric SDK Go库`github.com/hyperledger/fabric-sdk-go/pkg/core/config/testdata/`里面可以找到 | ||
|
||
同时还需要配置通道名(Channel)、组织名(Organization)、证书(crypto-config)等 | ||
|
||
最快的配置指引在`/test/gin_integration_test.go` | ||
``` shell script | ||
echo "编辑integration配置,SDK配置..." | ||
echo "运行快速部署" | ||
cd $GOPATH/src/github.com/dormao/restgo_foodmanage/test | ||
go test | ||
|
||
echo "关闭服务器" | ||
^C | ||
``` | ||
|
||
## 许可 | ||
[MIT](https://opensource.org/licenses/MIT) | ||
|
||
## 其他语言 | ||
[English](./README.md) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package controllers | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
models2 "github.com/dormao/chaincode_foodmanage/models" | ||
"github.com/dormao/chaincode_foodmanage/models/consts" | ||
"github.com/dormao/restgo_foodmanage/models" | ||
"github.com/dormao/restgo_foodmanage/sdk" | ||
"github.com/dormao/restgo_foodmanage/util" | ||
"github.com/gin-gonic/gin" | ||
) | ||
|
||
func BuyerLogin(ctx *gin.Context) { | ||
creden := &models.PasswordCredentialRequest{} | ||
if !util.CheckParamsValid(ctx, creden) { | ||
util.JsonReturn(ctx, models2.WithError(consts.CodeErrorParams, consts.MsgErrorParams)) | ||
return | ||
} | ||
resp, err := sdk.ClientExecute(models2.UnAuthLogin, [][]byte{ | ||
[]byte(fmt.Sprint(models2.OperatorBuyer)), []byte(creden.Id), []byte(creden.Password)}) | ||
if err != nil { | ||
util.InternalReturn(ctx) | ||
return | ||
} | ||
util.StringReturn(ctx, string(resp.Payload)) | ||
} | ||
|
||
func BuyerBuyProduct(ctx *gin.Context) { | ||
type PurchaseRequest struct { | ||
*models.IdRequest | ||
BuyCount int `json:"count"` | ||
} | ||
input := &PurchaseRequest{} | ||
if !util.CheckParamsValid(ctx, input) { | ||
util.JsonReturn(ctx, models2.WithError(consts.CodeErrorParams, consts.MsgErrorParams)) | ||
return | ||
} | ||
token, _ := json.Marshal(models2.CreateCredentialsWithToken(input.Token)) | ||
resp, err := sdk.ClientExecute(models2.OPERATE_PURCHASE, [][]byte{ | ||
token, []byte(input.Id), []byte(fmt.Sprint(input.BuyCount)), | ||
}) | ||
if err != nil { | ||
util.InternalReturn(ctx) | ||
return | ||
} | ||
util.StringReturn(ctx, string(resp.Payload)) | ||
} | ||
|
||
func BuyerConfirmTransaction(ctx *gin.Context) { | ||
input := &models.IdRequest{} | ||
if !util.CheckParamsValid(ctx, input) { | ||
util.JsonReturn(ctx, models2.WithError(consts.CodeErrorParams, consts.MsgErrorParams)) | ||
return | ||
} | ||
token, _ := json.Marshal(models2.CreateCredentialsWithToken(input.Token)) | ||
resp, err := sdk.ClientExecute(models2.OPERATE_CONFIRM, [][]byte{ | ||
token, []byte(input.Id), | ||
}) | ||
if err != nil { | ||
util.InternalReturn(ctx) | ||
return | ||
} | ||
util.StringReturn(ctx, string(resp.Payload)) | ||
} | ||
|
||
func BuyerCancelTransaction(ctx *gin.Context) { | ||
input := &models.IdRequest{} | ||
if !util.CheckParamsValid(ctx, input) { | ||
util.JsonReturn(ctx, models2.WithError(consts.CodeErrorParams, consts.MsgErrorParams)) | ||
return | ||
} | ||
token, _ := json.Marshal(models2.CreateCredentialsWithToken(input.Token)) | ||
resp, err := sdk.ClientExecute(models2.OPERATE_CANCELORDER, [][]byte{ | ||
token, []byte(input.Id), | ||
}) | ||
if err != nil { | ||
util.InternalReturn(ctx) | ||
return | ||
} | ||
util.StringReturn(ctx, string(resp.Payload)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package controllers | ||
|
||
import ( | ||
ccmodels "github.com/dormao/chaincode_foodmanage/models" | ||
"github.com/dormao/chaincode_foodmanage/models/consts" | ||
"github.com/dormao/restgo_foodmanage/models" | ||
"github.com/dormao/restgo_foodmanage/sdk" | ||
"github.com/dormao/restgo_foodmanage/util" | ||
"github.com/gin-gonic/gin" | ||
"github.com/gin-gonic/gin/binding" | ||
"github.com/hyperledger/fabric-sdk-go/pkg/client/channel" | ||
) | ||
|
||
func RegisterOperator(ctx *gin.Context) { | ||
request := &models.RegisterRequest{} | ||
if ctx.ShouldBindWith(request, binding.Form) != nil { | ||
util.JsonReturn(ctx, ccmodels.WithError(consts.CodeErrorParams, consts.MsgErrorParams)) | ||
return | ||
} | ||
args := [][]byte{[]byte(request.Password)} | ||
var resp channel.Response | ||
var err error | ||
switch request.OperatorType { | ||
case ccmodels.OperatorSeller: | ||
resp, err = sdk.ClientExecute(ccmodels.UnAuthRegisterSeller, args) | ||
case ccmodels.OperatorBuyer: | ||
resp, err = sdk.ClientExecute(ccmodels.UnAuthRegisterBuyer, args) | ||
case ccmodels.OperatorTransporter: | ||
resp, err = sdk.ClientExecute(ccmodels.UnAuthRegisterTransporter, args) | ||
default: | ||
util.JsonReturn(ctx, ccmodels.WithError(consts.CodeErrorOperatorNotFound, consts.MsgErrorOperatorNotFound)) | ||
return | ||
} | ||
if err != nil { | ||
util.JsonReturn(ctx, ccmodels.WithError(consts.CodeErrorGeneral, consts.MsgErrorGeneral)) | ||
return | ||
} | ||
util.StringReturn(ctx, string(resp.Payload)) | ||
} | ||
|
||
/* internal use | ||
func Ping(ctx *gin.Context){ | ||
err := sdk.ExecutePing() | ||
if err != nil { | ||
util.JsonReturn(ctx, ccmodels.WithError(consts.CodeErrorGeneral, consts.MsgErrorGeneral)) | ||
return | ||
} | ||
util.StringReturn(ctx, "pong") | ||
} | ||
func QueryPing(ctx *gin.Context){ | ||
resp, err := sdk.QueryPing() | ||
if err != nil { | ||
util.JsonReturn(ctx, ccmodels.WithError(consts.CodeErrorGeneral, consts.MsgErrorGeneral)) | ||
return | ||
} | ||
util.StringReturn(ctx, string(resp.Payload)) | ||
} | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
package controllers | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
models2 "github.com/dormao/chaincode_foodmanage/models" | ||
"github.com/dormao/chaincode_foodmanage/models/consts" | ||
"github.com/dormao/restgo_foodmanage/models" | ||
"github.com/dormao/restgo_foodmanage/sdk" | ||
"github.com/dormao/restgo_foodmanage/util" | ||
"github.com/gin-gonic/gin" | ||
) | ||
|
||
func SellerLogin(ctx *gin.Context) { | ||
creden := &models.PasswordCredentialRequest{} | ||
if !util.CheckParamsValid(ctx, creden) { | ||
util.JsonReturn(ctx, models2.WithError(consts.CodeErrorParams, consts.MsgErrorParams)) | ||
return | ||
} | ||
resp, err := sdk.ClientExecute(models2.UnAuthLogin, [][]byte{ | ||
[]byte(fmt.Sprint(models2.OperatorSeller)), []byte(creden.Id), []byte(creden.Password)}) | ||
if err != nil { | ||
util.InternalReturn(ctx) | ||
return | ||
} | ||
util.StringReturn(ctx, string(resp.Payload)) | ||
} | ||
|
||
func SellerCreateProduct(ctx *gin.Context) { | ||
input := &models.TokenCredential{} | ||
if !util.CheckParamsValid(ctx, input) { | ||
util.JsonReturn(ctx, models2.WithError(consts.CodeErrorParams, consts.MsgErrorParams)) | ||
return | ||
} | ||
tokenByte, _ := json.Marshal(models2.CreateCredentialsWithToken(input.Token)) | ||
resp, err := sdk.ClientExecute(models2.OPERATE_ADDPRODUCT, [][]byte{tokenByte}) | ||
if err != nil { | ||
util.InternalReturn(ctx) | ||
return | ||
} | ||
util.StringReturn(ctx, string(resp.Payload)) | ||
} | ||
|
||
func SellerSellOnProduct(ctx *gin.Context) { | ||
input := &models.IdRequest{} | ||
if !util.CheckParamsValid(ctx, input) { | ||
util.JsonReturn(ctx, models2.WithError(consts.CodeErrorParams, consts.MsgErrorParams)) | ||
return | ||
} | ||
token, _ := json.Marshal(models2.CreateCredentialsWithToken(input.Token)) | ||
resp, err := sdk.ClientExecute(models2.OPERATE_TAKEONSELL, [][]byte{ | ||
token, []byte(input.Id), | ||
}) | ||
if err != nil { | ||
util.InternalReturn(ctx) | ||
return | ||
} | ||
util.StringReturn(ctx, string(resp.Payload)) | ||
} | ||
|
||
func SellerSellOffProduct(ctx *gin.Context) { | ||
input := &models.IdRequest{} | ||
if !util.CheckParamsValid(ctx, input) { | ||
util.JsonReturn(ctx, models2.WithError(consts.CodeErrorParams, consts.MsgErrorParams)) | ||
return | ||
} | ||
token, _ := json.Marshal(models2.CreateCredentialsWithToken(input.Token)) | ||
resp, err := sdk.ClientExecute(models2.OPERATE_TAKEOFFSELL, [][]byte{ | ||
token, []byte(input.Id), | ||
}) | ||
if err != nil { | ||
util.InternalReturn(ctx) | ||
return | ||
} | ||
util.StringReturn(ctx, string(resp.Payload)) | ||
} | ||
|
||
func SellerTransmit(ctx *gin.Context) { | ||
type TransmitRequest struct { | ||
*models.IdRequest | ||
TransporterId string `json:"transporter_id"` | ||
} | ||
input := &TransmitRequest{} | ||
if !util.CheckParamsValid(ctx, input) { | ||
util.JsonReturn(ctx, models2.WithError(consts.CodeErrorParams, consts.MsgErrorParams)) | ||
return | ||
} | ||
token, _ := json.Marshal(models2.CreateCredentialsWithToken(input.Token)) | ||
resp, err := sdk.ClientExecute(models2.OPERATE_TRANSMIT, [][]byte{ | ||
token, []byte(input.Id), []byte(input.TransporterId), | ||
}) | ||
if err != nil { | ||
util.InternalReturn(ctx) | ||
return | ||
} | ||
util.StringReturn(ctx, string(resp.Payload)) | ||
} | ||
|
||
func SellerCancelTransaction(ctx *gin.Context) { | ||
input := &models.IdRequest{} | ||
if !util.CheckParamsValid(ctx, input) { | ||
util.JsonReturn(ctx, models2.WithError(consts.CodeErrorParams, consts.MsgErrorParams)) | ||
return | ||
} | ||
token, _ := json.Marshal(models2.CreateCredentialsWithToken(input.Token)) | ||
resp, err := sdk.ClientExecute(models2.OPERATE_CANCELTRANSPORT, [][]byte{ | ||
token, []byte(input.Id), | ||
}) | ||
if err != nil { | ||
util.InternalReturn(ctx) | ||
return | ||
} | ||
util.StringReturn(ctx, string(resp.Payload)) | ||
} | ||
|
||
func SellerUpdateProduct(ctx *gin.Context) { | ||
input := &models.SellerProductInfoRequest{} | ||
if !util.CheckParamsValid(ctx, input) { | ||
util.JsonReturn(ctx, models2.WithError(consts.CodeErrorParams, consts.MsgErrorParams)) | ||
return | ||
} | ||
token, _ := json.Marshal(models2.CreateCredentialsWithToken(input.Token)) | ||
req, _ := json.Marshal(&models2.ProductUpdateRequest{ | ||
DataModel: &models2.DataModel{Id: input.Id}, | ||
EachPrice: uint64(input.EachPrice), | ||
Description: input.Description, | ||
Inventory: uint32(input.Inventory), | ||
TransportAmount: uint64(input.TransportAmount), | ||
SpecifiedTemperature: byte(input.SpecifiedTemperature), | ||
}) | ||
resp, err := sdk.ClientExecute(models2.OPERATE_UPDATE_PRODUCT, [][]byte{token, req}) | ||
if err != nil { | ||
util.InternalReturn(ctx) | ||
return | ||
} | ||
util.StringReturn(ctx, string(resp.Payload)) | ||
} |
Oops, something went wrong.