Commit 4571e7a4 authored by suyanlong's avatar suyanlong

Add api server function

parent 612d9e9e
Pipeline #7985 canceled with stages
...@@ -2,12 +2,19 @@ package api ...@@ -2,12 +2,19 @@ package api
import ( import (
"context" "context"
"encoding/json"
"fmt" "fmt"
"net/http"
"path/filepath"
"github.com/meshplus/bitxhub-kit/crypto/asym"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/link33/sidecar/cmd/sidecar/client" "github.com/link33/sidecar/cmd/sidecar/client"
"github.com/link33/sidecar/internal/peermgr"
"github.com/link33/sidecar/internal/repo" "github.com/link33/sidecar/internal/repo"
"github.com/link33/sidecar/model/pb" "github.com/link33/sidecar/model/pb"
appchainmgr "github.com/meshplus/bitxhub-core/appchain-mgr"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
) )
...@@ -17,13 +24,14 @@ type Server struct { ...@@ -17,13 +24,14 @@ type Server struct {
logger logrus.FieldLogger logger logrus.FieldLogger
ctx context.Context ctx context.Context
cancel context.CancelFunc cancel context.CancelFunc
peerMgr peermgr.PeerManager
} }
type response struct { type response struct {
Data []byte `json:"data"` 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()) ctx, cancel := context.WithCancel(context.Background())
gin.SetMode(gin.ReleaseMode) gin.SetMode(gin.ReleaseMode)
router := gin.New() router := gin.New()
...@@ -33,7 +41,8 @@ func NewServer(config *repo.Config, logger logrus.FieldLogger) (*Server, error) ...@@ -33,7 +41,8 @@ func NewServer(config *repo.Config, logger logrus.FieldLogger) (*Server, error)
logger: logger, logger: logger,
ctx: ctx, ctx: ctx,
cancel: cancel, cancel: cancel,
}, nil peerMgr: peerMgr,
}
} }
func (g *Server) Start() error { func (g *Server) Start() error {
...@@ -72,9 +81,96 @@ func (g *Server) registerAppchain(c *gin.Context) { ...@@ -72,9 +81,96 @@ func (g *Server) registerAppchain(c *gin.Context) {
} }
func (g *Server) sendAppchain(c *gin.Context, appchainType pb.Message_Type) { 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") 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) { 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 ( ...@@ -10,6 +10,7 @@ import (
"github.com/meshplus/bitxhub-kit/storage/leveldb" "github.com/meshplus/bitxhub-kit/storage/leveldb"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
"github.com/link33/sidecar/api"
_ "github.com/link33/sidecar/imports" _ "github.com/link33/sidecar/imports"
"github.com/link33/sidecar/internal" "github.com/link33/sidecar/internal"
"github.com/link33/sidecar/internal/appchain" "github.com/link33/sidecar/internal/appchain"
...@@ -31,6 +32,7 @@ type App struct { ...@@ -31,6 +32,7 @@ type App struct {
config *repo.Config config *repo.Config
logger logrus.FieldLogger logger logrus.FieldLogger
manger internal.Launcher manger internal.Launcher
apiServer *api.Server
} }
// NewSidecar instantiates sidecar instance. // NewSidecar instantiates sidecar instance.
...@@ -47,7 +49,7 @@ func NewSidecar(repoRoot string, config *repo.Config) (internal.Launcher, error) ...@@ -47,7 +49,7 @@ func NewSidecar(repoRoot string, config *repo.Config) (internal.Launcher, error)
var ( var (
//ck checker.Checker //ck checker.Checker
//cryptor txcrypto.Cryptor //cryptor txcrypto.Cryptor
//apiServer *api.Server //apiServer *apiServer.Server
) )
r := router.NewRouter(loggers.Logger(loggers.Router)) r := router.NewRouter(loggers.Logger(loggers.Router))
pm, err := peermgr.New(config, r, nodePrivKey, privateKey, 1, loggers.Logger(loggers.PeerMgr)) pm, err := peermgr.New(config, r, nodePrivKey, privateKey, 1, loggers.Logger(loggers.PeerMgr))
...@@ -61,6 +63,7 @@ func NewSidecar(repoRoot string, config *repo.Config) (internal.Launcher, error) ...@@ -61,6 +63,7 @@ func NewSidecar(repoRoot string, config *repo.Config) (internal.Launcher, error)
r.Adds(clientPort) r.Adds(clientPort)
mg, err := manger.NewManager(addr.String(), r, pm, appchainMgr, loggers.Logger(loggers.Manger)) mg, err := manger.NewManager(addr.String(), r, pm, appchainMgr, loggers.Logger(loggers.Manger))
Asset(err) Asset(err)
apiServer := api.NewServer(config, pm, loggers.Logger(loggers.ApiServer))
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
return &App{ return &App{
storage: store, storage: store,
...@@ -69,16 +72,24 @@ func NewSidecar(repoRoot string, config *repo.Config) (internal.Launcher, error) ...@@ -69,16 +72,24 @@ func NewSidecar(repoRoot string, config *repo.Config) (internal.Launcher, error)
cancel: cancel, cancel: cancel,
config: config, config: config,
manger: mg, manger: mg,
apiServer: apiServer,
}, nil }, nil
} }
// Start starts three main components of sidecar app // Start starts three main components of sidecar app
func (s *App) Start() error { func (s *App) Start() error {
if err := s.apiServer.Start(); err != nil {
return err
}
return s.manger.Start() return s.manger.Start()
} }
// Stop stops three main components of sidecar app // Stop stops three main components of sidecar app
func (s *App) Stop() error { func (s *App) Stop() error {
if err := s.apiServer.Stop(); err != nil {
return err
}
return s.manger.Stop() 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