mirror of
				https://github.com/go-micro/go-micro.git
				synced 2025-10-30 23:27:41 +02:00 
			
		
		
		
	add a mux package for the proxy
This commit is contained in:
		
							
								
								
									
										47
									
								
								util/mux/mux.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										47
									
								
								util/mux/mux.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,47 @@ | ||||
| // Package mux provides proxy muxing | ||||
| package mux | ||||
|  | ||||
| import ( | ||||
| 	"context" | ||||
| 	"sync" | ||||
|  | ||||
| 	"github.com/micro/go-micro/debug/handler" | ||||
| 	"github.com/micro/go-micro/proxy" | ||||
| 	"github.com/micro/go-micro/server" | ||||
| ) | ||||
|  | ||||
| // Server is a proxy muxer that incudes the use of the DefaultHandler  | ||||
| type Server struct { | ||||
| 	// name of service | ||||
| 	Name string | ||||
| 	// Proxy handler | ||||
| 	Proxy proxy.Proxy | ||||
| } | ||||
|  | ||||
| var ( | ||||
| 	once sync.Once | ||||
| ) | ||||
|  | ||||
| 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() { | ||||
| 		server.DefaultRouter.Handle( | ||||
| 			server.DefaultRouter.NewHandler( | ||||
| 				handler.DefaultHandler, | ||||
| 				server.InternalHandler(true), | ||||
| 			), | ||||
| 		) | ||||
| 	}) | ||||
|  | ||||
| 	return &Server{ | ||||
| 		Name: name, | ||||
| 		Proxy: p, | ||||
| 	} | ||||
| } | ||||
		Reference in New Issue
	
	Block a user