Skip to content

Commit

Permalink
Updated Dockerfile and docker-compose.yml, added mongodb service
Browse files Browse the repository at this point in the history
  • Loading branch information
arpitkhandelwal1984 committed Sep 12, 2018
1 parent c069744 commit 13a2494
Show file tree
Hide file tree
Showing 13 changed files with 4,907 additions and 4,228 deletions.
5 changes: 5 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
node_modules
.env
npm-debug.log
db/
db/*
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,6 @@ node_modules/
.build
.env
.vscode

# DB
db/data/*
12 changes: 8 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
FROM node:8.10.0
FROM node:alpine

WORKDIR /src

RUN npm install -g gulp
COPY package*.json ./

RUN npm install
RUN npm install --only=production

COPY . /src
COPY . .

EXPOSE 8000

CMD [ "npm", "start" ]
135 changes: 25 additions & 110 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ However, for production the credentials should be set as environment variables.
│   │   ├── js // User defined scripts
│   │   └── vendor // Vendor scripts.
│   └── styles // All SASS stylesheets
├──config // Contains all app configurations
├── config // Contains all app configurations
│   ├── assets.js // Assets configuration file
│   ├── config.js // Application configuration.
│   ├── default.json // Configuration file.
Expand All @@ -79,15 +79,15 @@ However, for production the credentials should be set as environment variables.
├── lib // Core application lib/plugins
├── logs // Contains app log file
└── tasks // Contains all gulp tasks
├──.gitignore // standard git ignore file
├──.babelrc // Babel config
├──.eslintrc // Define eslint rules.
├──.eslintignore // Ignores certain files for eslint rules
├──Dockerfile // Standard doceker file
├──docker-compose.yml // Standard docker compose file
├──gulpfile.js // Gulp entry file
├──server.js // Contains all app configurations
├──.env // dotenv configuration file for environment variable
├── .gitignore // standard git ignore file
├── .babelrc // Babel config
├── .eslintrc // Define eslint rules.
├── .eslintignore // Ignores certain files for eslint rules
├── Dockerfile // Standard doceker file
├── docker-compose.yml // Standard docker compose file
├── gulpfile.js // Gulp entry file
├── server.js // Contains all app configurations
├── .env // dotenv configuration file for environment variable
└── test
├── testcases // Testcases organised by module names.
└── test.js // Test file.
Expand All @@ -103,7 +103,7 @@ To simulate environment variables in Dev environment, please create .env file at

```
DEBUGGER=false // Enable/disable debug mode
NODE_ENV=development // Node environment development/production
NODE_ENV=production // Node environment development/production
PORT=8000 // Server Port
SERVER_HOST=0.0.0.0 // Hapi Server host
COOKIE_SECRET=This_Should_be_32_characters_long
Expand All @@ -115,22 +115,27 @@ GMAIL_SENDEREMAIL=Display email id for sender // Could be same or differen
GMAIL_SENDERNAME=Sender's name to display in email
MAIL_HOST=smtp.gmail.com // Mail host
MAIL_PORT=465 // Mail Port
DATABASE_URL=mongodb://localhost:27017/hapiness // Mongo database url
SWAGGER_HOST=localhost:8000 // Host Url for Swagger
DATABASE_URL=mongodb://localhost:27017/hapiness // Mongo database url
#DATABASE_URL=mongodb://mongodb:27017/hapiness // Mongo database url while using docker
```
Please make sure you remove any _.env_ file present at the root of this project before you invoke docker-compose or else docker will pick up the enviroment variables from this file and may lead to inconsistent results.

## Running the server locally

- Install `node`, `npm`
- Define env configuration
- Run these commands
- Define env variables
- Run the following commands

```sh
# Install deps
$ npm install

# Run the node server
# Run the node server in dev mode
$ gulp dev

# Run the node server in production mode
$ gulp

```
Expand All @@ -146,8 +151,10 @@ Steps to run app in docker container :
3. Start the server in daemon thread using cmd: $ docker-compose up -d
4. Stop the server using cmd : $ docker-compose down

In case you are willing to use this project as is (i.e. without external mongodb installation), the docker-compose provided with this project should suffice. It brings along a _mongodb_ service which stores the data in the _/db/data_ directory. But in case you wish to use your existing MongoDB installation then please remove the _mongodb_ service from the docker-compose.yml file and correct the database_url environment variable.

## REST API Versioning
Hapiness now supports versioning out of the box. For sample purposes, v1 and v2 folders with appropriate route handlers are shared with this boilerplate. The versioning is done at the router's level only. Services are still placed at a single place with no support for versioning.
Hapiness now supports versioning out of the box. For sample purposes, v1 and v2 folders with appropriate route handlers are shared with this boilerplate. The versioning is done at the router's level only. Services are still placed at a single place with no support for versioning.

## Testing
Hapiness now supports writting unit test cases using 'Mocha' and 'Supertest' node packages.
Expand All @@ -158,100 +165,8 @@ Hapiness now supports writting unit test cases using 'Mocha' and 'Supertest' nod
$ npm test

```
## Hapiness Upgrade Guide.
We have upgraded from Hapi v16.x to LTS v17.2.3

The new Hapi.js version brought along a series of breaking changes from its predecessor. Summarising all of those is out of scope of this guide, however we have placed the link of the document we referred while doing this upgrade at the bootom of this readme. Following are the major changes we faced and thought are worth sharing with our followers -

#Creating plugins

To comply with the new structure, update your plugins to use a named export plugin that provides an object containing all the information. Please note that the register function now takes only the server and options object (got rid of next callback).

#hapi v16

```
exports.register = (server, options, next) => { … }
exports.register.attributes = {
pkg: require('../package.json')
};
```

#hapi v17

```
exports.plugin = {
register: (server, options) => {
},
pkg: require('../package.json')
}
```

#No More 'reply()' callbacks

With hapi v17.x you can return values from route handlers directly, the reply interface isn’t available anymore.

#hapi v16

```
server.ext('onPreResponse', (request, reply) => { … })
const handler = (request, reply) => {
// return a string
return reply('ok')
// return an object and hapi creates JSON out of it
return reply({ name: 'Future Studio', makeItRock: true })
// redirect … to 404 … hehehehe :D
return reply.redirect('/404')
// return a view
return reply.view('index', { name: 'Future Studio' })
// use the "reply" to create a response with chained methods
return reply(someHTML)
.type('text/html')
.header('X-Custom', 'my-value')
.code(201)
}
```

#hapi v17
```
// the new structure applies to lifecycle points and request handlers
// here are some examples on how to use the new response toolkit
server.ext('onPreResponse', (request, h) => { … })
const handler = (request, h) => {
// return a string
return 'ok'
// return an object and hapi creates JSON out of it
return { name: 'Future Studio', makeItRock: true }
// redirect … to 404
return h.redirect('/404')
// return a view
return h.view('index', { name: 'Future Studio' })
// use the "h" response toolkit to create a response
return h
.response(someHTML)
.type('text/html')
.header('X-Custom', 'my-value')
.code(201)
}
```

For complete upgrade guide to upgrade from hapi 16.x to hapi 17.x please refer the link below:

https://futurestud.io/tutorials/hapi-v17-upgrade-guide-your-move-to-async-await
## Upgrade to Hapi v17.x
The upgrade guid from earlier versions to v17.x can be found [here](https://github.com/sytango-technologies/Hapiness/blob/master/upgrade/Readme.md)

## Contributors
[Arpit Khandelwal](https://www.linkedin.com/in/arpitkhandelwal1984/)
Expand Down
5 changes: 2 additions & 3 deletions config/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ let internals = {
}
};


// Confidence document object
internals.config = {
$meta: 'App configuration file',
Expand Down Expand Up @@ -42,7 +41,7 @@ internals.config = {
uri:'mongodb://localhost:27017/hapinessTest'
},
$default: {
uri: process.env.DATABASE_URL ,
uri: process.env.DATABASE_URL,
options: {}
}
},
Expand Down Expand Up @@ -71,7 +70,7 @@ internals.config = {
},
good: {
ops: {
interval: 1000
interval: 1000
},
reporters: {
myConsoleReporter: [{
Expand Down
2 changes: 1 addition & 1 deletion config/manifest.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ internals.manifest = {
$meta: 'App manifest document',
server: {
host : process.env.SERVER_HOST,
port: 8000
port: process.env.PORT
},
register: {
plugins : [
Expand Down
Empty file added db/.gitkeep
Empty file.
50 changes: 39 additions & 11 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,39 @@
web:
build: .
volumes:
- ./:/app
ports:
- "8000:8000"
# links:
# - db
command: cd /src
command: gulp

version: '2.1'
services:
mongodb:
image: mongo:latest
restart: always
container_name: "mongodb"
environment:
- MONGO_DATA_DIR=/db/data
- MONGO_LOG_DIR=/dev/null
volumes:
- ./db/data:/data/db
ports:
- 27017:27017
command: mongod --bind_ip_all --smallfiles --logpath=/dev/null # --quiet

web:
build: .
links:
- mongodb
container_name: hapiness
restart: always
ports:
- "8000:8000"
environment:
- NODE_ENV=production
- PORT=${PORT}
- DATABASE_URL=${DATABASE_URL}
- GMAIL_ID=${GMAIL_ID}
- GMAIL_PASSWORD=${GMAIL_PASSWORD}
- GMAIL_SENDEREMAIL=${GMAIL_SENDEREMAIL}
- GMAIL_SENDERNAME=${GMAIL_SENDERNAME}
- MAIL_HOST=${MAIL_HOST}
- MAIL_PORT=${MAIL_PORT}
- SWAGGER_HOST=${SWAGGER_HOST}
- JWT_SECRET=${JWT_SECRET}
- COOKIE_SECRET=${COOKIE_SECRET}
- YAR_COOKIE_SECRET=${YAR_COOKIE_SECRET}
depends_on:
- mongodb
12 changes: 5 additions & 7 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,14 @@

const Gulp = require('gulp');
const RequireDir = require('require-dir');

// Load tasks
RequireDir('./tasks');
RequireDir('./tasks'); // Load tasks


// Build task definitions
Gulp.task('prod-build', ['fonts', 'images', 'misc', 'styles', 'webpack:build', 'lint']);
Gulp.task('dev-build', ['fonts', 'images', 'misc', 'styles', 'webpack:dev-build', 'lint']);

Gulp.task('dev', ['dotenv','dev-build', 'watch', 'nodemon']);
Gulp.task('prod', ['dotenv','prod-build','pm2']); // 'dotenv' should be removed from prod task in a production environment.

Gulp.task('default', ['dev']);
Gulp.task('prod-build', ['fonts','images', 'misc', 'styles', 'webpack:build']);
Gulp.task('prod', ['prod-build', 'forever']);

Gulp.task('default', ['prod']);
Loading

0 comments on commit 13a2494

Please sign in to comment.