Commit 88993240 authored by suyanlong's avatar suyanlong

1.Add Router interface function.

2.Fixed bug for Manger
parent 7277a275
......@@ -2,7 +2,6 @@ package api
import (
"context"
"encoding/json"
"fmt"
"net/http"
"path/filepath"
......@@ -13,44 +12,43 @@ import (
"github.com/sirupsen/logrus"
"gitlab.33.cn/link33/sidecar/cmd/sidecar/client"
"gitlab.33.cn/link33/sidecar/internal/peermgr"
"gitlab.33.cn/link33/sidecar/internal/repo"
"gitlab.33.cn/link33/sidecar/internal/rulemgr"
"gitlab.33.cn/link33/sidecar/internal/router"
"gitlab.33.cn/link33/sidecar/model/pb"
"gitlab.33.cn/link33/sidecar/pkg/json"
"gitlab.33.cn/link33/sidecar/tool"
)
type Server struct {
router *gin.Engine
config *repo.Config
logger logrus.FieldLogger
ctx context.Context
cancel context.CancelFunc
peerMgr peermgr.PeerManager
rule *rulemgr.RuleMgr
engine *gin.Engine
config *repo.Config
logger logrus.FieldLogger
ctx context.Context
cancel context.CancelFunc
router router.Router
}
type response struct {
Data []byte `json:"data"`
}
func NewServer(config *repo.Config, peerMgr peermgr.PeerManager, rule *rulemgr.RuleMgr, logger logrus.FieldLogger) *Server {
func NewServer(config *repo.Config, router router.Router, logger logrus.FieldLogger) *Server {
ctx, cancel := context.WithCancel(context.Background())
gin.SetMode(gin.ReleaseMode)
router := gin.New()
r := gin.New()
return &Server{
router: router,
config: config,
logger: logger,
ctx: ctx,
cancel: cancel,
peerMgr: peerMgr,
rule: rule,
engine: r,
config: config,
logger: logger,
ctx: ctx,
cancel: cancel,
router: router,
}
}
func (g *Server) Start() error {
g.router.Use(gin.Recovery())
v1 := g.router.Group("/v1")
g.engine.Use(gin.Recovery())
v1 := g.engine.Group("/v1")
{
v1.POST(client.RegisterAppchainUrl, g.registerAppchain)
v1.POST(client.UpdateAppchainUrl, g.updateAppchain)
......@@ -59,14 +57,11 @@ func (g *Server) Start() error {
go func() {
go func() {
err := g.router.Run(fmt.Sprintf(":%d", g.config.Port.Http))
if err != nil {
panic(err)
}
tool.Asset(g.engine.Run(fmt.Sprintf(":%d", g.config.Port.Http)))
}()
<-g.ctx.Done()
}()
return g.peerMgr.RegisterMsgHandler(pb.Message_RULE_DEPLOY, g.rule.HandleRule)
return nil
}
func (g *Server) Stop() error {
......@@ -99,7 +94,7 @@ func (g *Server) sendAppchain(c *gin.Context, appchainType pb.Message_Type) {
}
msg := pb.Msg(appchainType, true, data)
ackMsg, err := g.peerMgr.SendByID(sidecarID, msg)
ackMsg, err := g.router.Send(sidecarID, msg)
if err != nil {
res.Data = []byte(err.Error())
c.JSON(http.StatusInternalServerError, res)
......@@ -150,7 +145,7 @@ func (g *Server) getAppchain(c *gin.Context) {
}
msg := pb.Msg(pb.Message_APPCHAIN_GET, true, data)
ackMsg, err := g.peerMgr.SendByID(targetSidecarID, msg)
ackMsg, err := g.router.Send(targetSidecarID, msg)
if err != nil {
res.Data = []byte(err.Error())
c.JSON(http.StatusInternalServerError, res)
......
......@@ -66,12 +66,11 @@ func NewSidecar(repoRoot string, config *repo.Config) (internal.Launcher, error)
cryptor := txcrypto.NewCryptor(nil, appchainMgr, privateKey)
clientPort := appchain.NewPorts(clients, cryptor, logger)
r.Adds(clientPort)
mg, err := manger.NewManager(addr.String(), pm, appchainMgr, loggers.Logger(loggers.Manger))
tool.Asset(err)
rule, err := rulemgr.New(store, loggers.Logger(loggers.Rule))
tool.Asset(err)
apiServer := api.NewServer(config, pm, rule, loggers.Logger(loggers.ApiServer))
mg, err := manger.NewManager(addr.String(), pm, appchainMgr, rule, loggers.Logger(loggers.Manger))
tool.Asset(err)
apiServer := api.NewServer(config, r, loggers.Logger(loggers.ApiServer))
ctx, cancel := context.WithCancel(context.Background())
return &App{
storage: store,
......
......@@ -6,6 +6,7 @@ import (
"gitlab.33.cn/link33/sidecar/internal/peermgr"
"gitlab.33.cn/link33/sidecar/internal/port"
"gitlab.33.cn/link33/sidecar/internal/rulemgr"
"gitlab.33.cn/link33/sidecar/model/pb"
"gitlab.33.cn/link33/sidecar/pkg/json"
)
......@@ -14,6 +15,7 @@ type Manager struct {
logger logrus.FieldLogger
appchainMgr appchainmgr.AppchainMgr
pm peermgr.PeerManager
rule *rulemgr.RuleMgr
// router router.Router
}
......@@ -25,11 +27,12 @@ func (mgr *Manager) Stop() error {
return mgr.pm.Stop()
}
func NewManager(addr string, pm peermgr.PeerManager, Mgr appchainmgr.AppchainMgr, logger logrus.FieldLogger) (*Manager, error) {
func NewManager(addr string, pm peermgr.PeerManager, Mgr appchainmgr.AppchainMgr, rule *rulemgr.RuleMgr, logger logrus.FieldLogger) (*Manager, error) {
am := &Manager{
appchainMgr: Mgr,
logger: logger,
pm: pm,
rule: rule,
}
err := pm.RegisterMultiMsgHandler([]pb.Message_Type{
......@@ -40,7 +43,10 @@ func NewManager(addr string, pm peermgr.PeerManager, Mgr appchainmgr.AppchainMgr
if err != nil {
return nil, err
}
err = pm.RegisterMsgHandler(pb.Message_RULE_DEPLOY, rule.HandleRule)
if err != nil {
return nil, err
}
return am, nil
}
......
......@@ -18,6 +18,10 @@ type Router interface {
Load(key string) (value port.Port, ok bool)
Send(id string, msg *pb.Message) (*pb.Message, error)
AsyncSend(id string, msg *pb.Message) error
// InPut(ibtp *pb.IBTP) chan *pb.IBTP
// OutPut(ibtp *pb.IBTP) chan *pb.IBTP
// 代替上面两个方法
......
......@@ -3,8 +3,6 @@ package json
import json "github.com/goccy/go-json"
var (
Marshal = json.Marshal
Marshal = json.Marshal
Unmarshal = json.Unmarshal
)
......@@ -76,3 +76,5 @@ func Exist(path string) bool {
//https://github.com/zricethezav/gitleaks
//https://github.com/casbin/casbin
//https://github.com/casbin/casbin
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