Build your API Gateway


build

Because of the plugins (middleware) is not possible to have a generic binary, therefore after creating the configuration file and based on the plugins defined the gateway needs to be build.

Setup

Setup go environment https://golang.org/doc/install

go >= 1.7 is required

For example using $HOME/go for your workspace

export GOPATH=$HOME/go

Create the directory:

mkdir -p $HOME/go/src/github.com/slashquery

Clone project into that directory:

git clone [email protected]:slashquery/slashquery.git $HOME/go/src/github.com/slashquery/slashquery

Create your configuration file and type make CONFIG=/path/to/slashquery.yml:

cd $HOME/go/src/github.com/slashquery/slashquery
make CONFIG=/path/to/slashquery.yml

How it works

The routes section in the configuration file is used to create the router routes.go file.

The routes.go file is generated by running within the slashquery source directory:

make CONFIG=/path/to/slashquery.yml

or

go run genroutes.go -f /path/to/slashquery.yml

Read more about go generate

genroutes.go

genroutes.go parses the configuration file and creates the routes.go file based on defined plugins.

For example, for all the requests to path /foo, if this plugins are defined:

  • RequestID
  • WAF
  • CORS
  • Prometheus

See more about the plugins here.

The request flow would be something like this:

api.example.com/foo                                     LoadBalancer --< upstream
               |            (plugins/middleware)             |
               |                                             |
               `--> RequestID --> WAF --> CORS --> Promethehs´

The configuration file would be:

routes:
  foo:
    url: http://foo.example.com
    plugins: # the order of the plugins meters
      - requestid
      - waf
      - cors
      - prometheus

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"]

When adding plugins to a route, the order meters, this means that plugins are called in the order that they are entered, in the previous example, the first plugin to be called is RequestID and the latest one Prometheus.

The plugins section contains all the plugins available.

comments powered by Disqus