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