Add auth support

master
René Jochum 2 years ago
parent 13e8e625af
commit 5edfd81831
Signed by: jochum
GPG Key ID: F7D906F5E51E8E5E

5
.gitignore vendored

@ -2,4 +2,7 @@
.task/
!.gitkeep
!.gitkeep
go.work
go.work.sum

@ -1,6 +0,0 @@
SPDXVersion: SPDX-2.1
DataLicense: CC0-1.0
Creator: René Jochum (rene@jochum.dev)
PackageName: jochum.dev/jo-micro/router
PackageOriginator: René Jochum
PackageLicenseDeclared: Apache-2.0 OR GPL-2.0-or-later

@ -1,4 +1,4 @@
[![Build Status](https://drone.fk.jochum.dev/api/badges/jo-micro/router/status.svg)](https://drone.fk.jochum.dev/jo-micro/router)
[![Build Status](https://drone.fk.jochum.dev/api/badges/jo-micro/router/status.svg)](https://drone.fk.jochum.dev/jo-micro/router) [![Go Reference](https://pkg.go.dev/badge/jochum.dev/jo-micro/router.svg)](https://pkg.go.dev/jochum.dev/jo-micro/router)
# router
@ -69,6 +69,7 @@ func main() {
router.Path("/"),
router.Endpoint(authservicepb.AuthV1Service.UserList),
router.Params("limit", "offset"),
router.AuthRequired(),
),
router.NewRoute(
router.Method(router.MethodPost),
@ -90,18 +91,21 @@ func main() {
router.Path("/:userId"),
router.Endpoint(authservicepb.AuthV1Service.UserDelete),
router.Params("userId"),
router.AuthRequired(),
),
router.NewRoute(
router.Method(router.MethodGet),
router.Path("/:userId"),
router.Endpoint(authservicepb.AuthV1Service.UserDetail),
router.Params("userId"),
router.AuthRequired(),
),
router.NewRoute(
router.Method(router.MethodPut),
router.Path("/:userId/roles"),
router.Endpoint(authservicepb.AuthV1Service.UserUpdateRoles),
router.Params("userId"),
router.AuthRequired(),
),
)
r.RegisterWithServer(s)

@ -43,7 +43,7 @@ tasks:
run: "once"
desc: Generate protobruf go files
sources:
- ./proto/**/*.proto
- ./internal/proto/**/*.proto
cmds:
- task: builder
vars:

@ -14,6 +14,7 @@ import (
"go-micro.dev/v4/client"
"go-micro.dev/v4/errors"
"google.golang.org/protobuf/types/known/emptypb"
"jochum.dev/jo-micro/auth"
"jochum.dev/jo-micro/router/cmd/microrouterd/config"
iLogger "jochum.dev/jo-micro/router/internal/logger"
"jochum.dev/jo-micro/router/internal/proto/routerclientpb"
@ -28,20 +29,22 @@ type JSONRoute struct {
// Handler is the handler for the proxy
type Handler struct {
service micro.Service
engine *gin.Engine
routes map[string]*routerclientpb.RoutesReply_Route
service micro.Service
engine *gin.Engine
routerAuth auth.RouterPlugin
routes map[string]*routerclientpb.RoutesReply_Route
}
func NewHandler(service micro.Service, engine *gin.Engine) (*Handler, error) {
func NewHandler() (*Handler, error) {
return &Handler{
service: service,
engine: engine,
routes: make(map[string]*routerclientpb.RoutesReply_Route),
routes: make(map[string]*routerclientpb.RoutesReply_Route),
}, nil
}
func (h *Handler) Start() error {
func (h *Handler) Init(service micro.Service, engine *gin.Engine, routerAuth auth.RouterPlugin) error {
h.service = service
h.engine = engine
h.routerAuth = routerAuth
globalGroup := h.engine.Group("")
// Refresh routes for the proxy every 10 seconds
@ -87,7 +90,7 @@ func (h *Handler) Start() error {
WithField("path", path).
Debugf("Found route")
g.Handle(route.GetMethod(), route.GetPath(), h.proxy(s.Name, route))
g.Handle(route.GetMethod(), route.GetPath(), h.proxy(s.Name, route, route.AuthRequired))
h.routes[pathMethod] = route
h.routes[pathMethod].Path = path
}
@ -105,7 +108,7 @@ func (h *Handler) Stop() error {
return nil
}
func (h *Handler) proxy(serviceName string, route *routerclientpb.RoutesReply_Route) func(*gin.Context) {
func (h *Handler) proxy(serviceName string, route *routerclientpb.RoutesReply_Route, authRequired bool) func(*gin.Context) {
return func(c *gin.Context) {
// Map query/path params
params := make(map[string]string)
@ -175,11 +178,19 @@ func (h *Handler) proxy(serviceName string, route *routerclientpb.RoutesReply_Ro
req := h.service.Client().NewRequest(serviceName, route.GetEndpoint(), request, client.WithContentType("application/json"))
ctx := util.CtxFromRequest(c, c.Request)
// Auth
ctx, err := h.routerAuth.ForwardContext(c.Request, c)
if err != nil || authRequired {
c.JSON(http.StatusUnauthorized, gin.H{
"status": http.StatusUnauthorized,
"message": err,
})
return
}
// remote call
var response json.RawMessage
err := h.service.Client().Call(ctx, req, &response)
err = h.service.Client().Call(ctx, req, &response)
if err != nil {
iLogger.Logrus().Error(err)

@ -1,3 +1,5 @@
// -lang=go1.19
package main
import (
@ -10,6 +12,7 @@ import (
"github.com/gin-gonic/gin"
httpServer "github.com/go-micro/plugins/v4/server/http"
"jochum.dev/jo-micro/auth"
"jochum.dev/jo-micro/router"
"jochum.dev/jo-micro/router/cmd/microrouterd/config"
@ -19,22 +22,13 @@ import (
"jochum.dev/jo-micro/router/internal/proto/routerserverpb"
)
func internalService(engine *gin.Engine) {
func internalService(routerHandler *handler.Handler) {
srv := micro.NewService()
routerHandler, err := handler.NewHandler(srv, engine)
if err != nil {
logger.Fatal(err)
}
opts := []micro.Option{
micro.Name(config.Name + "-internal"),
micro.Version(config.Version),
micro.Action(func(c *cli.Context) error {
if err := routerHandler.Start(); err != nil {
logger.Fatal(err)
}
routerserverpb.RegisterRouterServerServiceHandler(srv.Server(), routerHandler)
r := router.NewHandler(
@ -61,7 +55,6 @@ func internalService(engine *gin.Engine) {
if err := routerHandler.Stop(); err != nil {
logger.Fatal(err)
}
}
func main() {
@ -78,11 +71,22 @@ func main() {
}
r := gin.New()
routerHandler, err := handler.NewHandler()
if err != nil {
logger.Fatal(err)
}
authReg := auth.RouterAuthRegistry()
flags := []cli.Flag{}
flags = append(flags, iLogger.Flags()...)
flags = append(flags, authReg.Flags()...)
opts := []micro.Option{
micro.Name(config.Name),
micro.Version(config.Version),
micro.Address(config.GetRouterConfig().Address),
micro.Flags(iLogger.Flags()...),
micro.Flags(flags...),
micro.Action(func(c *cli.Context) error {
// Start the logger
if err := iLogger.Start(c); err != nil {
@ -90,8 +94,21 @@ func main() {
return err
}
// Initialize the Auth Plugin over RouterAuthRegistry
if err := authReg.Init(c, srv); err != nil {
logger.Fatal(err)
return err
}
// Initalize the Handler
if err := routerHandler.Init(srv, r, authReg.MustPlugin()); err != nil {
logger.Fatal(err)
}
// Add middlewares to gin
r.Use(ginlogrus.Logger(iLogger.Logrus()), gin.Recovery())
// Register gin with micro
if err := micro.RegisterHandler(srv.Server(), r); err != nil {
logger.Fatal(err)
}
@ -101,13 +118,18 @@ func main() {
}
srv.Init(opts...)
go internalService(r)
go internalService(routerHandler)
// Run server
if err := srv.Run(); err != nil {
logger.Fatal(err)
}
// Stop the plugin in RouterAuthRegistry
if err := authReg.Stop(); err != nil {
logger.Fatal(err)
}
// Stop the logger
if err := iLogger.Stop(); err != nil {
logger.Fatal(err)

@ -1,25 +1,10 @@
package main
import (
_ "github.com/go-micro/plugins/v4/broker/kafka"
_ "github.com/go-micro/plugins/v4/broker/mqtt"
_ "github.com/go-micro/plugins/v4/broker/nats"
_ "github.com/go-micro/plugins/v4/broker/rabbitmq"
_ "github.com/go-micro/plugins/v4/broker/redis"
_ "github.com/go-micro/plugins/v4/registry/consul"
_ "github.com/go-micro/plugins/v4/registry/etcd"
_ "github.com/go-micro/plugins/v4/registry/eureka"
_ "github.com/go-micro/plugins/v4/registry/gossip"
_ "github.com/go-micro/plugins/v4/registry/kubernetes"
_ "github.com/go-micro/plugins/v4/registry/nacos"
_ "github.com/go-micro/plugins/v4/registry/nats"
_ "github.com/go-micro/plugins/v4/registry/zookeeper"
_ "github.com/go-micro/plugins/v4/transport/grpc"
_ "github.com/go-micro/plugins/v4/transport/http"
_ "github.com/go-micro/plugins/v4/transport/nats"
_ "github.com/go-micro/plugins/v4/transport/rabbitmq"
_ "github.com/go-micro/plugins/v4/transport/tcp"
_ "github.com/go-micro/plugins/v4/transport/utp"
_ "jochum.dev/jo-micro/auth/plugins/router/jwt"
)

112
go.mod

@ -4,70 +4,35 @@ go 1.19
require (
github.com/gin-gonic/gin v1.8.1
github.com/go-micro/plugins/v4/broker/kafka v1.1.0
github.com/go-micro/plugins/v4/broker/mqtt v1.1.0
github.com/go-micro/plugins/v4/broker/nats v1.1.1-0.20220908125827-e0369dde429b
github.com/go-micro/plugins/v4/broker/rabbitmq v1.1.0
github.com/go-micro/plugins/v4/broker/redis v1.1.0
github.com/go-micro/plugins/v4/config/encoder/toml v1.1.0
github.com/go-micro/plugins/v4/config/encoder/yaml v1.1.0
github.com/go-micro/plugins/v4/logger/logrus v1.1.0
github.com/go-micro/plugins/v4/registry/consul v1.1.0
github.com/go-micro/plugins/v4/registry/etcd v1.1.0
github.com/go-micro/plugins/v4/registry/eureka v1.1.0
github.com/go-micro/plugins/v4/registry/gossip v1.1.0
github.com/go-micro/plugins/v4/registry/kubernetes v1.1.0
github.com/go-micro/plugins/v4/registry/nacos v1.1.0
github.com/go-micro/plugins/v4/registry/nats v1.1.1-0.20220908125827-e0369dde429b
github.com/go-micro/plugins/v4/registry/zookeeper v1.1.0
github.com/go-micro/plugins/v4/server/http v1.1.0
github.com/go-micro/plugins/v4/transport/grpc v1.1.0
github.com/go-micro/plugins/v4/transport/http v1.1.0
github.com/go-micro/plugins/v4/transport/nats v1.1.1-0.20220908125827-e0369dde429b
github.com/go-micro/plugins/v4/transport/rabbitmq v1.1.0
github.com/go-micro/plugins/v4/transport/tcp v1.1.0
github.com/go-micro/plugins/v4/transport/utp v1.1.0
github.com/pkg/errors v0.9.1
github.com/sirupsen/logrus v1.9.0
github.com/toorop/gin-logrus v0.0.0-20210225092905-2c785434f26f
github.com/urfave/cli/v2 v2.16.0
github.com/urfave/cli/v2 v2.16.3
go-micro.dev/v4 v4.8.1
google.golang.org/protobuf v1.28.1
jochum.dev/jo-micro/auth v0.0.0-20220919054400-33d0da8c4566
)
require (
github.com/BurntSushi/toml v1.2.0 // indirect
github.com/Microsoft/go-winio v0.5.2 // indirect
github.com/ProtonMail/go-crypto v0.0.0-20220824120805-4b6e5c587895 // indirect
github.com/Shopify/sarama v1.36.0 // indirect
github.com/acomagu/bufpipe v1.0.3 // indirect
github.com/aliyun/alibaba-cloud-sdk-go v1.61.1768 // indirect
github.com/anacrolix/missinggo v1.3.0 // indirect
github.com/anacrolix/missinggo/perf v1.0.0 // indirect
github.com/anacrolix/missinggo/v2 v2.7.0 // indirect
github.com/anacrolix/sync v0.4.0 // indirect
github.com/anacrolix/utp v0.1.0 // indirect
github.com/armon/go-metrics v0.4.1 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/avast/retry-go v3.0.0+incompatible // indirect
github.com/bitly/go-simplejson v0.5.0 // indirect
github.com/buger/jsonparser v1.1.1 // indirect
github.com/cenkalti/backoff/v4 v4.1.3 // indirect
github.com/cespare/xxhash/v2 v2.1.2 // indirect
github.com/clbanning/mxj v1.8.4 // indirect
github.com/cloudflare/circl v1.2.0 // indirect
github.com/coreos/go-semver v0.3.0 // indirect
github.com/coreos/go-systemd/v22 v22.3.2 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/eapache/go-resiliency v1.3.0 // indirect
github.com/eapache/go-xerial-snappy v0.0.0-20180814174437-776d5712da21 // indirect
github.com/eapache/queue v1.1.0 // indirect
github.com/eclipse/paho.mqtt.golang v1.4.1 // indirect
github.com/emirpasic/gods v1.18.1 // indirect
github.com/evanphx/json-patch/v5 v5.6.0 // indirect
github.com/fatih/color v1.13.0 // indirect
github.com/felixge/httpsnoop v1.0.3 // indirect
github.com/franela/goreq v0.0.0-20171204163338-bcd34c9993f8 // indirect
github.com/fsnotify/fsnotify v1.5.4 // indirect
github.com/ghodss/yaml v1.0.0 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
@ -77,105 +42,48 @@ require (
github.com/go-git/go-git/v5 v5.4.2 // indirect
github.com/go-playground/locales v0.14.0 // indirect
github.com/go-playground/universal-translator v0.18.0 // indirect
github.com/go-playground/validator/v10 v10.11.0 // indirect
github.com/go-zookeeper/zk v1.0.3 // indirect
github.com/go-playground/validator/v10 v10.11.1 // indirect
github.com/gobwas/httphead v0.1.0 // indirect
github.com/gobwas/pool v0.2.1 // indirect
github.com/gobwas/ws v1.1.0 // indirect
github.com/goccy/go-json v0.9.11 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/mock v1.6.0 // indirect
github.com/golang-jwt/jwt/v4 v4.4.2 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/gomodule/redigo v1.8.9 // indirect
github.com/google/btree v1.1.2 // indirect
github.com/google/go-cmp v0.5.8 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/gorilla/handlers v1.5.1 // indirect
github.com/gorilla/websocket v1.5.0 // indirect
github.com/hashicorp/consul/api v1.14.0 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
github.com/hashicorp/go-hclog v1.3.0 // indirect
github.com/hashicorp/go-immutable-radix v1.3.1 // indirect
github.com/hashicorp/go-msgpack v1.1.5 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/hashicorp/go-rootcerts v1.0.2 // indirect
github.com/hashicorp/go-sockaddr v1.0.2 // indirect
github.com/hashicorp/go-uuid v1.0.3 // indirect
github.com/hashicorp/golang-lru v0.5.4 // indirect
github.com/hashicorp/memberlist v0.4.0 // indirect
github.com/hashicorp/serf v0.10.0 // indirect
github.com/huandu/xstrings v1.3.2 // indirect
github.com/hudl/fargo v1.4.0 // indirect
github.com/imdario/mergo v0.3.13 // indirect
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect
github.com/jcmturner/aescts/v2 v2.0.0 // indirect
github.com/jcmturner/dnsutils/v2 v2.0.0 // indirect
github.com/jcmturner/gofork v1.7.6 // indirect
github.com/jcmturner/gokrb5/v8 v8.4.3 // indirect
github.com/jcmturner/rpc/v2 v2.0.3 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/kevinburke/ssh_config v1.2.0 // indirect
github.com/klauspost/compress v1.15.9 // indirect
github.com/leodido/go-urn v1.2.1 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.16 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.2-0.20181231171920-c182affec369 // indirect
github.com/miekg/dns v1.1.50 // indirect
github.com/minio/highwayhash v1.0.1 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/mitchellh/hashstructure v1.1.0 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/nacos-group/nacos-sdk-go/v2 v2.1.0 // indirect
github.com/nats-io/jwt/v2 v2.0.2 // indirect
github.com/nats-io/nats.go v1.16.0 // indirect
github.com/nats-io/nats.go v1.17.0 // indirect
github.com/nats-io/nkeys v0.3.0 // indirect
github.com/nats-io/nuid v1.0.1 // indirect
github.com/nxadm/tail v1.4.8 // indirect
github.com/onsi/gomega v1.15.0 // indirect
github.com/op/go-logging v0.0.0-20160315200505-970db520ece7 // indirect
github.com/oxtoacart/bpool v0.0.0-20190530202638-03653db5a59c // indirect
github.com/patrickmn/go-cache v2.1.0+incompatible // indirect
github.com/pelletier/go-toml/v2 v2.0.5 // indirect
github.com/pierrec/lz4/v4 v4.1.16 // indirect
github.com/prometheus/client_golang v1.13.0 // indirect
github.com/prometheus/client_model v0.2.0 // indirect
github.com/prometheus/common v0.37.0 // indirect
github.com/prometheus/procfs v0.8.0 // indirect
github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529 // indirect
github.com/sergi/go-diff v1.2.0 // indirect
github.com/streadway/amqp v1.0.0 // indirect
github.com/ugorji/go/codec v1.2.7 // indirect
github.com/xanzy/ssh-agent v0.3.2 // indirect
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect
go.etcd.io/etcd/api/v3 v3.5.4 // indirect
go.etcd.io/etcd/client/pkg/v3 v3.5.4 // indirect
go.etcd.io/etcd/client/v3 v3.5.4 // indirect
go.uber.org/atomic v1.10.0 // indirect
go.uber.org/goleak v1.1.12 // indirect
go.uber.org/multierr v1.8.0 // indirect
go.uber.org/zap v1.23.0 // indirect
golang.org/x/crypto v0.0.0-20220829220503-c86fa9a7ed90 // indirect
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4 // indirect
golang.org/x/net v0.0.0-20220909164309-bea034e7d591 // indirect
golang.org/x/oauth2 v0.0.0-20220909003341-f21342109be1 // indirect
golang.org/x/sync v0.0.0-20220907140024-f12130a52804 // indirect
golang.org/x/sys v0.0.0-20220909162455-aba9fc2a8ff2 // indirect
golang.org/x/sys v0.0.0-20220915200043-7b5979e65e41 // indirect
golang.org/x/text v0.3.7 // indirect
golang.org/x/time v0.0.0-20220722155302-e5dcc9cfc0b9 // indirect
golang.org/x/tools v0.1.12 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto v0.0.0-20220909194730-69f6226f97e5 // indirect
google.golang.org/genproto v0.0.0-20220916172020-2692e8806bfa // indirect
google.golang.org/grpc v1.49.0 // indirect
gopkg.in/gcfg.v1 v1.2.3 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/natefinch/lumberjack.v2 v2.0.0 // indirect
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 // indirect
gopkg.in/warnings.v0 v0.1.2 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
)
)

914
go.sum

File diff suppressed because it is too large Load Diff

@ -25,11 +25,12 @@ func NewHandler(routerURI string, routes ...*Route) *Handler {
}
pbRoutes = append(pbRoutes, &routerclientpb.RoutesReply_Route{
IsGlobal: r.IsGlobal,
Method: r.Method,
Path: r.Path,
Endpoint: util.ReflectFunctionName(r.Endpoint),
Params: r.Params,
IsGlobal: r.IsGlobal,
Method: r.Method,
Path: r.Path,
Endpoint: util.ReflectFunctionName(r.Endpoint),
Params: r.Params,
AuthRequired: r.AuthRequired,
})
}

@ -82,11 +82,12 @@ type RoutesReply_Route struct {
unknownFields protoimpl.UnknownFields
// isGlobal=True == no prefix route
IsGlobal bool `protobuf:"varint,1,opt,name=isGlobal,proto3" json:"isGlobal,omitempty"`
Method string `protobuf:"bytes,2,opt,name=method,proto3" json:"method,omitempty"`
Path string `protobuf:"bytes,3,opt,name=path,proto3" json:"path,omitempty"`
Endpoint string `protobuf:"bytes,4,opt,name=endpoint,proto3" json:"endpoint,omitempty"`
Params []string `protobuf:"bytes,5,rep,name=params,proto3" json:"params,omitempty"`
IsGlobal bool `protobuf:"varint,1,opt,name=isGlobal,proto3" json:"isGlobal,omitempty"`
Method string `protobuf:"bytes,2,opt,name=method,proto3" json:"method,omitempty"`
Path string `protobuf:"bytes,3,opt,name=path,proto3" json:"path,omitempty"`
Endpoint string `protobuf:"bytes,4,opt,name=endpoint,proto3" json:"endpoint,omitempty"`
Params []string `protobuf:"bytes,5,rep,name=params,proto3" json:"params,omitempty"`
AuthRequired bool `protobuf:"varint,6,opt,name=authRequired,proto3" json:"authRequired,omitempty"`
}
func (x *RoutesReply_Route) Reset() {
@ -156,6 +157,13 @@ func (x *RoutesReply_Route) GetParams() []string {
return nil
}
func (x *RoutesReply_Route) GetAuthRequired() bool {
if x != nil {
return x.AuthRequired
}
return false
}
var File_routerclientpb_proto protoreflect.FileDescriptor
var file_routerclientpb_proto_rawDesc = []byte{
@ -163,13 +171,13 @@ var file_routerclientpb_proto_rawDesc = []byte{
0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x0e, 0x72, 0x6f, 0x75, 0x74, 0x65, 0x72, 0x63, 0x6c,
0x69, 0x65, 0x6e, 0x74, 0x70, 0x62, 0x1a, 0x1b, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2f, 0x70,
0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2f, 0x65, 0x6d, 0x70, 0x74, 0x79, 0x2e, 0x70, 0x72,
0x6f, 0x74, 0x6f, 0x22, 0xec, 0x01, 0x0a, 0x0b, 0x52, 0x6f, 0x75, 0x74, 0x65, 0x73, 0x52, 0x65,
0x6f, 0x74, 0x6f, 0x22, 0x90, 0x02, 0x0a, 0x0b, 0x52, 0x6f, 0x75, 0x74, 0x65, 0x73, 0x52, 0x65,
0x70, 0x6c, 0x79, 0x12, 0x1c, 0x0a, 0x09, 0x72, 0x6f, 0x75, 0x74, 0x65, 0x72, 0x55, 0x52, 0x49,
0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x09, 0x72, 0x6f, 0x75, 0x74, 0x65, 0x72, 0x55, 0x52,
0x49, 0x12, 0x39, 0x0a, 0x06, 0x72, 0x6f, 0x75, 0x74, 0x65, 0x73, 0x18, 0x02, 0x20, 0x03, 0x28,
0x0b, 0x32, 0x21, 0x2e, 0x72, 0x6f, 0x75, 0x74, 0x65, 0x72, 0x63, 0x6c, 0x69, 0x65, 0x6e, 0x74,
0x70, 0x62, 0x2e, 0x52, 0x6f, 0x75, 0x74, 0x65, 0x73, 0x52, 0x65, 0x70, 0x6c, 0x79, 0x2e, 0x52,
0x6f, 0x75, 0x74, 0x65, 0x52, 0x06, 0x72, 0x6f, 0x75, 0x74, 0x65, 0x73, 0x1a, 0x83, 0x01, 0x0a,
0x6f, 0x75, 0x74, 0x65, 0x52, 0x06, 0x72, 0x6f, 0x75, 0x74, 0x65, 0x73, 0x1a, 0xa7, 0x01, 0x0a,
0x05, 0x52, 0x6f, 0x75, 0x74, 0x65, 0x12, 0x1a, 0x0a, 0x08, 0x69, 0x73, 0x47, 0x6c, 0x6f, 0x62,
0x61, 0x6c, 0x18, 0x01, 0x20, 0x01, 0x28, 0x08, 0x52, 0x08, 0x69, 0x73, 0x47, 0x6c, 0x6f, 0x62,
0x61, 0x6c, 0x12, 0x16, 0x0a, 0x06, 0x6d, 0x65, 0x74, 0x68, 0x6f, 0x64, 0x18, 0x02, 0x20, 0x01,
@ -178,17 +186,19 @@ var file_routerclientpb_proto_rawDesc = []byte{
0x0a, 0x08, 0x65, 0x6e, 0x64, 0x70, 0x6f, 0x69, 0x6e, 0x74, 0x18, 0x04, 0x20, 0x01, 0x28, 0x09,
0x52, 0x08, 0x65, 0x6e, 0x64, 0x70, 0x6f, 0x69, 0x6e, 0x74, 0x12, 0x16, 0x0a, 0x06, 0x70, 0x61,
0x72, 0x61, 0x6d, 0x73, 0x18, 0x05, 0x20, 0x03, 0x28, 0x09, 0x52, 0x06, 0x70, 0x61, 0x72, 0x61,
0x6d, 0x73, 0x32, 0x56, 0x0a, 0x13, 0x52, 0x6f, 0x75, 0x74, 0x65, 0x72, 0x43, 0x6c, 0x69, 0x65,
0x6e, 0x74, 0x53, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x12, 0x3f, 0x0a, 0x06, 0x52, 0x6f, 0x75,
0x74, 0x65, 0x73, 0x12, 0x16, 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f,
0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x45, 0x6d, 0x70, 0x74, 0x79, 0x1a, 0x1b, 0x2e, 0x72, 0x6f,
0x75, 0x74, 0x65, 0x72, 0x63, 0x6c, 0x69, 0x65, 0x6e, 0x74, 0x70, 0x62, 0x2e, 0x52, 0x6f, 0x75,
0x74, 0x65, 0x73, 0x52, 0x65, 0x70, 0x6c, 0x79, 0x22, 0x00, 0x42, 0x40, 0x5a, 0x3e, 0x6a, 0x6f,
0x63, 0x68, 0x75, 0x6d, 0x2e, 0x64, 0x65, 0x76, 0x2f, 0x6a, 0x6f, 0x2d, 0x6d, 0x69, 0x63, 0x72,
0x6f, 0x2f, 0x72, 0x6f, 0x75, 0x74, 0x65, 0x72, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2f, 0x72,
0x6f, 0x75, 0x74, 0x65, 0x72, 0x63, 0x6c, 0x69, 0x65, 0x6e, 0x74, 0x70, 0x62, 0x3b, 0x72, 0x6f,
0x75, 0x74, 0x65, 0x72, 0x63, 0x6c, 0x69, 0x65, 0x6e, 0x74, 0x70, 0x62, 0x62, 0x06, 0x70, 0x72,
0x6f, 0x74, 0x6f, 0x33,
0x6d, 0x73, 0x12, 0x22, 0x0a, 0x0c, 0x61, 0x75, 0x74, 0x68, 0x52, 0x65, 0x71, 0x75, 0x69, 0x72,
0x65, 0x64, 0x18, 0x06, 0x20, 0x01, 0x28, 0x08, 0x52, 0x0c, 0x61, 0x75, 0x74, 0x68, 0x52, 0x65,
0x71, 0x75, 0x69, 0x72, 0x65, 0x64, 0x32, 0x56, 0x0a, 0x13, 0x52, 0x6f, 0x75, 0x74, 0x65, 0x72,
0x43, 0x6c, 0x69, 0x65, 0x6e, 0x74, 0x53, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x12, 0x3f, 0x0a,
0x06, 0x52, 0x6f, 0x75, 0x74, 0x65, 0x73, 0x12, 0x16, 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65,
0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x45, 0x6d, 0x70, 0x74, 0x79, 0x1a,
0x1b, 0x2e, 0x72, 0x6f, 0x75, 0x74, 0x65, 0x72, 0x63, 0x6c, 0x69, 0x65, 0x6e, 0x74, 0x70, 0x62,
0x2e, 0x52, 0x6f, 0x75, 0x74, 0x65, 0x73, 0x52, 0x65, 0x70, 0x6c, 0x79, 0x22, 0x00, 0x42, 0x40,
0x5a, 0x3e, 0x6a, 0x6f, 0x63, 0x68, 0x75, 0x6d, 0x2e, 0x64, 0x65, 0x76, 0x2f, 0x6a, 0x6f, 0x2d,
0x6d, 0x69, 0x63, 0x72, 0x6f, 0x2f, 0x72, 0x6f, 0x75, 0x74, 0x65, 0x72, 0x2f, 0x70, 0x72, 0x6f,
0x74, 0x6f, 0x2f, 0x72, 0x6f, 0x75, 0x74, 0x65, 0x72, 0x63, 0x6c, 0x69, 0x65, 0x6e, 0x74, 0x70,
0x62, 0x3b, 0x72, 0x6f, 0x75, 0x74, 0x65, 0x72, 0x63, 0x6c, 0x69, 0x65, 0x6e, 0x74, 0x70, 0x62,
0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
}
var (

@ -18,6 +18,7 @@ message RoutesReply {
string path = 3;
string endpoint = 4;
repeated string params = 5;
bool authRequired = 6;
}
string routerURI = 1;

@ -1,21 +0,0 @@
package util
import (
"context"
"net/http"
"strings"
"github.com/gin-gonic/gin"
"go-micro.dev/v4/metadata"
)
// CtxFromRequest adds HTTP request headers to the context as metadata
func CtxFromRequest(c *gin.Context, r *http.Request) context.Context {
md := make(metadata.Metadata, len(r.Header)+1)
for k, v := range r.Header {
// The space here is wanted spaces are not allowed in HTTP header fields.
md["PROXY "+strings.Replace(k, " ", "_", -1)] = strings.Join(v, ",")
}
return metadata.MergeContext(c, md, true)
}

@ -6,22 +6,24 @@ import (
type Route struct {
// isGlobal=True == no prefix route
IsGlobal bool
Method string
Path string
Endpoint interface{}
Params []string
IsGlobal bool
Method string
Path string
Endpoint interface{}
Params []string
AuthRequired bool
}
type Option func(*Route)
func NewRoute(opts ...Option) *Route {
route := &Route{
IsGlobal: false,
Method: MethodGet,
Path: "/",
Endpoint: nil,
Params: []string{},
IsGlobal: false,
Method: MethodGet,
Path: "/",
Endpoint: nil,
Params: []string{},
AuthRequired: false,
}
for _, o := range opts {
@ -65,3 +67,9 @@ func Params(n ...string) Option {
o.Params = n
}
}
func AuthRequired() Option {
return func(o *Route) {
o.AuthRequired = true
}
}

Loading…
Cancel
Save