Skip to content

Commit 024b624

Browse files
committed
Updated target api version to 1.1.13.
Replaced traefik with Caddy as reverse proxy. Added health check for api in docker compose (resolves #4). Updated node to v14 and other dependencies.
1 parent 76d2d1e commit 024b624

File tree

6 files changed

+71
-66
lines changed

6 files changed

+71
-66
lines changed

README.md

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -37,30 +37,22 @@ Running the API image alone requires that you have a MongoDB instance and (ideal
3737
3838
## Running a production-ready service
3939
40-
If you do not already have a MongoDB instance or are intending to expose your xBrowserSync service over the internet then it is recommended to use the provided [`docker-compose.yml`](https://github.com/xbrowsersync/api-docker/blob/master/docker-compose.yml) which will create fully configured containers for a MongoDB database, the xBrowserSync API and a [traefik](https://traefik.io/) reverse proxy web server to run in front of the API. Traefik automatically acquires and updates SSL certificates from [Let's Encrypt](https://letsencrypt.org/) so that your xBrowserSync API service will run securely over HTTPS.
40+
If you do not already have a MongoDB instance or are intending to expose your xBrowserSync service over the internet then it is recommended to use the provided [`docker-compose.yml`](https://github.com/xbrowsersync/api-docker/blob/master/docker-compose.yml) which will create fully configured containers for a MongoDB database, the xBrowserSync API and a [Caddy](https://caddyserver.com/) web server as a reverse proxy in front of the API. Caddy automatically acquires and updates SSL certificates so that your xBrowserSync API service will run securely over HTTPS.
4141
4242
1. Clone the [api-docker](https://github.com/xbrowsersync/api-docker/) GitHub repo:
4343
4444
```
4545
$ git clone https://github.com/xbrowsersync/api-docker.git
4646
```
4747
48-
2. Secure the [`acme.json`](https://github.com/xbrowsersync/api-docker/blob/master/acme.json) file as per traefik's requirements:
48+
2. Open the [`.env`](https://github.com/xbrowsersync/api-docker/blob/master/.env) file in a text editor and update the `XBS_API_HOSTNAME` value to correspond to the host name that the API service will be exposed over (ensure you have configured your DNS provider to point the desired host name to your host's IP address). Also, change the `XBS_DB_USERNAME` and `XBS_DB_PASSWORD` values to any of your choosing.
4949
50-
```
51-
$ sudo chmod 600 acme.json
52-
```
53-
54-
3. Open the [`.env`](https://github.com/xbrowsersync/api-docker/blob/master/.env) file in a text editor and update the `XBS_API_HOSTNAME` value to correspond to the host name that the API service will be exposed over (ensure you have configured your DNS provider to point the desired host name to your host's IP address). Also, change the `XBS_DB_USERNAME` and `XBS_DB_PASSWORD` values to any of your choosing.
55-
56-
4. Open the [`traefik.toml`](https://github.com/xbrowsersync/api-docker/blob/master/traefik.toml) file in a text editor and update the `email` value on [line 18](https://github.com/xbrowsersync/api-docker/blob/master/traefik.toml#L18) to your own email address in order to successfully acquire an SSL certificate from [Let's Encrypt](https://letsencrypt.org/).
57-
58-
5. (Optionally) open the [`settings.json`](https://github.com/xbrowsersync/api-docker/blob/master/settings.json) file and include any custom [settings](https://github.com/xbrowsersync/api#3-modify-configuration-settings) values you wish to run on your service. Important: do not change the `db.host` value.
50+
3. (Optionally) open the [`settings.json`](https://github.com/xbrowsersync/api-docker/blob/master/settings.json) file and include any custom [settings](https://github.com/xbrowsersync/api#3-modify-configuration-settings) values you wish to run on your service. Important: do not change the `db.host` value.
5951
60-
6. Run the following command to start the containers:
52+
4. Run the following command to start the containers:
6153
6254
```
63-
$ sudo docker-compose up -d
55+
$ docker-compose up -d
6456
```
6557
6658
You can now access your xBrowserSync API service over HTTPS at the value of `XBS_API_HOSTNAME` defined in the [`.env`](https://github.com/xbrowsersync/api-docker/blob/master/.env) file.

acme.json

Whitespace-only changes.

docker-compose.yml

Lines changed: 20 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,14 @@
11
version: "3.7"
22

3-
volumes:
4-
xbs-db-data:
5-
63
services:
74
db:
85
container_name: "xbs-db"
96
environment:
107
- "MONGO_INITDB_DATABASE=xbrowsersync"
118
- "MONGO_INITDB_ROOT_PASSWORD=$XBS_DB_PASSWORD"
129
- "MONGO_INITDB_ROOT_USERNAME=$XBS_DB_USERNAME"
13-
image: "mongo:4.2.6"
14-
labels:
15-
- "traefik.enable=false"
16-
networks:
17-
- "xbs-net"
18-
restart: "always"
10+
image: "mongo:4.4.4"
11+
restart: "unless-stopped"
1912
volumes:
2013
- "xbs-db-data:/data/db"
2114
- "./mongoconfig.js:/docker-entrypoint-initdb.d/mongoconfig.js"
@@ -26,33 +19,32 @@ services:
2619
environment:
2720
- "XBROWSERSYNC_DB_PWD=$XBS_DB_PASSWORD"
2821
- "XBROWSERSYNC_DB_USER=$XBS_DB_USERNAME"
29-
image: "xbrowsersync/api:1.1.12"
30-
labels:
31-
- "traefik.frontend.rule=Host:$XBS_API_HOSTNAME"
32-
- "traefik.port=8080"
33-
networks:
34-
- "xbs-net"
35-
restart: "always"
22+
healthcheck:
23+
test: [ "CMD", "node", "/usr/src/api/healthcheck.js" ]
24+
interval: "1m"
25+
timeout: "10s"
26+
retries: "5"
27+
start_period: "30s"
28+
image: "xbrowsersync/api:1.1.13"
29+
restart: "unless-stopped"
3630
volumes:
3731
- "./settings.json:/usr/src/api/config/settings.json"
32+
- "./healthcheck.js:/usr/src/api/healthcheck.js"
3833
reverse-proxy:
39-
command: "--api --docker"
34+
command: "caddy reverse-proxy --from $XBS_API_HOSTNAME --to api:8080"
4035
container_name: "xbs-reverse-proxy"
4136
depends_on:
4237
- "api"
43-
image: "traefik:1.7.24-alpine"
44-
labels:
45-
- "traefik.enable=false"
46-
networks:
47-
- "xbs-net"
38+
image: "caddy:2.3.0-alpine"
4839
ports:
4940
- "443:443"
5041
- "80:80"
51-
restart: "always"
42+
restart: "unless-stopped"
5243
volumes:
53-
- "/var/run/docker.sock:/var/run/docker.sock"
54-
- "./acme.json:/acme.json"
55-
- "./traefik.toml:/traefik.toml"
44+
- "xbs-caddy-config:/config"
45+
- "xbs-caddy-data:/data"
5646

57-
networks:
58-
xbs-net:
47+
volumes:
48+
xbs-caddy-config:
49+
xbs-caddy-data:
50+
xbs-db-data:

dockerfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
FROM node:12.16.2-alpine
1+
FROM node:14.16.0-alpine
22

33
# Set environment variables
4-
ENV XBROWSERSYNC_API_VERSION 1.1.12
4+
ENV XBROWSERSYNC_API_VERSION 1.1.13
55

66
WORKDIR /usr/src/api
77

healthcheck.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
const http = require('http');
2+
3+
const response = http.request(
4+
{
5+
host: '0.0.0.0',
6+
method: 'GET',
7+
path: '/info',
8+
port: 8080,
9+
timeout: 2000,
10+
},
11+
(res) => {
12+
let body = '';
13+
res.setEncoding('utf8');
14+
15+
res.on('data', (chunk) => {
16+
body += chunk;
17+
});
18+
19+
res.on('end', () => {
20+
if (res.statusCode === 200) {
21+
const payload = JSON.parse(body);
22+
switch (payload.status) {
23+
case 1:
24+
case 3:
25+
console.log('HEALTHCHECK: online');
26+
process.exit(0);
27+
case 2:
28+
default:
29+
console.log('HEALTHCHECK: offline');
30+
}
31+
} else {
32+
console.log('HEALTHCHECK: offline');
33+
}
34+
process.exit(1);
35+
});
36+
}
37+
);
38+
39+
response.on('error', function (err) {
40+
console.log('HEALTHCHECK: offline');
41+
process.exit(1);
42+
});
43+
44+
response.end();

traefik.toml

Lines changed: 0 additions & 23 deletions
This file was deleted.

0 commit comments

Comments
 (0)