Martin Helmich
Mittwald CM Service GmbH & Co. KG
This code is GPL-licensed.
This repository contains a service gateway that can be used both as a single-signon gateway or API gateway for a microservice architecture. It implements features like load balancing, rate limiting and (rudimentary) HTTP caching. It uses Consul for service discovery and configuration management.
For building, you will need a halfway current Go SDK (tested with 1.4 and 1.5). Then simply go install
:
> go install github.com/mittwald/servicegateway
This will produce a (more or less) static binary that you can deploy without any dependencies.
Alternatively, use the Makefile that is shipped within this repository to build the binary and/or a Docker image containing this application:
> make
> make docker
The basic configuration is read from a configuration file that by default is expected to be located in /etc/servicegateway.json
. However, you can override that location using the -config
command line parameter.
Check the example-configs directory for example configurations for usage as API gateway and Single-SignOn gateway.
Most of the configuration options (that are not required for the actual program startup) can also be provided by Consul, an open-source service discovery engine. Configuration can be stored in Consul's key-value store.
Consul uses a hierarchical key-value store. All configuration items for the servicegateway must be stored under a common key prefix that is supplied via the -consul-base
command-line parameter.
This affects the following configuration items:
- Rate-limiting configuration (key
<base-prefix>/ratelimiting
) - Caching configuration (key
<base-prefix>/caching
) - Upstream application (keys
<base-prefix>/application/<app-identifier>
)
Each upstream application is its own key/value pair with the value being a JSON document describing the application.
You can configure the key prefix in which the service gateway should look for configured applications using the -consul-base
parameter:
./servicegateway -consul-base gateway/applications
You can create a new application like follows (substitute <name>
with an arbitrary identifier for your application):
> curl -X PUT -d @app.json http://localhost:8500/v1/kv/gateway/applications/<name>
This PUT
s the contents of the following file app.json
into the Consul key/value store:
{
"backend": {
"url": "http://httpbin.org"
},
"caching": {
"auto_flush": true,
"enabled": true,
"ttl": 3600
},
"routing": {
"type": "path",
"path": "/bin"
},
"rate_limiting": true
}
Important: Configuration changes made in Consul will become effective immediately, without needing to restart the service gateway.
See the documentation reference.
tbw.
tbw.