Make none client code internal, redesign the api.

master v0.1.0
René Jochum 2 years ago
parent 61987fdc7a
commit 847b48f425

@ -53,10 +53,10 @@ tasks:
cmds: cmds:
- task: builder - task: builder
vars: 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 - task: builder
vars: 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: build:podman:
deps: deps:

@ -1,8 +1,6 @@
package main package main
import ( import (
"net/http"
"github.com/urfave/cli/v2" "github.com/urfave/cli/v2"
"go-micro.dev/v4" "go-micro.dev/v4"
"go-micro.dev/v4/logger" "go-micro.dev/v4/logger"
@ -11,10 +9,9 @@ import (
httpServer "github.com/go-micro/plugins/v4/server/http" httpServer "github.com/go-micro/plugins/v4/server/http"
"github.com/go-micro/router" "github.com/go-micro/router"
"github.com/go-micro/router/config" "github.com/go-micro/router/internal/config"
"github.com/go-micro/router/handler" "github.com/go-micro/router/internal/handler"
"github.com/go-micro/router/proto/routerclientpb" "github.com/go-micro/router/internal/proto/routerserverpb"
"github.com/go-micro/router/proto/routerserverpb"
) )
func internalService(engine *gin.Engine) { func internalService(engine *gin.Engine) {
@ -35,15 +32,15 @@ func internalService(engine *gin.Engine) {
routerserverpb.RegisterRouterServerServiceHandler(srv.Server(), routerHandler) routerserverpb.RegisterRouterServerServiceHandler(srv.Server(), routerHandler)
routerHandler := router.NewHandler( r := router.NewHandler(
config.GetServerConfig().RouterURI, config.GetServerConfig().RouterURI,
router.NewRoute( router.NewRoute(
router.RouteMethod(http.MethodGet), router.Method(router.MethodGet),
router.RoutePath("/routes"), router.Path("/routes"),
router.RouteEndpoint(routerserverpb.RouterServerService.Routes), router.Endpoint(routerserverpb.RouterServerService.Routes),
), ),
) )
routerclientpb.RegisterRouterClientServiceHandler(srv.Server(), routerHandler) r.RegisterWithServer(srv.Server())
return nil return nil
}), }),

@ -13,7 +13,7 @@ ENV GOPATH="/go"
ARG CACHEBUST=1 ARG CACHEBUST=1
ARG VERSION 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 # STEP 2 build a small image
# start from busybox # start from busybox

@ -3,8 +3,9 @@ package router
import ( import (
"context" "context"
"github.com/go-micro/router/proto/routerclientpb" "github.com/go-micro/router/internal/proto/routerclientpb"
"github.com/go-micro/router/util" "github.com/go-micro/router/internal/util"
"go-micro.dev/v4/server"
"google.golang.org/protobuf/types/known/emptypb" "google.golang.org/protobuf/types/known/emptypb"
) )
@ -30,6 +31,11 @@ func NewHandler(routerURI string, routes ...Route) *Handler {
return &Handler{routerURI, pbRoutes} 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 // Routes returns the registered routes
func (h *Handler) Routes(ctx context.Context, req *emptypb.Empty, rsp *routerclientpb.RoutesReply) error { func (h *Handler) Routes(ctx context.Context, req *emptypb.Empty, rsp *routerclientpb.RoutesReply) error {
rsp.RouterURI = h.routerURI rsp.RouterURI = h.routerURI

@ -6,7 +6,7 @@ import (
"github.com/go-micro/plugins/v4/config/encoder/toml" "github.com/go-micro/plugins/v4/config/encoder/toml"
"github.com/go-micro/plugins/v4/config/encoder/yaml" "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" "github.com/pkg/errors"
"go-micro.dev/v4/config" "go-micro.dev/v4/config"
"go-micro.dev/v4/config/reader" "go-micro.dev/v4/config/reader"

@ -5,15 +5,15 @@ import (
"encoding/base64" "encoding/base64"
"encoding/json" "encoding/json"
"fmt" "fmt"
"io/ioutil" "io"
"net/http" "net/http"
"time" "time"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/go-micro/router/config" "github.com/go-micro/router/internal/config"
"github.com/go-micro/router/proto/routerclientpb" "github.com/go-micro/router/internal/proto/routerclientpb"
"github.com/go-micro/router/proto/routerserverpb" "github.com/go-micro/router/internal/proto/routerserverpb"
"github.com/go-micro/router/util" "github.com/go-micro/router/internal/util"
"go-micro.dev/v4" "go-micro.dev/v4"
"go-micro.dev/v4/client" "go-micro.dev/v4/client"
"go-micro.dev/v4/errors" "go-micro.dev/v4/errors"
@ -123,7 +123,7 @@ func (h *Handler) proxy(serviceName string, route *routerclientpb.RoutesReply_Ro
if err != nil { if err != nil {
continue continue
} }
data, err := ioutil.ReadAll(fp) data, err := io.ReadAll(fp)
if err != nil { if err != nil {
continue continue
} }

@ -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"
)

@ -11,9 +11,9 @@ type Route struct {
Params []string 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{ route := Route{
IsGlobal: false, IsGlobal: false,
Method: http.MethodGet, Method: http.MethodGet,
@ -29,31 +29,31 @@ func NewRoute(endpoint interface{}, opts ...RouteOption) Route {
return route return route
} }
func RouteIsGlobal(n bool) RouteOption { func IsGlobal(n bool) Option {
return func(o *Route) { return func(o *Route) {
o.IsGlobal = n o.IsGlobal = n
} }
} }
func RouteMethod(n string) RouteOption { func Method(n string) Option {
return func(o *Route) { return func(o *Route) {
o.Method = n o.Method = n
} }
} }
func RoutePath(n string) RouteOption { func Path(n string) Option {
return func(o *Route) { return func(o *Route) {
o.Path = n o.Path = n
} }
} }
func RouteEndpoint(n interface{}) RouteOption { func Endpoint(n interface{}) Option {
return func(o *Route) { return func(o *Route) {
o.Endpoint = n o.Endpoint = n
} }
} }
func RouteParams(n []string) RouteOption { func Params(n ...string) Option {
return func(o *Route) { return func(o *Route) {
o.Params = n o.Params = n
} }

Loading…
Cancel
Save