From 8ee31b94a16a21e10e105e0e51c5736316452ba7 Mon Sep 17 00:00:00 2001 From: Asim Aslam Date: Fri, 7 Aug 2020 12:47:20 +0100 Subject: [PATCH] remove handler/util package in favour of util/router (#1908) --- api/handler/api/api.go | 4 +-- api/handler/rpc/rpc.go | 4 +-- api/handler/rpc/stream.go | 4 +-- {api/handler/util => util/router}/router.go | 30 ++++++++++----------- 4 files changed, 21 insertions(+), 21 deletions(-) rename {api/handler/util => util/router}/router.go (89%) diff --git a/api/handler/api/api.go b/api/handler/api/api.go index 7d5a569d..06b45f31 100644 --- a/api/handler/api/api.go +++ b/api/handler/api/api.go @@ -6,11 +6,11 @@ import ( goapi "github.com/micro/go-micro/v3/api" "github.com/micro/go-micro/v3/api/handler" - "github.com/micro/go-micro/v3/api/handler/util" api "github.com/micro/go-micro/v3/api/proto" "github.com/micro/go-micro/v3/client" "github.com/micro/go-micro/v3/errors" "github.com/micro/go-micro/v3/util/ctx" + "github.com/micro/go-micro/v3/util/router" ) type apiHandler struct { @@ -72,7 +72,7 @@ func (a *apiHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { // create the context from headers cx := ctx.FromRequest(r) - if err := c.Call(cx, req, rsp, client.WithRouter(util.Router(service.Services))); err != nil { + if err := c.Call(cx, req, rsp, client.WithRouter(router.New(service.Services))); err != nil { w.Header().Set("Content-Type", "application/json") ce := errors.Parse(err.Error()) switch ce.Code { diff --git a/api/handler/rpc/rpc.go b/api/handler/rpc/rpc.go index f51e7a65..5a22ed54 100644 --- a/api/handler/rpc/rpc.go +++ b/api/handler/rpc/rpc.go @@ -11,7 +11,6 @@ import ( jsonpatch "github.com/evanphx/json-patch/v5" "github.com/micro/go-micro/v3/api" "github.com/micro/go-micro/v3/api/handler" - "github.com/micro/go-micro/v3/api/handler/util" "github.com/micro/go-micro/v3/api/internal/proto" "github.com/micro/go-micro/v3/client" "github.com/micro/go-micro/v3/codec" @@ -22,6 +21,7 @@ import ( "github.com/micro/go-micro/v3/metadata" "github.com/micro/go-micro/v3/util/ctx" "github.com/micro/go-micro/v3/util/qson" + "github.com/micro/go-micro/v3/util/router" "github.com/oxtoacart/bpool" ) @@ -113,7 +113,7 @@ func (h *rpcHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { } // create custom router - callOpt := client.WithRouter(util.Router(service.Services)) + callOpt := client.WithRouter(router.New(service.Services)) // walk the standard call path // get payload diff --git a/api/handler/rpc/stream.go b/api/handler/rpc/stream.go index 1078cdc1..25d6789f 100644 --- a/api/handler/rpc/stream.go +++ b/api/handler/rpc/stream.go @@ -13,10 +13,10 @@ import ( "github.com/gobwas/ws" "github.com/gobwas/ws/wsutil" "github.com/micro/go-micro/v3/api" - "github.com/micro/go-micro/v3/api/handler/util" "github.com/micro/go-micro/v3/client" raw "github.com/micro/go-micro/v3/codec/bytes" "github.com/micro/go-micro/v3/logger" + "github.com/micro/go-micro/v3/util/router" ) // serveWebsocket will stream rpc back over websockets assuming json @@ -111,7 +111,7 @@ func serveWebsocket(ctx context.Context, w http.ResponseWriter, r *http.Request, ) // create custom router - callOpt := client.WithRouter(util.Router(service.Services)) + callOpt := client.WithRouter(router.New(service.Services)) // create a new stream stream, err := c.Stream(ctx, req, callOpt) diff --git a/api/handler/util/router.go b/util/router/router.go similarity index 89% rename from api/handler/util/router.go rename to util/router/router.go index a079e6d4..b9b5a517 100644 --- a/api/handler/util/router.go +++ b/util/router/router.go @@ -1,12 +1,25 @@ -package util +package router import ( "github.com/micro/go-micro/v3/registry" "github.com/micro/go-micro/v3/router" ) +type apiRouter struct { + routes []router.Route + router.Router +} + +func (r *apiRouter) Lookup(...router.QueryOption) ([]router.Route, error) { + return r.routes, nil +} + +func (r *apiRouter) String() string { + return "api" +} + // Router is a hack for API routing -func Router(srvs []*registry.Service) router.Router { +func New(srvs []*registry.Service) router.Router { var routes []router.Route for _, srv := range srvs { @@ -17,16 +30,3 @@ func Router(srvs []*registry.Service) router.Router { return &apiRouter{routes: routes} } - -func (r *apiRouter) Lookup(...router.QueryOption) ([]router.Route, error) { - return r.routes, nil -} - -type apiRouter struct { - routes []router.Route - router.Router -} - -func (r *apiRouter) String() string { - return "api" -}