Merge pull request #13 from christroutner/docker

Docker
This commit is contained in:
Chris Troutner
2019-02-08 16:54:55 -08:00
committed by GitHub
13 changed files with 120 additions and 28 deletions
+1
View File
@@ -59,3 +59,4 @@ docs
.nyc_output .nyc_output
coverage coverage
database/
+12 -4
View File
@@ -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). - 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 ## Features
This project covers basic necessities of most APIs. This project covers basic necessities of most APIs.
* Authentication (passport & jwt) * Authentication (passport & jwt)
* Database (mongoose) * Database (mongoose)
* Testing (mocha) * Testing (mocha)
* Doc generation with apidoc * Doc generation with apidoc
* linting using standard * Linting using standard
* Packaged as a Docker container
## Requirements ## Requirements
* node __^8.9.4__ * node __^10.15.1__
* npm __^5.7.1__ * npm __^6.7.0__
## Installation ## Installation
```bash ```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 ## Structure
@@ -53,6 +56,9 @@ git clone https://github.com/christroutner/babel-free-koa2-api-boilerplate.git
│ │ └── test.js │ │ └── test.js
│ ├── index.js # Config entrypoint - exports config according to envionrment and commons │ ├── index.js # Config entrypoint - exports config according to envionrment and commons
│ └── passport.js # Passportjs config of strategies │ └── passport.js # Passportjs config of strategies
|
├── production # Dockerfile for build production container
|
├── src # Source code ├── src # Source code
│ ├── modules │ ├── modules
│ │ ├── controller.js # Module-specific controllers │ │ ├── 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 dev` Start server on dev mode with nodemon
* `npm run docs` Generate API documentation * `npm run docs` Generate API documentation
* `npm test` Run mocha tests * `npm test` Run mocha tests
* `docker-compose build` Build a 'production' Docker container
* `docker-compose up` Run the docker container
## Documentation ## Documentation
API documentation is written inline and generated by [apidoc](http://apidocjs.com/). API documentation is written inline and generated by [apidoc](http://apidocjs.com/).
+2 -1
View File
@@ -42,7 +42,8 @@ async function startServer () {
modules(app) modules(app)
// Enable CORS for testing // Enable CORS for testing
// app.use(cors({origin: '*'})) // THIS IS A SECURITY RISK. COMMENT OUT FOR PRODUCTION
app.use(cors({ origin: '*' }))
// MIDDLEWARE END // MIDDLEWARE END
+4 -3
View File
@@ -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 = { module.exports = {
port: process.env.PORT || 5000 port: process.env.PORT || 5000
+4 -6
View File
@@ -1,13 +1,11 @@
/* /*
export default { These are the environment settings for the DEVELOPMENT environment.
session: 'secret-boilerplate-token', This is the environment run by default with `npm start` if KOA_ENV is not
token: 'secret-jwt-token', specified.
database: 'mongodb://localhost:27017/p2pvps-server-dev'
}
*/ */
module.exports = { module.exports = {
session: 'secret-boilerplate-token', session: 'secret-boilerplate-token',
token: 'secret-jwt-token', token: 'secret-jwt-token',
database: 'mongodb://localhost:27017/p2pvps-server-dev' database: 'mongodb://localhost:27017/koa-server-dev'
} }
+7 -6
View File
@@ -1,13 +1,14 @@
/* /*
export default { These are the environment settings for the PRODUCTION environment.
session: 'secret-boilerplate-token', This is the environment run with `npm start` if KOA_ENV=production.
token: 'secret-jwt-token', This is the environment run inside the Docker container.
database: 'mongodb://localhost:27017/p2pvps-server-prod'
} 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 = { module.exports = {
session: 'secret-boilerplate-token', session: 'secret-boilerplate-token',
token: 'secret-jwt-token', token: 'secret-jwt-token',
database: 'mongodb://localhost:27017/p2pvps-server-prod' database: 'mongodb://172.17.0.1:5555/koa-server-prod'
} }
+4 -6
View File
@@ -1,13 +1,11 @@
/* /*
export default { These are the environment settings for the TEST environment.
session: 'secret-boilerplate-token', This is the environment run with `npm start` if KOA_ENV=test.
token: 'secret-jwt-token', This is the environment run by the test suite.
database: 'mongodb://localhost:27017/p2pvps-server-test'
}
*/ */
module.exports = { module.exports = {
session: 'secret-boilerplate-token', session: 'secret-boilerplate-token',
token: 'secret-jwt-token', token: 'secret-jwt-token',
database: 'mongodb://localhost:27017/p2pvps-server-test' database: 'mongodb://localhost:27017/koa-server-test'
} }
+1 -1
View File
@@ -1,6 +1,6 @@
const common = require('./env/common') const common = require('./env/common')
const env = process.env.NODE_ENV || 'development' const env = process.env.KOA_ENV || 'development'
const config = require(`./env/${env}`) const config = require(`./env/${env}`)
module.exports = Object.assign({}, common, config) module.exports = Object.assign({}, common, config)
+1
View File
@@ -0,0 +1 @@
This is where the mongodb docker container stores its db files.
+23
View File
@@ -0,0 +1,23 @@
# Start the testnet server with the command 'docker-compose up -d'
koa-mongodb:
image: mongo
ports:
- "5555:27017" # <host port>:<container port>
volumes:
- ./database:/data/db
command: mongod --smallfiles --logpath=/dev/null # -- quiet
restart: always
koa:
build: ./production/
dockerfile: Dockerfile
links:
- koa-mongodb
ports:
- "5000:5000" # <host port>:<container port>
volumes:
# - ./logs:/home/coinjoin/consolidating-coinjoin/logs
- ./keys:/home/safeuser/keys
restart: always
+1 -1
View File
@@ -5,7 +5,7 @@
"main": "index.js", "main": "index.js",
"scripts": { "scripts": {
"start": "node index.js", "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", "lint": "eslint src/**/*.js",
"docs": "./node_modules/.bin/apidoc -i src/ -o docs", "docs": "./node_modules/.bin/apidoc -i src/ -o docs",
"coverage": "nyc report --reporter=text-lcov | coveralls", "coverage": "nyc report --reporter=text-lcov | coveralls",
+57
View File
@@ -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 <chris.troutner@gmail.com>
#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"]
+3
View File
@@ -0,0 +1,3 @@
#!/bin/bash
export KOA_ENV=production
npm start