Build your API Gateway


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 go environment

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/

Clone project into that directory:

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

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

cd $HOME/go/src/
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


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

Read more about go generate


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:                                     LoadBalancer --< upstream
               |            (plugins/middleware)             |
               |                                             |
               `--> RequestID --> WAF --> CORS --> Promethehs´

The configuration file would be:

    plugins: # the order of the plugins meters
      - requestid
      - waf
      - cors
      - prometheus

  cors: ["", "cors.CORS"]
  csrf: ["", "csrf.CSRF"]
  prometheus: ["", "prometheus.Prometheus"]
  requestid: ["", "requestid.RequestID"]
  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