From ff11452cf0a976c5798343d38433daf09c732f56 Mon Sep 17 00:00:00 2001 From: Chris Troutner Date: Thu, 7 Feb 2019 17:22:57 -0800 Subject: [PATCH 1/9] Switching env config files around a bit --- config/env/common.js | 7 ++++--- config/env/development.js | 10 ++++------ config/env/production.js | 13 +++++++------ config/env/test.js | 10 ++++------ config/index.js | 2 +- package.json | 2 +- 6 files changed, 21 insertions(+), 23 deletions(-) diff --git a/config/env/common.js b/config/env/common.js index 4a03e2d..f1d3ee7 100644 --- a/config/env/common.js +++ b/config/env/common.js @@ -1,6 +1,7 @@ -// export default { -// port: process.env.PORT || 5000 -// } +/* + This file is used to store unsecure, application-specific data common to all + environments. +*/ module.exports = { port: process.env.PORT || 5000 diff --git a/config/env/development.js b/config/env/development.js index 47f1615..35d1f4d 100644 --- a/config/env/development.js +++ b/config/env/development.js @@ -1,13 +1,11 @@ /* -export default { - session: 'secret-boilerplate-token', - token: 'secret-jwt-token', - database: 'mongodb://localhost:27017/p2pvps-server-dev' -} + These are the environment settings for the DEVELOPMENT environment. + This is the environment run by default with `npm start` if KOA_ENV is not + specified. */ module.exports = { session: 'secret-boilerplate-token', token: 'secret-jwt-token', - database: 'mongodb://localhost:27017/p2pvps-server-dev' + database: 'mongodb://localhost:27017/koa-server-dev' } diff --git a/config/env/production.js b/config/env/production.js index a68e550..33e08d0 100644 --- a/config/env/production.js +++ b/config/env/production.js @@ -1,13 +1,14 @@ /* -export default { - session: 'secret-boilerplate-token', - token: 'secret-jwt-token', - database: 'mongodb://localhost:27017/p2pvps-server-prod' -} + These are the environment settings for the PRODUCTION environment. + This is the environment run with `npm start` if KOA_ENV=production. + This is the environment run inside the Docker container. + + It is assumed the MonogDB Docker container is accessed by port 5555 + so as not to conflict with the default host port of 27017 for MongoDB. */ module.exports = { session: 'secret-boilerplate-token', token: 'secret-jwt-token', - database: 'mongodb://localhost:27017/p2pvps-server-prod' + database: 'mongodb://localhost:5555/koa-server-prod' } diff --git a/config/env/test.js b/config/env/test.js index 9cc99f1..e71cc39 100644 --- a/config/env/test.js +++ b/config/env/test.js @@ -1,13 +1,11 @@ /* -export default { - session: 'secret-boilerplate-token', - token: 'secret-jwt-token', - database: 'mongodb://localhost:27017/p2pvps-server-test' -} + These are the environment settings for the TEST environment. + This is the environment run with `npm start` if KOA_ENV=test. + This is the environment run by the test suite. */ module.exports = { session: 'secret-boilerplate-token', token: 'secret-jwt-token', - database: 'mongodb://localhost:27017/p2pvps-server-test' + database: 'mongodb://localhost:27017/koa-server-test' } diff --git a/config/index.js b/config/index.js index db7fcb9..0596847 100644 --- a/config/index.js +++ b/config/index.js @@ -1,6 +1,6 @@ const common = require('./env/common') -const env = process.env.NODE_ENV || 'development' +const env = process.env.KOA_ENV || 'development' const config = require(`./env/${env}`) module.exports = Object.assign({}, common, config) diff --git a/package.json b/package.json index 6d59d26..45b46e4 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,7 @@ "main": "index.js", "scripts": { "start": "node index.js", - "test": "NODE_ENV=test nyc --reporter=text ./node_modules/.bin/mocha --exit", + "test": "KOA_ENV=test nyc --reporter=text ./node_modules/.bin/mocha --exit", "lint": "eslint src/**/*.js", "docs": "./node_modules/.bin/apidoc -i src/ -o docs", "coverage": "nyc report --reporter=text-lcov | coveralls", From 3c631cbece20d885f2bc9829e5737d651cd8aa9e Mon Sep 17 00:00:00 2001 From: Chris Troutner Date: Thu, 7 Feb 2019 17:25:46 -0800 Subject: [PATCH 2/9] Adding docker files --- docker-compose.yml | 32 +++++++++++++++++++++ production/Dockerfile | 57 +++++++++++++++++++++++++++++++++++++ production/start-production | 3 ++ 3 files changed, 92 insertions(+) create mode 100644 docker-compose.yml create mode 100644 production/Dockerfile create mode 100755 production/start-production diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..c1dda93 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,32 @@ +# Start the testnet server with the command 'docker-compose up -d' + +koa-mongodb: + image: mongo + ports: + - "27017:27017" # : + volumes: + - ./database:/data/db + command: mongod --smallfiles --logpath=/dev/null # -- quiet + restart: always + +koa: + build: ./koa-boilerplate/ + dockerfile: Dockerfile + links: + - koa-mongodb + ports: + - "3000:3000" # : + volumes: +# - ./logs:/home/coinjoin/consolidating-coinjoin/logs + - ./keys:/home/safeuser/keys + + restart: always + +tor: + image: goldy/tor-hidden-service + links: + - koa + environment: + PORT_MAP: 80 # Map port to detected service + volumes: + - ./keys:/var/lib/tor/hidden_service/ diff --git a/production/Dockerfile b/production/Dockerfile new file mode 100644 index 0000000..6592d03 --- /dev/null +++ b/production/Dockerfile @@ -0,0 +1,57 @@ +# Create a Dockerized API server +# + +#IMAGE BUILD COMMANDS +# ct-base-ubuntu = ubuntu 18.04 + nodejs +FROM christroutner/ct-base-ubuntu +MAINTAINER Chris Troutner + +#Create the user 'safeuser' and add them to the sudo group. +#RUN useradd -ms /bin/bash safeuser +#RUN adduser safeuser sudo + +#Set password to 'password' change value below if you want a different password +#RUN echo safeuser:password | chpasswd + +#Set the working directory to be the home directory +WORKDIR /home/safeuser + +#Setup NPM for non-root global install +#RUN mkdir /home/safeuser/.npm-global +#RUN chown -R safeuser .npm-global +#RUN echo "export PATH=~/.npm-global/bin:$PATH" >> /home/safeuser/.profile +#RUN runuser -l safeuser -c "npm config set prefix '~/.npm-global'" + +# Expose the port the API will be served on. +EXPOSE 5000 + +# Switch to user account. +USER safeuser +# Prep 'sudo' commands. +RUN echo 'abcd8765' | sudo -S pwd + +# Clone the rest.bitcoin.com repository +WORKDIR /home/safeuser +RUN git clone https://github.com/christroutner/babel-free-koa2-api-boilerplate +RUN mv babel-free-koa2-api-boilerplate koa +RUN mkdir keys + +# Switch to the desired branch. `master` is usually stable, +# and `stage` has the most up-to-date changes. +WORKDIR /home/safeuser/koa + +# For development: switch to unstable branch +RUN git checkout docker + +# Install dependencies +RUN npm install + +VOLUME /home/safeuser/keys + +EXPOSE 3000 + +# Start the application. +COPY start-production start-production +CMD ["./start-production"] + +#CMD ["npm", "start"] diff --git a/production/start-production b/production/start-production new file mode 100755 index 0000000..155efee --- /dev/null +++ b/production/start-production @@ -0,0 +1,3 @@ +#!/bin/bash +export KOA_ENV=production +npm start From f5c1ccf800fcc58c528ae0fe6cd7982b7d0d7e60 Mon Sep 17 00:00:00 2001 From: Chris Troutner Date: Thu, 7 Feb 2019 17:26:37 -0800 Subject: [PATCH 3/9] Adding database files --- database/README.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 database/README.md diff --git a/database/README.md b/database/README.md new file mode 100644 index 0000000..16897c3 --- /dev/null +++ b/database/README.md @@ -0,0 +1 @@ +This is where the mongodb docker container stores its db files. From 1fddca1a41fc4ce9ede4c757536b556d63f11b0d Mon Sep 17 00:00:00 2001 From: Chris Troutner Date: Thu, 7 Feb 2019 17:29:19 -0800 Subject: [PATCH 4/9] Updating gitignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index c8cf125..35cb51d 100644 --- a/.gitignore +++ b/.gitignore @@ -59,3 +59,4 @@ docs .nyc_output coverage +database/ From 3c59913a41cac4ae2832a8e9449dc58841f860dd Mon Sep 17 00:00:00 2001 From: Chris Troutner Date: Thu, 7 Feb 2019 17:40:14 -0800 Subject: [PATCH 5/9] Updating production config to connect to dockerized mongodb --- config/env/production.js | 2 +- docker-compose.yml | 13 ++----------- production/Dockerfile | 4 ++-- 3 files changed, 5 insertions(+), 14 deletions(-) diff --git a/config/env/production.js b/config/env/production.js index 33e08d0..1cdd141 100644 --- a/config/env/production.js +++ b/config/env/production.js @@ -10,5 +10,5 @@ module.exports = { session: 'secret-boilerplate-token', token: 'secret-jwt-token', - database: 'mongodb://localhost:5555/koa-server-prod' + database: 'mongodb://172.17.0.1:5555/koa-server-prod' } diff --git a/docker-compose.yml b/docker-compose.yml index c1dda93..26ec376 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -3,14 +3,14 @@ koa-mongodb: image: mongo ports: - - "27017:27017" # : + - "5555:27017" # : volumes: - ./database:/data/db command: mongod --smallfiles --logpath=/dev/null # -- quiet restart: always koa: - build: ./koa-boilerplate/ + build: ./production/ dockerfile: Dockerfile links: - koa-mongodb @@ -21,12 +21,3 @@ koa: - ./keys:/home/safeuser/keys restart: always - -tor: - image: goldy/tor-hidden-service - links: - - koa - environment: - PORT_MAP: 80 # Map port to detected service - volumes: - - ./keys:/var/lib/tor/hidden_service/ diff --git a/production/Dockerfile b/production/Dockerfile index 6592d03..f6adedf 100644 --- a/production/Dockerfile +++ b/production/Dockerfile @@ -32,8 +32,8 @@ RUN echo 'abcd8765' | sudo -S pwd # Clone the rest.bitcoin.com repository WORKDIR /home/safeuser -RUN git clone https://github.com/christroutner/babel-free-koa2-api-boilerplate -RUN mv babel-free-koa2-api-boilerplate koa +RUN git clone https://github.com/christroutner/koa-api-boilerplate +RUN mv koa-api-boilerplate koa RUN mkdir keys # Switch to the desired branch. `master` is usually stable, From 92d0339212035012f8c81d0c0b52aab963916224 Mon Sep 17 00:00:00 2001 From: Chris Troutner Date: Fri, 8 Feb 2019 15:45:30 -0800 Subject: [PATCH 6/9] fix(cors): Turning CORS *on* by default --- bin/server.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/bin/server.js b/bin/server.js index 95244b3..5e92a80 100644 --- a/bin/server.js +++ b/bin/server.js @@ -42,7 +42,8 @@ async function startServer () { modules(app) // Enable CORS for testing - // app.use(cors({origin: '*'})) + // THIS IS A SECURITY RISK. COMMENT OUT FOR PRODUCTION + app.use(cors({ origin: '*' })) // MIDDLEWARE END From 589580798ce7f35fb0402d5f1dffd3845f37b61d Mon Sep 17 00:00:00 2001 From: Chris Troutner Date: Fri, 8 Feb 2019 15:48:20 -0800 Subject: [PATCH 7/9] Moving docker port to 5000 --- docker-compose.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docker-compose.yml b/docker-compose.yml index 26ec376..3d9551a 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -15,7 +15,7 @@ koa: links: - koa-mongodb ports: - - "3000:3000" # : + - "5000:5000" # : volumes: # - ./logs:/home/coinjoin/consolidating-coinjoin/logs - ./keys:/home/safeuser/keys From 7afe3c8f0cf3cea6f93eff7c4b0ea592b28b0776 Mon Sep 17 00:00:00 2001 From: Chris Troutner Date: Fri, 8 Feb 2019 16:44:53 -0800 Subject: [PATCH 8/9] feat(docker): Production builds to a Docker container --- README.md | 13 +++++++++---- production/Dockerfile | 2 +- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index a781c62..fa5a0c8 100644 --- a/README.md +++ b/README.md @@ -22,23 +22,26 @@ It makes the following modifications: - Configured for Travis CI (continuous integration), Coveralls (code coverage), GreenKeeper (automated dependency management), and Semantic Release (automated versioning). +- 'Production' environment is targeted for packaging as a Docker container. + ## Features This project covers basic necessities of most APIs. * Authentication (passport & jwt) * Database (mongoose) * Testing (mocha) * Doc generation with apidoc -* linting using standard +* Linting using standard +* Packaged as a Docker container ## Requirements -* node __^8.9.4__ -* npm __^5.7.1__ +* node __^10.15.1__ +* npm __^6.7.0__ ## Installation ```bash -git clone https://github.com/christroutner/babel-free-koa2-api-boilerplate.git +git clone https://github.com/christroutner/koa-api-boilerplate/tree/docker ``` ## Structure @@ -68,6 +71,8 @@ git clone https://github.com/christroutner/babel-free-koa2-api-boilerplate.git * `npm run dev` Start server on dev mode with nodemon * `npm run docs` Generate API documentation * `npm test` Run mocha tests +* `docker-compose build` Build a 'production' Docker container +* `docker-compose up` Run the docker container ## Documentation API documentation is written inline and generated by [apidoc](http://apidocjs.com/). diff --git a/production/Dockerfile b/production/Dockerfile index f6adedf..bd7e922 100644 --- a/production/Dockerfile +++ b/production/Dockerfile @@ -2,7 +2,7 @@ # #IMAGE BUILD COMMANDS -# ct-base-ubuntu = ubuntu 18.04 + nodejs +# ct-base-ubuntu = ubuntu 18.04 + nodejs v10 LTS FROM christroutner/ct-base-ubuntu MAINTAINER Chris Troutner From 5053c6a2ad1919c7650a5d43b8ee0a3f5f259e56 Mon Sep 17 00:00:00 2001 From: Chris Troutner Date: Fri, 8 Feb 2019 16:47:06 -0800 Subject: [PATCH 9/9] Updating readme --- README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README.md b/README.md index fa5a0c8..afe4f1c 100644 --- a/README.md +++ b/README.md @@ -56,6 +56,9 @@ git clone https://github.com/christroutner/koa-api-boilerplate/tree/docker │ │ └── test.js │ ├── index.js # Config entrypoint - exports config according to envionrment and commons │ └── passport.js # Passportjs config of strategies +| +├── production # Dockerfile for build production container +| ├── src # Source code │ ├── modules │ │ ├── controller.js # Module-specific controllers