v3 refactor (#1868)

* Move to v3

Co-authored-by: Ben Toogood <bentoogood@gmail.com>
This commit is contained in:
Asim Aslam
2020-07-27 13:22:00 +01:00
committed by GitHub
parent 9dfeb98111
commit 563768b58a
424 changed files with 6383 additions and 22490 deletions

View File

@@ -5,10 +5,11 @@ import (
"context"
"sync"
"github.com/micro/go-micro/v2/client"
"github.com/micro/go-micro/v2/debug/service/handler"
"github.com/micro/go-micro/v2/proxy"
"github.com/micro/go-micro/v2/server"
"github.com/micro/go-micro/v3/client/grpc"
"github.com/micro/go-micro/v3/debug/service/handler"
"github.com/micro/go-micro/v3/proxy"
"github.com/micro/go-micro/v3/server"
"github.com/micro/go-micro/v3/server/mucp"
)
// Server is a proxy muxer that incudes the use of the DefaultHandler
@@ -17,6 +18,14 @@ type Server struct {
Name string
// Proxy handler
Proxy proxy.Proxy
// The default handler
Handler Handler
}
type Handler interface {
proxy.Proxy
NewHandler(interface{}, ...server.HandlerOption) server.Handler
Handle(server.Handler) error
}
var (
@@ -25,32 +34,35 @@ var (
func (s *Server) ProcessMessage(ctx context.Context, msg server.Message) error {
if msg.Topic() == s.Name {
return server.DefaultRouter.ProcessMessage(ctx, msg)
return s.Handler.ProcessMessage(ctx, msg)
}
return s.Proxy.ProcessMessage(ctx, msg)
}
func (s *Server) ServeRequest(ctx context.Context, req server.Request, rsp server.Response) error {
if req.Service() == s.Name {
return server.DefaultRouter.ServeRequest(ctx, req, rsp)
return s.Handler.ServeRequest(ctx, req, rsp)
}
return s.Proxy.ServeRequest(ctx, req, rsp)
}
func New(name string, p proxy.Proxy) *Server {
r := mucp.DefaultRouter
// only register this once
once.Do(func() {
server.DefaultRouter.Handle(
r.Handle(
// inject the debug handler
server.DefaultRouter.NewHandler(
handler.NewHandler(client.DefaultClient),
r.NewHandler(
handler.NewHandler(grpc.NewClient()),
server.InternalHandler(true),
),
)
})
return &Server{
Name: name,
Proxy: p,
Name: name,
Proxy: p,
Handler: r,
}
}