You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
router/handler.go

51 lines
1.3 KiB
Go

package router
import (
"context"
"go-micro.dev/v4/server"
"google.golang.org/protobuf/types/known/emptypb"
"jochum.dev/jo-micro/router/internal/proto/routerclientpb"
"jochum.dev/jo-micro/router/internal/util"
)
// Handler is the handler for jochum.dev/jo-micro/router/proto/routerpb.RrouterService
type Handler struct {
routerURI string
routes []*routerclientpb.RoutesReply_Route
}
// NewHandler returns a new dynrouterpb Handler
func NewHandler(routerURI string, routes ...*Route) *Handler {
pbRoutes := []*routerclientpb.RoutesReply_Route{}
for _, r := range routes {
// NewRoute returns nil if no Endpoint has been specified, ignore these here
if r == nil {
continue
}
pbRoutes = append(pbRoutes, &routerclientpb.RoutesReply_Route{
IsGlobal: r.IsGlobal,
Method: r.Method,
Path: r.Path,
Endpoint: util.ReflectFunctionName(r.Endpoint),
Params: r.Params,
})
}
return &Handler{routerURI, pbRoutes}
}
// RegisterWithServer registers this Handler with a server
func (h *Handler) RegisterWithServer(s server.Server) {
routerclientpb.RegisterRouterClientServiceHandler(s, h)
}
// Routes returns the registered routes
func (h *Handler) Routes(ctx context.Context, req *emptypb.Empty, rsp *routerclientpb.RoutesReply) error {
rsp.RouterURI = h.routerURI
rsp.Routes = h.routes
return nil
}