Microservices using an API Gateway.


slashquery microservices API

The big picture:

   (slashquery API Gateway)

api.example.com/<path endpoint>
               |            (plugins)       (upstream)      (microservices)
               |                                            /home
               | /*     >--[middleware]---[LoadBalancer]--< /home
               |                                            /home
               |
               |                                            /foo
               | /foo/* >--[middleware]---[LoadBalancer]--< /foo
               |                                            /foo
               |
               |                                            /foo-version-2
 (versioning)  | /foo/* >--[middleware]---[LoadBalancer]--< /foo-version-2
               |                                            /foo-version-2
               |
               |                                            /bar
               ` /bar/* >--[middleware]---[LoadBalancer]--< /bar
                                                            /bar

More about versioning: https://violetear.org/post/versioning/

The configuration file in YAML format:

config:
  host: ~ # listen all IP's
  port: 80
  resolver: 8.8.8.8  # fallback to /etc/resolve.conf
  request-id: X-Request-Id # use existing header

routes:
  ~: # api.example.com/* -> http://home.example.com/*
    path: /*
    upstream: home
    host: example.com
    plugins:
      - requestid
      - waf

  foo: # api.example.com/foo/ -> https://foo.example.com
    url: https://foo.example.com
    insecure: yes # allow self signed certificates
    methods:
      - POST
    plugins:
      - requestid
      - waf
      - cors

  foo#version.2: # api.example.com/foo/ -> https://foo-version-2.example.com
    url: https://foo-version-2.example.com
    insecure: yes # allow self signed certificates
    methods:
      - POST
    plugins:
      - requestid
      - waf
      - cors

  bar: # api.example.com/bar -> http://bar.example.com
    url: http://bar.example.com
    methods:
      - GET
      - HEAD
      - OPTIONS
    plugins:
      - requestid
      - waf
      - cors

upstreams:
  home:
    timeout: 3
    servers:
      - home.example.com
      - home.example.org

plugins:
  cors:  ["github.com/slashquery-plugins/cors", "cors.CORS"]
  csrf:  ["github.com/slashquery-plugins/csrf", "csrf.CSRF"]
  prometheus: ["github.com/slashquery-plugins/prometheus", "prometheus.Prometheus"]
  requestid: ["github.com/slashquery-plugins/requestid", "requestid.RequestID"]
  waf: ["github.com/slashquery-plugins/waf", "waf.WAF"]

path /*

If ending the path with a “*” the full requested path will be passed, for example:

http://api.slashquery.com/foo/bar/xxx/yyy

If path := /foo/*:

http://<upstream>/foo/bar/xxx/yyy

If path := /foo:

http://<upstream>/foo
comments powered by Disqus