diff --git a/.gitignore b/.gitignore index c8cf125..35cb51d 100644 --- a/.gitignore +++ b/.gitignore @@ -59,3 +59,4 @@ docs .nyc_output coverage +database/ diff --git a/README.md b/README.md index a781c62..afe4f1c 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 @@ -53,6 +56,9 @@ git clone https://github.com/christroutner/babel-free-koa2-api-boilerplate.git │ │ └── 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 @@ -68,6 +74,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/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 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..1cdd141 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://172.17.0.1: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/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. diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..3d9551a --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,23 @@ +# Start the testnet server with the command 'docker-compose up -d' + +koa-mongodb: + image: mongo + ports: + - "5555:27017" # : + volumes: + - ./database:/data/db + command: mongod --smallfiles --logpath=/dev/null # -- quiet + restart: always + +koa: + build: ./production/ + dockerfile: Dockerfile + links: + - koa-mongodb + ports: + - "5000:5000" # : + volumes: +# - ./logs:/home/coinjoin/consolidating-coinjoin/logs + - ./keys:/home/safeuser/keys + + restart: always 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", diff --git a/production/Dockerfile b/production/Dockerfile new file mode 100644 index 0000000..bd7e922 --- /dev/null +++ b/production/Dockerfile @@ -0,0 +1,57 @@ +# Create a Dockerized API server +# + +#IMAGE BUILD COMMANDS +# ct-base-ubuntu = ubuntu 18.04 + nodejs v10 LTS +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/koa-api-boilerplate +RUN mv koa-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