A ready-to-use and simple go-micro router
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
René Jochum 361267d2f0
Update deps
1 week ago
cmd/microrouterd Register RouterClientService.Routes within the client handler 1 week ago
docker/router Add CACHEBUST for dependencies 1 week ago
internal Use components in Handler, pt2 1 week ago
.drone.yml Fix drone CI 2 weeks ago
.env.sample Make docker registry configureable 2 weeks ago
.gitignore Make docker registry configureable 2 weeks ago
LICENSE Fix the LICENSE 3 weeks ago
README.md Update deps 1 week ago
Taskfile.yml Add CACHEBUST for dependencies 1 week ago
go.mod Update deps 1 week ago
go.sum Update deps 1 week ago
handler.go Fix clientauth verifier registering 1 week ago
method.go Fix routes having the same method, update docs, update build 3 weeks ago
route.go Add user ratelimiter 2 weeks ago

README.md

Build Status Software License Go Reference

router

A dynamic router (API Gatway) for go-micro.

It looks for services that host "internal/proto/routerclientpb/routerclientpb.RouterClientService" and ask's them for routes/endpoints, then it registers that endpoints via a proxy method within gin.

Caveats

  • gin doesn't allow to delete routes, so if you want to delete a route you have to restart go-micro/router.

Usage

docker-compose without auth

docker-compose:

services:
  router:
    restart: unless-stopped
    image: docker.io/jomicro/router:0.3.8
    environment:
      - MICRO_TRANSPORT=grpc
      - MICRO_REGISTRY=nats
      - MICRO_REGISTRY_ADDRESS=nats:4222
      - MICRO_BROKER=nats
      - MICRO_BROKER_ADDRESS=nats:4222
      - MICRO_ROUTER_LISTEN=:8080
      - MICRO_ROUTER_LOG_LEVEL=info

    ports:
      - 8080:8080
    depends_on:
      - nats

docker-compose with auth

services:
  router:
    restart: unless-stopped
    image: docker.io/jomicro/router:0.3.8
    environment:
      - MICRO_AUTH2_CLIENT=jwt
      - MICRO_AUTH2_ROUTER=jwt
      - MICRO_AUTH2_JWT_AUDIENCES="key from task keys inside the auth project"
      - MICRO_AUTH2_JWT_PRIV_KEY="key from task keys inside the auth project"
      - MICRO_AUTH2_JWT_PUB_KEY="key from task keys inside the auth project"
      - MICRO_TRANSPORT=grpc
      - MICRO_REGISTRY=nats
      - MICRO_REGISTRY_ADDRESS=nats:4222
      - MICRO_BROKER=nats
      - MICRO_BROKER_ADDRESS=nats:4222
      - MICRO_ROUTER_LISTEN=:8080
      - MICRO_ROUTER_LOG_LEVEL=info

    ports:
      - 8080:8080
    depends_on:
      - nats

See cmd/microrouterd/plugins.go for a list of availabel transports, registries and brokers.

Todo

  • Add support for Streams / WebSockets.
  • Add support for debug?

Service integration examples

Have a look at internalService, jo-micro/auth2 or the author's FOSS project microlobby.

Here's some code from the jo-micro/auth2 project

import (
    "jochum.dev/jo-micro/router"
    "github.com/urfave/cli/v2"
    "go-micro.dev/v4"
    "jochum.dev/jo-micro/auth2/internal/proto/authpb"
)

func main() {
    service := micro.NewService()

    service.Init(
        micro.Action(func(c *cli.Context) error {
            s := service.Server()
            r := router.NewHandler(
              "api/auth/v1",
              router.NewRoute(
                router.Method(router.MethodGet),
                router.Path("/"),
                router.Endpoint(authpb.AuthService.List),
                router.Params("limit", "offset"),
                router.AuthRequired(),
                router.RatelimitUser("1-S", "10-M"),
              ),
              router.NewRoute(
                router.Method(router.MethodPost),
                router.Path("/login"),
                router.Endpoint(authpb.AuthService.Login),
                router.RatelimitClientIP("1-S", "10-M", "30-H", "100-D"),
              ),
              router.NewRoute(
                router.Method(router.MethodPost),
                router.Path("/register"),
                router.Endpoint(authpb.AuthService.Register),
                router.RatelimitClientIP("1-M", "10-H", "50-D"),
              ),
              router.NewRoute(
                router.Method(router.MethodPost),
                router.Path("/refresh"),
                router.Endpoint(authpb.AuthService.Refresh),
                router.RatelimitClientIP("1-M", "10-H", "50-D"),
              ),
              router.NewRoute(
                router.Method(router.MethodDelete),
                router.Path("/:userId"),
                router.Endpoint(authpb.AuthService.Delete),
                router.Params("userId"),
                router.AuthRequired(),
                router.RatelimitUser("1-S", "10-M"),
              ),
              router.NewRoute(
                router.Method(router.MethodGet),
                router.Path("/:userId"),
                router.Endpoint(authpb.AuthService.Detail),
                router.Params("userId"),
                router.AuthRequired(),
                router.RatelimitUser("100-M"),
              ),
              router.NewRoute(
                router.Method(router.MethodPut),
                router.Path("/:userId/roles"),
                router.Endpoint(authpb.AuthService.UpdateRoles),
                router.Params("userId"),
                router.AuthRequired(),
                router.RatelimitUser("1-M"),
              ),
            )
            r.RegisterWithServer(s)
        })
    )
}

Developers corner

Build podman/docker image

Prerequesits

Build

cp .env.sample .env
task

Remove everything

task rm

Authors

License

Its dual licensed:

  • Apache-2.0
  • GPL-2.0-or-later