modify the route type

This commit is contained in:
sulenn 2021-04-17 09:33:02 +08:00
parent 0cb796af0c
commit d987c30beb
13 changed files with 83 additions and 173 deletions

View File

@ -1,10 +0,0 @@
package structs
type InstallChaincodeOption struct {
Orgname string `json:"orgnization_name"`
Peers []string `json:"peers"`
ChaincodeName string `json:"chaincode_name"`
ChaincodePath string `json:"chaincode_path"`
ChaincodeVersion string `json:"chaincode_version"`
ChaincodeType string `json:"chaincode_type"` // 链码所用语言
}

View File

@ -1,17 +0,0 @@
package structs
type CreateChannelOption struct {
CommonOption
ChannelConfigPath string `json:"channel_config_path"`
}
type JoinChannelOption struct {
CommonOption
Peers []string `json:"peers"`
}
// 公共属性
type CommonOption struct {
ChannelName string `json:"channel_name" binding:"Required"`
Orgname string `json:"orgnization_name"`
}

View File

@ -1,25 +0,0 @@
package structs
type InstantiateChaincodeOption struct {
CommonOption
Peers []string `json:"peers"`
ChaincodeName string `json:"chaincode_name"`
ChaincodeVersion string `json:"chaincode_version"`
ChaincodeType string `json:"chaincode_type"` // 链码所用语言
}
type InvokeChaincodeOption struct {
CommonOption
Peers []string `json:"peers"`
ChaincodeName string `json:"chaincode_name"`
FunctionName string `json:"function_name"` // 调用链码中的方法名
Args []string `json:"args"` // 调用链码时传入的参数
}
type QueryChaincodeOption struct {
CommonOption
ChaincodeName string `json:"chaincode_name"`
Peer string `json:"peer"`
FunctionName string `json:"function_name"` // 调用链码中的方法名
Args string `json:"args"` // 调用链码时传入的参数
}

View File

@ -0,0 +1,15 @@
package structs
type FiscoBcos struct {
RequestType string `json:"request-type" binding:"Required"`
// repo 相关
Owner string `json:"owner"` // 创始人
TokenName string `json:"token_name"` // 项目代号(默认等同于项目名)
TotalSupply uint64 `json:"total_supply"` // 项目初始金额
CurSupply uint64 `json:"cur_supply"` // 项目当前金额
TokenBalance [][]interface{} `json:"token_balance"` // 用户初始Token分配
// user 相关
Username string `json:"username"` // 用户名
}

View File

@ -2,10 +2,11 @@ package structs
// CreateRepoOption is options when to create a repository
type CreateRepoOption struct {
Owner string `json:"username" binding:"Required"` // 创始人
TotalSupply uint64 `json:"total_supply" binding:"Required"` // 项目初始金额
TokenName string `json:"token_name" binding:"Required"` // 项目名
TokenBalance [][]interface{} `json:"token_balance"` // 用户初始Token分配
Owner string `json:"owner"` // 创始人
TokenName string `json:"token_name"` // 项目代号(默认等同于项目名)
TotalSupply uint64 `json:"total_supply"` // 项目初始金额
CurSupply uint64 `json:"cur_supply"` // 项目当前金额
TokenBalance [][]interface{} `json:"token_balance"` // 用户初始Token分配
}
type Repo struct {

View File

@ -1,7 +1,7 @@
package structs
type User struct {
Username string `json:"username" binding:"Required"`
Username string `json:"username"`
TokenName string `json:"token_name"`
}
@ -11,8 +11,3 @@ type UserBalance struct {
}
type UserBalanceList []*UserBalance
// type CreateUserOption struct {
// Username string `json:"username" binding:"Required"`
// Orgname string `json:"orgnization_name" binding:"Required"`
// }

View File

@ -1,18 +0,0 @@
package chaincode
import (
"gitea.com/macaron/macaron"
api "github.com/sulenn/trustie-fisco-bcos/modules/structs"
)
func Install(ctx *macaron.Context, opt api.InstallChaincodeOption) {
var installChaincodeOption = &api.InstallChaincodeOption{
Orgname: opt.Orgname,
Peers: opt.Peers,
ChaincodeName: opt.ChaincodeName,
ChaincodePath: opt.ChaincodePath,
ChaincodeVersion: opt.ChaincodeVersion,
ChaincodeType: opt.ChaincodeType,
}
ctx.JSON(200, installChaincodeOption)
}

View File

@ -1,25 +0,0 @@
package channel
import (
api "github.com/sulenn/trustie-fisco-bcos/modules/structs"
"gitea.com/macaron/macaron"
)
// 创建 channel
func Create(ctx *macaron.Context, opt api.CreateChannelOption) {
var channel = &api.CreateChannelOption{
CommonOption: api.CommonOption{ChannelName: opt.ChannelName, Orgname: opt.Orgname},
ChannelConfigPath: opt.ChannelConfigPath,
}
ctx.JSON(200, channel)
}
// 节点加入通道
func Join(ctx *macaron.Context, opt api.JoinChannelOption) {
var joinChannelOption = &api.JoinChannelOption{
CommonOption: api.CommonOption{ChannelName: opt.ChannelName, Orgname: opt.Orgname},
Peers: opt.Peers,
}
ctx.JSON(200, joinChannelOption)
}

View File

@ -1,42 +0,0 @@
package channel
import (
"gitea.com/macaron/macaron"
api "github.com/sulenn/trustie-fisco-bcos/modules/structs"
)
// 在通道中实例化链码
func InstantiateChaincode(ctx *macaron.Context, opt api.InstantiateChaincodeOption) {
var instantiateChaincodeOption = &api.InstantiateChaincodeOption{
CommonOption: api.CommonOption{ChannelName: opt.ChannelName, Orgname: opt.Orgname},
Peers: opt.Peers,
ChaincodeName: opt.ChaincodeName,
ChaincodeVersion: opt.ChaincodeVersion,
ChaincodeType: opt.ChaincodeType,
}
ctx.JSON(200, instantiateChaincodeOption)
}
// 调用链码,完成交易
func InvokeChaincode(ctx *macaron.Context, opt api.InvokeChaincodeOption) {
var invokeChaincodeOption = &api.InvokeChaincodeOption{
CommonOption: api.CommonOption{ChannelName: opt.ChannelName, Orgname: opt.Orgname},
Peers: opt.Peers,
ChaincodeName: opt.ChaincodeName,
FunctionName: opt.FunctionName,
Args: opt.Args,
}
ctx.JSON(200, invokeChaincodeOption)
}
// 查询链码中数据
func QueryChaincode(ctx *macaron.Context) {
var queryChaincodeOption = &api.QueryChaincodeOption{
CommonOption: api.CommonOption{ChannelName: ctx.Params(":channelName"), Orgname: ctx.Query("orgnization_name")},
ChaincodeName: ctx.Params(":chaincodeName"),
Peer: ctx.Query("peer"),
FunctionName: ctx.Query("function_name"),
Args: ctx.Query("args"),
}
ctx.JSON(200, queryChaincodeOption)
}

View File

@ -0,0 +1,36 @@
package fiscobcos
import (
"log"
"net/http"
"gitea.com/macaron/macaron"
api "github.com/sulenn/trustie-fisco-bcos/modules/structs"
"github.com/sulenn/trustie-fisco-bcos/routers/repo"
"github.com/sulenn/trustie-fisco-bcos/routers/user"
)
// 路由统一处理
func HandleAllRoutes(ctx *macaron.Context, opt api.FiscoBcos, logger *log.Logger) {
requestType := opt.RequestType
switch requestType {
case "create repo":
repo.Create(ctx,
api.CreateRepoOption{
Owner: opt.Owner,
TokenName: opt.TokenName,
TotalSupply: opt.TotalSupply,
CurSupply: opt.CurSupply,
TokenBalance: opt.TokenBalance,
},
logger)
case "query repo basic info":
repo.QueryBasic(ctx, opt.TokenName, logger)
case "query user balance of single repo":
user.SelectUserBalance(ctx, opt.Username, opt.TokenName, logger)
case "query user balance of all repos":
user.SelectUserAllBalance(ctx, opt.Username, logger)
default:
ctx.JSON(http.StatusNotFound, "the request type not found!")
}
}

View File

@ -99,8 +99,7 @@ func Create(ctx *macaron.Context, opt api.CreateRepoOption, logger *log.Logger)
ctx.JSON(http.StatusOK, structs.ResRepoUnsucc)
}
func QueryBasic(ctx *macaron.Context, logger *log.Logger) {
tokenName := ctx.Query("tokenname")
func QueryBasic(ctx *macaron.Context, tokenName string, logger *log.Logger) {
if tokenName == "" {
ctx.JSON(http.StatusOK, api.StringEmpty)
return

View File

@ -4,8 +4,7 @@ import (
"gitea.com/macaron/binding"
"gitea.com/macaron/macaron"
api "github.com/sulenn/trustie-fisco-bcos/modules/structs"
"github.com/sulenn/trustie-fisco-bcos/routers/repo"
"github.com/sulenn/trustie-fisco-bcos/routers/user"
"github.com/sulenn/trustie-fisco-bcos/routers/fiscobcos"
)
// NewMacaron is to create a new macaron
@ -24,12 +23,17 @@ func RegisterRoutes(m *macaron.Macaron) {
ctx.JSON(200, "hello world")
})
m.Group("/user", func() {
// m.Post("/register", bind(api.CreateUserOption{}), user.Register) // 注册用户
m.Get("/query/allbalance", user.SelectUserAllBalance) // 查询用户所有项目Token
m.Get("/query/balance", user.SelectUserBalance) // 查询用户单个项目Token
// 统一路由入口
m.Group("/trustie", func() {
m.Post("/fiscobcos", bind(api.FiscoBcos{}), fiscobcos.HandleAllRoutes)
})
// m.Group("/user", func() {
// // m.Post("/register", bind(api.CreateUserOption{}), user.Register) // 注册用户
// m.Get("/query/allbalance", user.SelectUserAllBalance) // 查询用户所有项目Token
// m.Get("/query/balance", user.SelectUserBalance) // 查询用户单个项目Token
// })
// m.Group("/channel", func() {
// m.Post("/create", bind(api.CreateChannelOption{}), channel.Create) // 创建通道
// m.Post("/join", bind(api.JoinChannelOption{}), channel.Join) // Perr 节点加入通道
@ -44,17 +48,17 @@ func RegisterRoutes(m *macaron.Macaron) {
// m.Post("/install", bind(api.InstallChaincodeOption{}), chaincode.Install) // 安装链码
// })
m.Group("/repos", func() {
m.Post("/create", bind(api.CreateRepoOption{}), repo.Create) // 在区块链账本中新建一个项目
// m.Group("/amount", func() { // 和 token 值相关路由
// m.Post("/transfer", bind(api.TransferTokenOption{}), repo.Transfer) // 转账
// m.Post("/add", bind(api.AddTokenOption{}), repo.Add) // 增加账户余额
// m.Post("/minus", bind(api.MinusTokenOption{}), repo.Minus) // 减少账户余额
m.Get("/query/basic", repo.QueryBasic) // 查询账户余额
// m.Get("/repo/query", repo.QueryRepoAmount) // 查询项目总金额
// })
// m.Group("/commit", func() {
// m.Post("/upload", bind(api.UploadCommitOption{}), repo.Upload) // 记录 commit 数据
// })
})
// m.Group("/repos", func() {
// m.Post("/create", bind(api.CreateRepoOption{}), repo.Create) // 在区块链账本中新建一个项目
// // m.Group("/amount", func() { // 和 token 值相关路由
// // m.Post("/transfer", bind(api.TransferTokenOption{}), repo.Transfer) // 转账
// // m.Post("/add", bind(api.AddTokenOption{}), repo.Add) // 增加账户余额
// // m.Post("/minus", bind(api.MinusTokenOption{}), repo.Minus) // 减少账户余额
// m.Get("/query/basic", repo.QueryBasic) // 查询账户余额
// // m.Get("/repo/query", repo.QueryRepoAmount) // 查询项目总金额
// // })
// // m.Group("/commit", func() {
// // m.Post("/upload", bind(api.UploadCommitOption{}), repo.Upload) // 记录 commit 数据
// // })
// })
}

View File

@ -22,8 +22,7 @@ import (
// }
// select user all balance
func SelectUserAllBalance(ctx *macaron.Context, logger *log.Logger) {
username := ctx.Query("username")
func SelectUserAllBalance(ctx *macaron.Context, username string, logger *log.Logger) {
if username == "" {
ctx.JSON(http.StatusOK, api.StringEmpty)
return
@ -65,9 +64,7 @@ func SelectUserAllBalance(ctx *macaron.Context, logger *log.Logger) {
}
// select user balance
func SelectUserBalance(ctx *macaron.Context, logger *log.Logger) {
username := ctx.Query("username")
tokenName := ctx.Query("tokenname")
func SelectUserBalance(ctx *macaron.Context, username string, tokenName string, logger *log.Logger) {
if username == "" || tokenName == "" {
ctx.JSON(http.StatusOK, api.StringEmpty)
return