Commit 58cc8183 authored by suyanlong's avatar suyanlong

Add two api function

parent 278041a9
Pipeline #8428 failed with stages
......@@ -37,11 +37,30 @@ func (g *Server) sendTransaction(c *gin.Context) {
}
// TODO
c.JSON(http.StatusOK, res)
panic("implement me")
}
// GET 127.0.0.1:80/api/QueryOuterMeta/:did
func (g *Server) QueryOuterMeta(c *gin.Context) {
did := c.Param("did")
data := g.mg.AppChain(did).QueryOuterMeta()
c.JSON(http.StatusOK, data)
}
// GET 127.0.0.1:80/api/QueryIBTP/:did/:to/:id
func (g *Server) QueryIBTP(c *gin.Context) {
id := c.Param("id")
to := c.Param("to")
did := c.Param("did")
ib, err := g.mg.AppChain(did).QueryIBTP(to, id)
if err != nil {
c.JSON(http.StatusOK, NewError(http.StatusOK, http.StatusInternalServerError, err.Error()))
} else {
c.JSON(http.StatusOK, ib)
}
}
// GET 127.0.0.1:80/api/getTransaction/:hash
func (g *Server) getTransaction(c *gin.Context) {
panic("implement me")
......
......@@ -16,6 +16,7 @@ import (
"github.com/sirupsen/logrus"
"gitlab.33.cn/link33/sidecar/cmd/sidecar/client"
"gitlab.33.cn/link33/sidecar/internal/appchain"
"gitlab.33.cn/link33/sidecar/internal/repo"
"gitlab.33.cn/link33/sidecar/internal/router"
"gitlab.33.cn/link33/sidecar/model/pb"
......@@ -30,13 +31,14 @@ type Server struct {
ctx context.Context
cancel context.CancelFunc
router router.Router
mg *appchain.Manager
}
type response struct {
Data []byte `json:"data"`
}
func NewServer(config *repo.Config, router router.Router, logger logrus.FieldLogger) *Server {
func NewServer(config *repo.Config, router router.Router, mg *appchain.Manager, logger logrus.FieldLogger) *Server {
ctx, cancel := context.WithCancel(context.Background())
// load the casbin model and policy from files, database is also supported.
......@@ -59,12 +61,15 @@ func NewServer(config *repo.Config, router router.Router, logger logrus.FieldLog
func (g *Server) Start() error {
g.engine.Use(gin.Recovery())
v1 := g.engine.Group("/v1")
v1 := g.engine.Group("/v1/api")
// g.engine.Use(gin.Recovery())
{
v1.POST(client.RegisterAppchainUrl, g.registerAppchain)
v1.POST(client.UpdateAppchainUrl, g.updateAppchain)
v1.GET(client.GetAppchainUrl, g.getAppchain)
v1.GET("/QueryOuterMeta/:did", g.QueryOuterMeta)
v1.GET("/QueryIBTP/:did/:to/:id", g.QueryIBTP)
}
go func() {
......
......@@ -18,6 +18,8 @@ const (
AuditAppchainUrl = "appchain/audit"
GetAppchainUrl = "appchain/get"
RegisterRuleUrl = "rule/register"
QueryOuterMeta = "QueryOuterMeta"
QueryIBTP = "QueryIBTP"
)
func httpGet(url string) ([]byte, error) {
......
......@@ -68,7 +68,7 @@ func NewSidecar(repoRoot string, config *repo.Config) (internal.Launcher, error)
r.Adds(managerPort.Ports())
tool.Asset(err)
apiServer := api.NewServer(config, r, loggers.Logger(loggers.ApiServer))
apiServer := api.NewServer(config, r, managerPort, loggers.Logger(loggers.ApiServer))
ctx, cancel := context.WithCancel(context.Background())
return &App{
storage: store,
......
......@@ -47,11 +47,11 @@ func newAppchain(ctx context.Context, client plugins.Client, cryptor txcrypto.Cr
}
}
func newAppchains(ctx context.Context, clients []plugins.Client, cryptor txcrypto.Cryptor, logger logrus.FieldLogger) []*appChain {
var ps []*appChain
func newAppchains(ctx context.Context, clients []plugins.Client, cryptor txcrypto.Cryptor, logger logrus.FieldLogger) map[string]*appChain {
ps := make(map[string]*appChain)
for _, c := range clients {
p := newAppchain(ctx, c, cryptor, logger)
ps = append(ps, p)
ps[p.ID()] = p
}
return ps
......
......@@ -15,7 +15,7 @@ type Manager struct {
ctx context.Context
cancel context.CancelFunc
g *tool.Group
appChains []*appChain
appChains map[string]*appChain
}
func NewManager(clients []plugins.Client, cryptor txcrypto.Cryptor, logger logrus.FieldLogger) *Manager {
......@@ -57,3 +57,7 @@ func (m *Manager) Ports() []port.Port {
}
return ports
}
func (m *Manager) AppChain(id string) AppChain {
return m.appChains[id]
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment