diff --git a/Taskfile.yml b/Taskfile.yml index fed6c04..b510b44 100644 --- a/Taskfile.yml +++ b/Taskfile.yml @@ -53,10 +53,10 @@ tasks: cmds: - task: builder vars: - CLI_ARGS: /bin/sh -c 'cd ./proto/routerclientpb; protoc --proto_path=/go/bin:. --micro_out=paths=source_relative:. --go_out=paths=source_relative:. routerclientpb.proto' + CLI_ARGS: /bin/sh -c 'cd ./internal/proto/routerclientpb; protoc --proto_path=/go/bin:. --micro_out=paths=source_relative:. --go_out=paths=source_relative:. routerclientpb.proto' - task: builder vars: - CLI_ARGS: /bin/sh -c 'cd ./proto/routerserverpb; protoc --proto_path=/go/bin:. --micro_out=paths=source_relative:. --go_out=paths=source_relative:. routerserverpb.proto' + CLI_ARGS: /bin/sh -c 'cd ./internal/proto/routerserverpb; protoc --proto_path=/go/bin:. --micro_out=paths=source_relative:. --go_out=paths=source_relative:. routerserverpb.proto' build:podman: deps: diff --git a/cmd/microrouterd/main.go b/cmd/microrouterd/main.go index e34e2b3..faeeea5 100644 --- a/cmd/microrouterd/main.go +++ b/cmd/microrouterd/main.go @@ -1,8 +1,6 @@ package main import ( - "net/http" - "github.com/urfave/cli/v2" "go-micro.dev/v4" "go-micro.dev/v4/logger" @@ -11,10 +9,9 @@ import ( httpServer "github.com/go-micro/plugins/v4/server/http" "github.com/go-micro/router" - "github.com/go-micro/router/config" - "github.com/go-micro/router/handler" - "github.com/go-micro/router/proto/routerclientpb" - "github.com/go-micro/router/proto/routerserverpb" + "github.com/go-micro/router/internal/config" + "github.com/go-micro/router/internal/handler" + "github.com/go-micro/router/internal/proto/routerserverpb" ) func internalService(engine *gin.Engine) { @@ -35,15 +32,15 @@ func internalService(engine *gin.Engine) { routerserverpb.RegisterRouterServerServiceHandler(srv.Server(), routerHandler) - routerHandler := router.NewHandler( + r := router.NewHandler( config.GetServerConfig().RouterURI, router.NewRoute( - router.RouteMethod(http.MethodGet), - router.RoutePath("/routes"), - router.RouteEndpoint(routerserverpb.RouterServerService.Routes), + router.Method(router.MethodGet), + router.Path("/routes"), + router.Endpoint(routerserverpb.RouterServerService.Routes), ), ) - routerclientpb.RegisterRouterClientServiceHandler(srv.Server(), routerHandler) + r.RegisterWithServer(srv.Server()) return nil }), diff --git a/docker/go-micro-router/Dockerfile b/docker/go-micro-router/Dockerfile index 091cb17..587bfac 100644 --- a/docker/go-micro-router/Dockerfile +++ b/docker/go-micro-router/Dockerfile @@ -13,7 +13,7 @@ ENV GOPATH="/go" ARG CACHEBUST=1 ARG VERSION -RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -installsuffix cgo -ldflags="-w -s -X 'github.com/go-micro/router/config.Version=$VERSION'" -o /usr/local/bin/microrouterd github.com/go-micro/router/cmd/microrouterd +RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -installsuffix cgo -ldflags="-w -s -X 'github.com/go-micro/router/internal/config.Version=$VERSION'" -o /usr/local/bin/microrouterd github.com/go-micro/router/cmd/microrouterd # STEP 2 build a small image # start from busybox diff --git a/handler.go b/handler.go index 27c258e..f841428 100644 --- a/handler.go +++ b/handler.go @@ -3,8 +3,9 @@ package router import ( "context" - "github.com/go-micro/router/proto/routerclientpb" - "github.com/go-micro/router/util" + "github.com/go-micro/router/internal/proto/routerclientpb" + "github.com/go-micro/router/internal/util" + "go-micro.dev/v4/server" "google.golang.org/protobuf/types/known/emptypb" ) @@ -30,6 +31,11 @@ func NewHandler(routerURI string, routes ...Route) *Handler { 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 diff --git a/config/config.go b/internal/config/config.go similarity index 100% rename from config/config.go rename to internal/config/config.go diff --git a/config/load.go b/internal/config/load.go similarity index 97% rename from config/load.go rename to internal/config/load.go index d90b91a..c1edfcd 100644 --- a/config/load.go +++ b/internal/config/load.go @@ -6,7 +6,7 @@ import ( "github.com/go-micro/plugins/v4/config/encoder/toml" "github.com/go-micro/plugins/v4/config/encoder/yaml" - "github.com/go-micro/router/util" + "github.com/go-micro/router/internal/util" "github.com/pkg/errors" "go-micro.dev/v4/config" "go-micro.dev/v4/config/reader" diff --git a/handler/handler.go b/internal/handler/handler.go similarity index 94% rename from handler/handler.go rename to internal/handler/handler.go index 8c4b276..101a9be 100644 --- a/handler/handler.go +++ b/internal/handler/handler.go @@ -5,15 +5,15 @@ import ( "encoding/base64" "encoding/json" "fmt" - "io/ioutil" + "io" "net/http" "time" "github.com/gin-gonic/gin" - "github.com/go-micro/router/config" - "github.com/go-micro/router/proto/routerclientpb" - "github.com/go-micro/router/proto/routerserverpb" - "github.com/go-micro/router/util" + "github.com/go-micro/router/internal/config" + "github.com/go-micro/router/internal/proto/routerclientpb" + "github.com/go-micro/router/internal/proto/routerserverpb" + "github.com/go-micro/router/internal/util" "go-micro.dev/v4" "go-micro.dev/v4/client" "go-micro.dev/v4/errors" @@ -123,7 +123,7 @@ func (h *Handler) proxy(serviceName string, route *routerclientpb.RoutesReply_Ro if err != nil { continue } - data, err := ioutil.ReadAll(fp) + data, err := io.ReadAll(fp) if err != nil { continue } diff --git a/proto/routerclientpb/routerclientpb.pb.go b/internal/proto/routerclientpb/routerclientpb.pb.go similarity index 100% rename from proto/routerclientpb/routerclientpb.pb.go rename to internal/proto/routerclientpb/routerclientpb.pb.go diff --git a/proto/routerclientpb/routerclientpb.pb.micro.go b/internal/proto/routerclientpb/routerclientpb.pb.micro.go similarity index 100% rename from proto/routerclientpb/routerclientpb.pb.micro.go rename to internal/proto/routerclientpb/routerclientpb.pb.micro.go diff --git a/proto/routerclientpb/routerclientpb.proto b/internal/proto/routerclientpb/routerclientpb.proto similarity index 100% rename from proto/routerclientpb/routerclientpb.proto rename to internal/proto/routerclientpb/routerclientpb.proto diff --git a/proto/routerserverpb/routerserverpb.pb.go b/internal/proto/routerserverpb/routerserverpb.pb.go similarity index 100% rename from proto/routerserverpb/routerserverpb.pb.go rename to internal/proto/routerserverpb/routerserverpb.pb.go diff --git a/proto/routerserverpb/routerserverpb.pb.micro.go b/internal/proto/routerserverpb/routerserverpb.pb.micro.go similarity index 100% rename from proto/routerserverpb/routerserverpb.pb.micro.go rename to internal/proto/routerserverpb/routerserverpb.pb.micro.go diff --git a/proto/routerserverpb/routerserverpb.proto b/internal/proto/routerserverpb/routerserverpb.proto similarity index 100% rename from proto/routerserverpb/routerserverpb.proto rename to internal/proto/routerserverpb/routerserverpb.proto diff --git a/util/ctx.go b/internal/util/ctx.go similarity index 100% rename from util/ctx.go rename to internal/util/ctx.go diff --git a/util/goroutine.go b/internal/util/goroutine.go similarity index 100% rename from util/goroutine.go rename to internal/util/goroutine.go diff --git a/util/serviceregistry.go b/internal/util/serviceregistry.go similarity index 100% rename from util/serviceregistry.go rename to internal/util/serviceregistry.go diff --git a/util/util.go b/internal/util/util.go similarity index 100% rename from util/util.go rename to internal/util/util.go diff --git a/method.go b/method.go new file mode 100644 index 0000000..f5170d0 --- /dev/null +++ b/method.go @@ -0,0 +1,17 @@ +// Copyright 2015 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package router + +const ( + MethodGet = "GET" + MethodHead = "HEAD" + MethodPost = "POST" + MethodPut = "PUT" + MethodPatch = "PATCH" // RFC 5789 + MethodDelete = "DELETE" + MethodConnect = "CONNECT" + MethodOptions = "OPTIONS" + MethodTrace = "TRACE" +) diff --git a/route.go b/route.go index a48a1c5..aa65f37 100644 --- a/route.go +++ b/route.go @@ -11,9 +11,9 @@ type Route struct { Params []string } -type RouteOption func(*Route) +type Option func(*Route) -func NewRoute(endpoint interface{}, opts ...RouteOption) Route { +func NewRoute(endpoint interface{}, opts ...Option) Route { route := Route{ IsGlobal: false, Method: http.MethodGet, @@ -29,31 +29,31 @@ func NewRoute(endpoint interface{}, opts ...RouteOption) Route { return route } -func RouteIsGlobal(n bool) RouteOption { +func IsGlobal(n bool) Option { return func(o *Route) { o.IsGlobal = n } } -func RouteMethod(n string) RouteOption { +func Method(n string) Option { return func(o *Route) { o.Method = n } } -func RoutePath(n string) RouteOption { +func Path(n string) Option { return func(o *Route) { o.Path = n } } -func RouteEndpoint(n interface{}) RouteOption { +func Endpoint(n interface{}) Option { return func(o *Route) { o.Endpoint = n } } -func RouteParams(n []string) RouteOption { +func Params(n ...string) Option { return func(o *Route) { o.Params = n }