parent
69dd203d2a
commit
db4a15594d
@ -1,39 +0,0 @@
|
||||
package client
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/go-micro/router/proto/routerclientpb"
|
||||
"github.com/go-micro/router/util"
|
||||
"google.golang.org/protobuf/types/known/emptypb"
|
||||
)
|
||||
|
||||
// Handler is the handler for github.com/go-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 {
|
||||
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}
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
@ -1,60 +0,0 @@
|
||||
package client
|
||||
|
||||
import "net/http"
|
||||
|
||||
type Route struct {
|
||||
// isGlobal=True == no prefix route
|
||||
IsGlobal bool
|
||||
Method string
|
||||
Path string
|
||||
Endpoint interface{}
|
||||
Params []string
|
||||
}
|
||||
|
||||
type RouteOption func(*Route)
|
||||
|
||||
func NewRoute(endpoint interface{}, opts ...RouteOption) Route {
|
||||
route := Route{
|
||||
IsGlobal: false,
|
||||
Method: http.MethodGet,
|
||||
Path: "/",
|
||||
Endpoint: endpoint,
|
||||
Params: []string{},
|
||||
}
|
||||
|
||||
for _, o := range opts {
|
||||
o(&route)
|
||||
}
|
||||
|
||||
return route
|
||||
}
|
||||
|
||||
func RouteIsGlobal(n bool) RouteOption {
|
||||
return func(o *Route) {
|
||||
o.IsGlobal = n
|
||||
}
|
||||
}
|
||||
|
||||
func RouteMethod(n string) RouteOption {
|
||||
return func(o *Route) {
|
||||
o.Method = n
|
||||
}
|
||||
}
|
||||
|
||||
func RoutePath(n string) RouteOption {
|
||||
return func(o *Route) {
|
||||
o.Path = n
|
||||
}
|
||||
}
|
||||
|
||||
func RouteEndpoint(n interface{}) RouteOption {
|
||||
return func(o *Route) {
|
||||
o.Endpoint = n
|
||||
}
|
||||
}
|
||||
|
||||
func RouteParams(n []string) RouteOption {
|
||||
return func(o *Route) {
|
||||
o.Params = n
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue