Commit 4571e7a4 authored by suyanlong's avatar suyanlong

Add api server function

parent 612d9e9e
Pipeline #7985 canceled with stages
......@@ -2,38 +2,47 @@ package api
import (
"context"
"encoding/json"
"fmt"
"net/http"
"path/filepath"
"github.com/meshplus/bitxhub-kit/crypto/asym"
"github.com/gin-gonic/gin"
"github.com/link33/sidecar/cmd/sidecar/client"
"github.com/link33/sidecar/internal/peermgr"
"github.com/link33/sidecar/internal/repo"
"github.com/link33/sidecar/model/pb"
appchainmgr "github.com/meshplus/bitxhub-core/appchain-mgr"
"github.com/sirupsen/logrus"
)
type Server struct {
router *gin.Engine
config *repo.Config
logger logrus.FieldLogger
ctx context.Context
cancel context.CancelFunc
router *gin.Engine
config *repo.Config
logger logrus.FieldLogger
ctx context.Context
cancel context.CancelFunc
peerMgr peermgr.PeerManager
}
type response struct {
Data []byte `json:"data"`
}
func NewServer(config *repo.Config, logger logrus.FieldLogger) (*Server, error) {
func NewServer(config *repo.Config, peerMgr peermgr.PeerManager, logger logrus.FieldLogger) *Server {
ctx, cancel := context.WithCancel(context.Background())
gin.SetMode(gin.ReleaseMode)
router := gin.New()
return &Server{
router: router,
config: config,
logger: logger,
ctx: ctx,
cancel: cancel,
}, nil
router: router,
config: config,
logger: logger,
ctx: ctx,
cancel: cancel,
peerMgr: peerMgr,
}
}
func (g *Server) Start() error {
......@@ -72,9 +81,96 @@ func (g *Server) registerAppchain(c *gin.Context) {
}
func (g *Server) sendAppchain(c *gin.Context, appchainType pb.Message_Type) {
// target pier id
pierID := c.Query("pier_id")
var res pb.Response
var appchain appchainmgr.Appchain
if err := c.BindJSON(&appchain); err != nil {
res.Data = []byte(err.Error())
c.JSON(http.StatusBadRequest, res)
return
}
data, err := json.Marshal(appchain)
if err != nil {
g.logger.Errorln(err)
}
msg := pb.Msg(appchainType, true, data)
ackMsg, err := g.peerMgr.SendByID(pierID, msg)
if err != nil {
res.Data = []byte(err.Error())
c.JSON(http.StatusInternalServerError, res)
return
}
g.handleAckAppchain(c, ackMsg)
panic("implement me")
}
func (g *Server) handleAckAppchain(c *gin.Context, msg *pb.Message) {
app := appchainmgr.Appchain{}
if err := json.Unmarshal(msg.Payload.Data, &app); err != nil {
g.logger.Error(err)
return
}
res := &response{}
switch msg.Type {
case pb.Message_APPCHAIN_REGISTER:
res.Data = []byte(fmt.Sprintf("appchain register successfully, id is %s\n", app.ID))
case pb.Message_APPCHAIN_UPDATE:
res.Data = []byte(fmt.Sprintf("appchain update successfully, id is %s\n", app.ID))
case pb.Message_APPCHAIN_GET:
res.Data = msg.Payload.Data
}
c.JSON(http.StatusOK, res)
}
func (g *Server) getAppchain(c *gin.Context) {
panic("implement me")
res := &response{}
// target pier id
selfPierID, err := g.getSelfPierID(c)
if err != nil {
c.JSON(http.StatusInternalServerError, err.Error())
return
}
targetPierID := c.Query("pier_id")
appchain := &appchainmgr.Appchain{
ID: selfPierID,
}
data, err := json.Marshal(appchain)
if err != nil {
c.JSON(http.StatusInternalServerError, err.Error())
return
}
msg := pb.Msg(pb.Message_APPCHAIN_GET, true, data)
ackMsg, err := g.peerMgr.SendByID(targetPierID, msg)
if err != nil {
res.Data = []byte(err.Error())
c.JSON(http.StatusInternalServerError, res)
return
}
g.handleAckAppchain(c, ackMsg)
}
func (g *Server) getSelfPierID(ctx *gin.Context) (string, error) {
repoRoot := g.config.RepoRoot
keyPath := filepath.Join(repoRoot, "key.json")
privKey, err := asym.RestorePrivateKey(keyPath, "link33")
if err != nil {
return "", err
}
address, err := privKey.PublicKey().Address()
if err != nil {
return "", err
}
return address.String(), nil
}
......@@ -10,6 +10,7 @@ import (
"github.com/meshplus/bitxhub-kit/storage/leveldb"
"github.com/sirupsen/logrus"
"github.com/link33/sidecar/api"
_ "github.com/link33/sidecar/imports"
"github.com/link33/sidecar/internal"
"github.com/link33/sidecar/internal/appchain"
......@@ -31,6 +32,7 @@ type App struct {
config *repo.Config
logger logrus.FieldLogger
manger internal.Launcher
apiServer *api.Server
}
// NewSidecar instantiates sidecar instance.
......@@ -47,7 +49,7 @@ func NewSidecar(repoRoot string, config *repo.Config) (internal.Launcher, error)
var (
//ck checker.Checker
//cryptor txcrypto.Cryptor
//apiServer *api.Server
//apiServer *apiServer.Server
)
r := router.NewRouter(loggers.Logger(loggers.Router))
pm, err := peermgr.New(config, r, nodePrivKey, privateKey, 1, loggers.Logger(loggers.PeerMgr))
......@@ -61,24 +63,33 @@ func NewSidecar(repoRoot string, config *repo.Config) (internal.Launcher, error)
r.Adds(clientPort)
mg, err := manger.NewManager(addr.String(), r, pm, appchainMgr, loggers.Logger(loggers.Manger))
Asset(err)
apiServer := api.NewServer(config, pm, loggers.Logger(loggers.ApiServer))
ctx, cancel := context.WithCancel(context.Background())
return &App{
storage: store,
logger: logger,
ctx: ctx,
cancel: cancel,
config: config,
manger: mg,
storage: store,
logger: logger,
ctx: ctx,
cancel: cancel,
config: config,
manger: mg,
apiServer: apiServer,
}, nil
}
// Start starts three main components of sidecar app
func (s *App) Start() error {
if err := s.apiServer.Start(); err != nil {
return err
}
return s.manger.Start()
}
// Stop stops three main components of sidecar app
func (s *App) Stop() error {
if err := s.apiServer.Stop(); err != nil {
return err
}
return s.manger.Stop()
}
......
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