From 024b624d9e3dbf0736ab9472554d18626a5994be Mon Sep 17 00:00:00 2001 From: nero120 Date: Fri, 2 Apr 2021 01:27:43 +0100 Subject: [PATCH] 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. --- README.md | 18 +++++------------ acme.json | 0 docker-compose.yml | 48 +++++++++++++++++++--------------------------- dockerfile | 4 ++-- healthcheck.js | 44 ++++++++++++++++++++++++++++++++++++++++++ traefik.toml | 23 ---------------------- 6 files changed, 71 insertions(+), 66 deletions(-) delete mode 100644 acme.json create mode 100644 healthcheck.js delete mode 100644 traefik.toml diff --git a/README.md b/README.md index 836c535..14b4a79 100644 --- a/README.md +++ b/README.md @@ -37,7 +37,7 @@ Running the API image alone requires that you have a MongoDB instance and (ideal ## Running a production-ready service -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. +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. 1. Clone the [api-docker](https://github.com/xbrowsersync/api-docker/) GitHub repo: @@ -45,22 +45,14 @@ If you do not already have a MongoDB instance or are intending to expose your xB $ git clone https://github.com/xbrowsersync/api-docker.git ``` - 2. Secure the [`acme.json`](https://github.com/xbrowsersync/api-docker/blob/master/acme.json) file as per traefik's requirements: + 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. - ``` - $ sudo chmod 600 acme.json - ``` - - 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. - - 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/). - - 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. + 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. - 6. Run the following command to start the containers: + 4. Run the following command to start the containers: ``` - $ sudo docker-compose up -d + $ docker-compose up -d ``` 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. diff --git a/acme.json b/acme.json deleted file mode 100644 index e69de29..0000000 diff --git a/docker-compose.yml b/docker-compose.yml index 8664bd4..8a8e45c 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,8 +1,5 @@ version: "3.7" -volumes: - xbs-db-data: - services: db: container_name: "xbs-db" @@ -10,12 +7,8 @@ services: - "MONGO_INITDB_DATABASE=xbrowsersync" - "MONGO_INITDB_ROOT_PASSWORD=$XBS_DB_PASSWORD" - "MONGO_INITDB_ROOT_USERNAME=$XBS_DB_USERNAME" - image: "mongo:4.2.6" - labels: - - "traefik.enable=false" - networks: - - "xbs-net" - restart: "always" + image: "mongo:4.4.4" + restart: "unless-stopped" volumes: - "xbs-db-data:/data/db" - "./mongoconfig.js:/docker-entrypoint-initdb.d/mongoconfig.js" @@ -26,33 +19,32 @@ services: environment: - "XBROWSERSYNC_DB_PWD=$XBS_DB_PASSWORD" - "XBROWSERSYNC_DB_USER=$XBS_DB_USERNAME" - image: "xbrowsersync/api:1.1.12" - labels: - - "traefik.frontend.rule=Host:$XBS_API_HOSTNAME" - - "traefik.port=8080" - networks: - - "xbs-net" - restart: "always" + healthcheck: + test: [ "CMD", "node", "/usr/src/api/healthcheck.js" ] + interval: "1m" + timeout: "10s" + retries: "5" + start_period: "30s" + image: "xbrowsersync/api:1.1.13" + restart: "unless-stopped" volumes: - "./settings.json:/usr/src/api/config/settings.json" + - "./healthcheck.js:/usr/src/api/healthcheck.js" reverse-proxy: - command: "--api --docker" + command: "caddy reverse-proxy --from $XBS_API_HOSTNAME --to api:8080" container_name: "xbs-reverse-proxy" depends_on: - "api" - image: "traefik:1.7.24-alpine" - labels: - - "traefik.enable=false" - networks: - - "xbs-net" + image: "caddy:2.3.0-alpine" ports: - "443:443" - "80:80" - restart: "always" + restart: "unless-stopped" volumes: - - "/var/run/docker.sock:/var/run/docker.sock" - - "./acme.json:/acme.json" - - "./traefik.toml:/traefik.toml" + - "xbs-caddy-config:/config" + - "xbs-caddy-data:/data" -networks: - xbs-net: \ No newline at end of file +volumes: + xbs-caddy-config: + xbs-caddy-data: + xbs-db-data: diff --git a/dockerfile b/dockerfile index 559ce61..10cd4b3 100644 --- a/dockerfile +++ b/dockerfile @@ -1,7 +1,7 @@ -FROM node:12.16.2-alpine +FROM node:14.16.0-alpine # Set environment variables -ENV XBROWSERSYNC_API_VERSION 1.1.12 +ENV XBROWSERSYNC_API_VERSION 1.1.13 WORKDIR /usr/src/api diff --git a/healthcheck.js b/healthcheck.js new file mode 100644 index 0000000..f8fafb9 --- /dev/null +++ b/healthcheck.js @@ -0,0 +1,44 @@ +const http = require('http'); + +const response = http.request( + { + host: '0.0.0.0', + method: 'GET', + path: '/info', + port: 8080, + timeout: 2000, + }, + (res) => { + let body = ''; + res.setEncoding('utf8'); + + res.on('data', (chunk) => { + body += chunk; + }); + + res.on('end', () => { + if (res.statusCode === 200) { + const payload = JSON.parse(body); + switch (payload.status) { + case 1: + case 3: + console.log('HEALTHCHECK: online'); + process.exit(0); + case 2: + default: + console.log('HEALTHCHECK: offline'); + } + } else { + console.log('HEALTHCHECK: offline'); + } + process.exit(1); + }); + } +); + +response.on('error', function (err) { + console.log('HEALTHCHECK: offline'); + process.exit(1); +}); + +response.end(); diff --git a/traefik.toml b/traefik.toml deleted file mode 100644 index de951b7..0000000 --- a/traefik.toml +++ /dev/null @@ -1,23 +0,0 @@ -defaultEntryPoints = ["http", "https"] - -[entryPoints] - [entryPoints.http] - address = ":80" - [entryPoints.http.redirect] - entryPoint = "https" - [entryPoints.https] - address = ":443" - [entryPoints.https.tls] - -[retry] - -[api] -dashboard = false - -[acme] -email = "you@yourdomain.org" -storage = "acme.json" -entryPoint = "https" -onHostRule = true - [acme.httpChallenge] - entryPoint = "http" \ No newline at end of file