2019-08-13 17:21:51 +03:00
|
|
|
// Package mux provides proxy muxing
|
|
|
|
package mux
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"sync"
|
|
|
|
|
2019-12-02 00:04:09 +03:00
|
|
|
proto "github.com/micro/go-micro/debug/proto"
|
2019-08-13 17:21:51 +03:00
|
|
|
"github.com/micro/go-micro/debug/handler"
|
|
|
|
"github.com/micro/go-micro/proxy"
|
|
|
|
"github.com/micro/go-micro/server"
|
|
|
|
)
|
|
|
|
|
2019-08-15 10:47:32 +03:00
|
|
|
// Server is a proxy muxer that incudes the use of the DefaultHandler
|
2019-08-13 17:21:51 +03:00
|
|
|
type Server struct {
|
|
|
|
// name of service
|
|
|
|
Name string
|
|
|
|
// Proxy handler
|
|
|
|
Proxy proxy.Proxy
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
once sync.Once
|
|
|
|
)
|
|
|
|
|
2019-11-25 19:31:43 +03:00
|
|
|
func (s *Server) ProcessMessage(ctx context.Context, msg server.Message) error {
|
|
|
|
if msg.Topic() == s.Name {
|
|
|
|
return server.DefaultRouter.ProcessMessage(ctx, msg)
|
|
|
|
}
|
|
|
|
return s.Proxy.ProcessMessage(ctx, msg)
|
|
|
|
}
|
|
|
|
|
2019-08-13 17:21:51 +03:00
|
|
|
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.Proxy.ServeRequest(ctx, req, rsp)
|
|
|
|
}
|
|
|
|
|
|
|
|
func New(name string, p proxy.Proxy) *Server {
|
|
|
|
// only register this once
|
|
|
|
once.Do(func() {
|
2019-12-02 00:04:09 +03:00
|
|
|
proto.RegisterDebugHandler(
|
|
|
|
server.DefaultServer,
|
|
|
|
handler.DefaultHandler,
|
|
|
|
server.InternalHandler(true),
|
2019-08-13 17:21:51 +03:00
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
return &Server{
|
2019-08-15 10:47:32 +03:00
|
|
|
Name: name,
|
2019-08-13 17:21:51 +03:00
|
|
|
Proxy: p,
|
|
|
|
}
|
|
|
|
}
|