Skip to content

Commit 05e0e7d

Browse files
committed
# added the debug version of the api-gateway activated with the env var DEBUG=true, and support for running unit tests via Test NGINX module
1 parent 9618ed4 commit 05e0e7d

File tree

6 files changed

+106
-43
lines changed

6 files changed

+106
-43
lines changed

Dockerfile

+40-9
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,24 @@ FROM alpine:latest
88

99
RUN apk update \
1010
&& apk add gcc tar libtool zlib jemalloc jemalloc-dev perl \
11-
make musl-dev openssl-dev pcre-dev g++ zlib-dev curl python
11+
make musl-dev openssl-dev pcre-dev g++ zlib-dev curl python \
12+
perl-test-longstring perl-list-moreutils perl-http-message
1213

1314
ENV OPENRESTY_VERSION 1.9.3.1
1415
ENV NAXSI_VERSION 0.53-2
1516
ENV PCRE_VERSION 8.37
17+
ENV TEST_NGINX_VERSION 0.24
1618
ENV HMAC_LUA_VERSION 1.0.0
1719
ENV REQUEST_VALIDATION_VERSION 1.0.1
1820

1921
RUN mkdir -p /tmp/api-gateway/
20-
ADD https://github.com/nbs-system/naxsi/archive/${NAXSI_VERSION}.tar.gz /tmp/api-gateway/naxsi-${NAXSI_VERSION}.tar.gz
21-
ADD http://downloads.sourceforge.net/project/pcre/pcre/${PCRE_VERSION}/pcre-${PCRE_VERSION}.tar.gz /tmp/api-gateway/prce-${PCRE_VERSION}.tar.gz
22-
ADD http://openresty.org/download/ngx_openresty-${OPENRESTY_VERSION}.tar.gz /tmp/api-gateway/ngx_openresty-${OPENRESTY_VERSION}.tar.gz
23-
ADD https://github.com/adobe-apiplatform/api-gateway-request-validation/archive/${REQUEST_VALIDATION_VERSION}.tar.gz /tmp/api-gateway/api-gateway-request-validation-${REQUEST_VALIDATION_VERSION}.tar.gz
24-
ADD https://github.com/adobe-apiplatform/api-gateway-hmac/archive/${HMAC_LUA_VERSION}.tar.gz /tmp/api-gateway/api-gateway-hmac-${HMAC_LUA_VERSION}.tar.gz
2522

26-
RUN cd /tmp/api-gateway/ \
23+
RUN cd /tmp/api-gateway/ \
24+
&& curl -L https://github.com/nbs-system/naxsi/archive/${NAXSI_VERSION}.tar.gz -o /tmp/api-gateway/naxsi-${NAXSI_VERSION}.tar.gz \
25+
&& curl -L http://downloads.sourceforge.net/project/pcre/pcre/${PCRE_VERSION}/pcre-${PCRE_VERSION}.tar.gz -o /tmp/api-gateway/prce-${PCRE_VERSION}.tar.gz \
26+
&& curl -L https://github.com/adobe-apiplatform/apigateway/releases/download/openresty_backup_1.9.3.1/ngx_openresty-1.9.3.1.tar.gz -o /tmp/api-gateway/ngx_openresty-${OPENRESTY_VERSION}.tar.gz \
27+
&& curl -L https://github.com/adobe-apiplatform/api-gateway-request-validation/archive/${REQUEST_VALIDATION_VERSION}.tar.gz -o /tmp/api-gateway/api-gateway-request-validation-${REQUEST_VALIDATION_VERSION}.tar.gz \
28+
&& curl -L https://github.com/adobe-apiplatform/api-gateway-hmac/archive/${HMAC_LUA_VERSION}.tar.gz -o /tmp/api-gateway/api-gateway-hmac-${HMAC_LUA_VERSION}.tar.gz \
2729
&& tar -xf ./ngx_openresty-${OPENRESTY_VERSION}.tar.gz \
2830
&& tar -xf ./prce-${PCRE_VERSION}.tar.gz \
2931
&& tar -xf ./naxsi-${NAXSI_VERSION}.tar.gz \
@@ -35,7 +37,30 @@ RUN cd /tmp/api-gateway/ \
3537
&& _localstatedir="/var" \
3638
&& _sysconfdir="/etc" \
3739
&& _sbindir="$_exec_prefix/sbin" \
40+
&& echo "building debugging version of the api-gateway ... " \
3841
&& ./configure \
42+
--prefix=${_exec_prefix}/api-gateway \
43+
--sbin-path=${_sbindir}/api-gateway-debug \
44+
--conf-path=${_sysconfdir}/api-gateway/api-gateway.conf \
45+
--error-log-path=${_localstatedir}/log/api-gateway/error.log \
46+
--http-log-path=${_localstatedir}/log/api-gateway/access.log \
47+
--pid-path=${_localstatedir}/run/api-gateway.pid \
48+
--lock-path=${_localstatedir}/run/api-gateway.lock \
49+
--add-module=../naxsi-${NAXSI_VERSION}/naxsi_src/ \
50+
--with-pcre=../pcre-${PCRE_VERSION}/ --with-pcre-jit \
51+
--with-http_ssl_module \
52+
--with-http_stub_status_module \
53+
--with-luajit \
54+
--without-http_ssi_module \
55+
--without-http_userid_module \
56+
--without-http_uwsgi_module \
57+
--without-http_scgi_module \
58+
--with-debug \
59+
-j${NPROC} \
60+
&& make -j${NPROC} \
61+
&& make install \
62+
&& echo "building regular version of the api-gateway ... " \
63+
&& ./configure \
3964
--prefix=${_exec_prefix}/api-gateway \
4065
--sbin-path=${_sbindir}/api-gateway \
4166
--conf-path=${_sysconfdir}/api-gateway/api-gateway.conf \
@@ -68,7 +93,13 @@ RUN cd /tmp/api-gateway/ \
6893
LUA_LIB_DIR=${_exec_prefix}/api-gateway/lualib \
6994
INSTALL=/tmp/api-gateway/ngx_openresty-${OPENRESTY_VERSION}/build/install \
7095
&& rm -rf /var/cache/apk/* \
71-
&& rm -rf /tmp/api-gateway
96+
&& rm -rf /tmp/api-gateway \
97+
&& echo " ... adding Nginx Test support" \
98+
&& curl -L https://github.com/openresty/test-nginx/archive/v${TEST_NGINX_VERSION}.tar.gz -o /usr/local/test-nginx-${TEST_NGINX_VERSION}.tar.gz \
99+
&& cd /usr/local/ \
100+
&& tar -xf /usr/local/test-nginx-${TEST_NGINX_VERSION}.tar.gz \
101+
&& rm /usr/local/test-nginx-${TEST_NGINX_VERSION}.tar.gz \
102+
&& ln -s /usr/local/sbin/api-gateway-debug /usr/local/sbin/nginx
72103

73104

74105
COPY init.sh /etc/init-container.sh
@@ -81,4 +112,4 @@ RUN adduser -S nginx-api-gateway \
81112
ONBUILD COPY api-gateway-config /etc/api-gateway
82113

83114

84-
CMD ["/etc/init-container.sh"]
115+
ENTRYPOINT ["/etc/init-container.sh"]

Makefile

+9-9
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,31 @@ DOCKER_TAG ?= snapshot-`date +'%Y%m%d-%H%M'`
22
DOCKER_REGISTRY ?= ''
33

44
docker:
5-
docker build -t apiplatform/apigateway .
5+
docker build -t adobeapiplatform/apigateway .
66

77
.PHONY: docker-ssh
88
docker-ssh:
9-
docker run -ti --entrypoint='bash' apiplatform/apigateway:latest
9+
docker run -ti --entrypoint='bash' adobeapiplatform/apigateway:latest
1010

1111
.PHONY: docker-run
1212
docker-run:
13-
docker run --rm --name="apigateway" -p 8080:80 apiplatform/apigateway:latest ${DOCKER_ARGS}
13+
docker run --rm --name="apigateway" -p 80:80 adobeapiplatform/apigateway:latest ${DOCKER_ARGS}
1414

1515
.PHONY: docker-debug
1616
docker-debug:
1717
#Volumes directories must be under your Users directory
1818
mkdir -p ${HOME}/tmp/apiplatform/apigateway
1919
rm -rf ${HOME}/tmp/apiplatform/apigateway/api-gateway-config
20-
#ln -s `pwd`/api-gateway-config ${HOME}/tmp/apiplatform/apigateway/api-gateway-config
2120
cp -r `pwd`/api-gateway-config ${HOME}/tmp/apiplatform/apigateway/
2221
docker run --name="apigateway" \
23-
-p 8080:80 \
24-
-e "LOG_LEVEL=debug" \
22+
-p 80:80 \
23+
-e "LOG_LEVEL=info" -e "DEBUG=true" \
2524
-v ${HOME}/tmp/apiplatform/apigateway/api-gateway-config/:/etc/api-gateway \
26-
apiplatform/apigateway:latest ${DOCKER_ARGS}
25+
adobeapiplatform/apigateway:latest ${DOCKER_ARGS}
2726

2827
.PHONY: docker-reload
2928
docker-reload:
29+
cp -r `pwd`/api-gateway-config ${HOME}/tmp/apiplatform/apigateway/
3030
docker exec apigateway api-gateway -t -p /usr/local/api-gateway/ -c /etc/api-gateway/api-gateway.conf
3131
docker exec apigateway api-gateway -s reload
3232

@@ -41,6 +41,6 @@ docker-stop:
4141

4242
.PHONY: docker-push
4343
docker-push:
44-
docker tag -f apiplatform/apigateway $(DOCKER_REGISTRY)/apiplatform/apigateway:$(DOCKER_TAG)
45-
docker push $(DOCKER_REGISTRY)/apiplatform/apigateway:$(DOCKER_TAG)
44+
docker tag -f adobeapiplatform/apigateway $(DOCKER_REGISTRY)/adobeapiplatform/apigateway:$(DOCKER_TAG)
45+
docker push $(DOCKER_REGISTRY)/adobeapiplatform/apigateway:$(DOCKER_TAG)
4646

README.md

+44-19
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,35 @@
11
apigateway
22
=============
3-
A performant API Gateway based on Openresty and Nginx.
3+
A performant API Gateway based on Openresty and NGINX.
44

5+
### Quick start
6+
7+
```
8+
docker run --name="apigateway" \
9+
-p 80:80 \
10+
-e "MARATHON_HOST=http://<marathon_host>:<port>/" \
11+
-e "LOG_LEVEL=info" \
12+
adobeapiplatform/apigateway:latest
13+
```
14+
15+
This command starts an API Gateway that automatically discovers the services running in Marathon.
16+
The discovered services are exposed on individual VHosts as you can see in the [config file](https://github.com/adobe-apiplatform/apigateway/blob/master/api-gateway-config/conf.d/marathon_apis.conf#L36).
17+
18+
#### Accessing a Marathon app
19+
20+
For example, if you have an application named `hello-world` you can access it on its VHost in 2 ways:
21+
1. Edit `/etc/hosts` and add `<docker_host_ip> hello-world.api.localhost` then browse to `http://hello-world.api.localhost`
22+
2. Sending the Host header in a curl command: `curl -H "Host:hello-world.api.localhost" http://<docker_host_ip>`
23+
24+
The [discovery script](https://github.com/adobe-apiplatform/apigateway/blob/master/api-gateway-config/marathon-service-discovery.sh) is provided as an example for a quick-start and it can be replaced with your favourite discovery mechanism.
25+
The script updates a [configuration file](https://github.com/adobe-apiplatform/apigateway/blob/master/api-gateway-config/environment.conf.d/api-gateway-upstreams.http.conf) containing all the NGINX upstreams that are used in the [config file](https://github.com/adobe-apiplatform/apigateway/blob/master/api-gateway-config/conf.d/marathon_apis.conf#L36).
26+
27+
#### Resolvers
28+
While starting up this container automatically creates the `/etc/api-gateway/conf.d/includes/resolvers.conf` config file using `/etc/resolv.conf` as the source.
29+
To learn more about the `resolver` directive in NGINX see the [docs](http://nginx.org/en/docs/http/ngx_http_core_module.html#resolver).
30+
31+
#### Running the API Gateway outside of Marathon and Mesos
32+
Besides the discovery part which is dependent on Marathon at the moment, the API Gateway can run on its own as well. The Marathon service discovery is activated with the ` -e "MARATHON_HOST=http://<marathon_host>:<port>/"`.
533

634
### Developer guide
735

@@ -19,25 +47,25 @@ A performant API Gateway based on Openresty and Nginx.
1947
```
2048
make docker-run
2149
```
22-
The main API Gateway process is exposed to port 8080. To test that the Gateway works see its `health-check`:
50+
The main API Gateway process is exposed to port `80`. To test that the Gateway works see its `health-check`:
2351
```
24-
$ curl http://<docker_host_ip>:8080/health-check
52+
$ curl http://<docker_host_ip>/health-check
2553
API-Platform is running!
2654
```
2755
If you're up for a quick performance test, you can play with Apache Benchmark via Docker:
2856

2957
```
30-
docker run jordi/ab ab -k -n 200000 -c 500 http://<docker_host_ip>:8080/health-check
58+
docker run jordi/ab ab -k -n 200000 -c 500 http://<docker_host_ip>/health-check
3159
```
3260

3361
To run docker mounting the local `api-gateway-config` directory into `/etc/api-gateway/` issue:
3462

3563
```bash
3664
$ make docker-debug
3765
```
38-
In debug mode docker container starts with `-e "LOG_LEVEL=debug"` which will give you a little more debugging information.
39-
40-
To enable the full debug logging in nginx ( using a build configured `--with-debug` ) there will be a separate container.
66+
In debug mode the docker container starts a special `api-gateway` compiled `--with-debug` providing very detailed debugging information.
67+
When started with `-e "LOG_LEVEL=info"` the output is quite verbose.
68+
To learn more about this option visit [NGINX docs](http://nginx.org/en/docs/debugging_log.html).
4169

4270
When done stop the image:
4371
```
@@ -61,7 +89,7 @@ For optimal performance leave the `network` on `HOST` mode. To learn more about
6189
"container": {
6290
"type": "DOCKER",
6391
"docker": {
64-
"image": "apiplatform/apigateway:latest",
92+
"image": "adobeapiplatform/apigateway:latest",
6593
"forcePullImage": true,
6694
"network": "HOST"
6795
}
@@ -71,16 +99,8 @@ For optimal performance leave the `network` on `HOST` mode. To learn more about
7199
"env": {
72100
"MARATHON_HOST": "http://<marathon_host>:<marathon_port>"
73101
},
74-
"constraints": [
75-
[
76-
"hostname",
77-
"UNIQUE"
78-
]
79-
],
80-
"acceptedResourceRoles": ["slave_public"],
81-
"ports": [
82-
80
83-
],
102+
"constraints": [ [ "hostname","UNIQUE" ] ],
103+
"ports": [ 80 ],
84104
"healthChecks": [
85105
{
86106
"protocol": "HTTP",
@@ -95,6 +115,11 @@ For optimal performance leave the `network` on `HOST` mode. To learn more about
95115
}
96116
```
97117

118+
To run the Gateway only on specific nodes marked with `slave_public` you can add the property bellow to the main JSON object:
119+
```
120+
"acceptedResourceRoles": [ "slave_public" ]
121+
```
122+
98123
##### Auto-discover and register Marathon tasks in the Gateway
99124

100125
To enable auto-discovery in a Mesos with Marathon framework define the following Environment Variables:
@@ -109,6 +134,6 @@ docker run --name="apigateway" \
109134
-p 8080:80 \
110135
-e "MARATHON_HOST=http://<marathon_host>:<port>/" \
111136
-e "LOG_LEVEL=info" \
112-
apiplatform/apigateway:latest
137+
adobeapiplatform/apigateway:latest
113138
```
114139

api-gateway-config/conf.d/marathon_apis.conf

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ server {
3333
# listen 8080;
3434

3535
# listenes on <marathon_app_name>.api.any.domain with the assumption that the marathon_app_name has been defined in marathon. TBD what to do when the app is not found
36-
server_name ~^(?<marathon_app_name>.[^\.]+)\.api\.(?<domain>.+);
36+
server_name ~^(?<marathon_app_name>.[^\.]+)\.(api|gw)\.(?<domain>.+);
3737

3838
server_tokens off;
3939

api-gateway-config/html/index.html

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
<html>
2525
<head>
26-
<title>Welcome to API Gateway ! An Nginx and Openresty based gateway.</title>
26+
<title>Welcome to API Gateway ! An NGINX and Openresty based gateway.</title>
2727
<style>
2828
body {
2929
width: 35em;
@@ -34,7 +34,7 @@
3434
</head>
3535
<body>
3636
<h1>Welcome to API Gateway ! </h1>
37-
<h3>An Nginx and Openresty based API Gateway.</h3>
37+
<h3>An NGINX and Openresty based API Gateway.</h3>
3838
<p>If you see this page, the API Gateway is successfully installed and
3939
working. Further configuration may be required.</p>
4040

init.sh

+10-3
Original file line numberDiff line numberDiff line change
@@ -21,22 +21,29 @@
2121
# * DEALINGS IN THE SOFTWARE.
2222
# *
2323
# */
24-
24+
debug_mode=$(echo $DEBUG)
2525
log_level=${LOG_LEVEL:-warn}
2626
marathon_host=$(echo $MARATHON_HOST)
27+
sleep_duration=${MARATHON_POLL_INTERVAL:-5}
2728

2829
echo "Starting api-gateway ..."
30+
if [ "${debug_mode}" == "true" ]; then
31+
echo " ... in DEBUG mode "
32+
mv /usr/local/sbin/api-gateway /usr/local/sbin/api-gateway-no-debug
33+
ln -sf /usr/local/sbin/api-gateway-debug /usr/local/sbin/api-gateway
34+
fi
35+
2936
/usr/local/sbin/api-gateway -V
3037
echo "------"
3138

3239
echo resolver $(awk 'BEGIN{ORS=" "} /nameserver/{print $2}' /etc/resolv.conf | sed "s/ $/;/g") > /etc/api-gateway/conf.d/includes/resolvers.conf
3340
echo " ... with dns $(cat /etc/api-gateway/conf.d/includes/resolvers.conf)"
3441

3542
if [[ -n "${marathon_host}" ]]; then
36-
echo " ... starting Marathon Service Discovery "
43+
echo " ... starting Marathon Service Discovery on ${marathon_host}"
3744
touch /var/run/apigateway-config-watcher.lastrun
3845
# start marathon's service discovery
39-
while true; do /etc/api-gateway/marathon-service-discovery.sh > /dev/stderr; sleep 5; done &
46+
while true; do /etc/api-gateway/marathon-service-discovery.sh > /dev/stderr; sleep ${sleep_duration}; done &
4047
# start simple statsd logger
4148
#
4249
# ASSUMPTION: there is a graphite app named "api-gateway-graphite" deployed in marathon

0 commit comments

Comments
 (0)