bartonhammond / snowflake-hapi-openshift

:snowflake: A node.js Hapi server that runs locally and on OpenShift with MongoDB and Redis
MIT License
132 stars 38 forks source link

Snowflake Server Snowflake Server

This is a Hapi server, backed by MongoDb & Redis, running freely on Openshift for the React-Native mobile app Snowflake.

You can consider this a "starter server" or a "boilerplate" or maybe just an "example" of how these technologies work together. In any case, I hope there's something here you can learn and apply to your project!

Services Javascript noSQL
OpenShift hapi mongodb
jmeter swagger redis
blazeMeter nodeMailer mongoose

Join the chat at https://gitter.im/bartonhammond/snowflake License

Content

Usecase

Summary

OpenShift

OpenShift supports a free NodeJS setup that will scale with web traffic. This Snowflake Server setup will use MongoDB and Redis. Open Shift Dashboard

Some commands that you'll want to know about, once you've install the 'rhc' client:

You can check the performance of your application using the link

your-app-domain/haproxy-status/

The HAProxy Status Page HAProxy

Hapi

The nodeJS server uses Hapi. Hapi was developed and Open Sourced by Walmart Labs. It has been battle tested by Walmart, the largest retailer on earth. I chose it over Express 'cause Hapi is more targeted to API support and it looked interesting.

This server is documented here in its entirety.

Here's some flavor of what Hapi offers. Below is the declarative definition of the /account/login end point. The payload is validated here and shows how the username has a regex expression and is required. The same for the email. The config option has the tags, description, notes that document how the api is used. The handler is defined elsewhere. Separating the end point validation and declaration from the implementation cleans up the code.

  {
    method: 'POST',
    path: '/account/login',
    handler: AccountHandlers.loginUser,
    config: {
      // Include this API in swagger documentation
      tags: ['api'],
      description: 'A user can login',
      notes: 'The user login will return a sessionToken',
      validate: {
    payload: {
          //username required with same regex as client          
      username: Joi.string().regex(CONFIG.validation.username).required(),
          //password required with same regex as client
      password: Joi.string().regex(CONFIG.validation.password).required()
    }
      }
    }
  },

MongoDb

Mongodb will host our documents, namely User information, at this time. We'll be using Mongoose for interacting with Mongo within our code.

Once you're ssh'd into Openshift via rhc ssh -a mysnowflake, you can use the mongo shell.

Redis

Redis is fantastic for key,value pair access. We're using it here for "Black Listing Json Web Tokens". You can read about this concept here https://auth0.com/blog/2015/03/10/blacklist-json-web-token-api-keys/

Swagger

Swagger provides the api documentation - simply augmenting the endpoints generates a page showing all the API access points.

Shown below is the generated API documentation -

Swagger docs

NOTE: you can test the APIs directly from the browser with the forms that Swagger provides!

Swagger form

Jason Web Token

JWT is used in the Authentication as a Session Token. You can read the docs here showing how it's setup.

JMeter

Using JMeter allowed me to performance test the API. I created a test suite with JMeter as shown below and debugged the script by running locally. Once I was satisfied, I changed the HTTP Request Defaults and uploaded to BlazeMeter for testing.

Shown below is the script defined in JMeter

JMeter setup

BlazeMeter

BlazeMeter was used to perform the tests as it is much better equipped to host the threads then my personal mac.

<img src="https://i.ytimg.com/s_vi/HKDw5po4TYM/1.jpg?sqp=CKyjprQF&rs=AOn4CLDM4rBr05-tstNYVcwhO09V1WXdNA&time=1451856598959" alt="Running BlazeMeter" width="240" height="180" border="10" />

The following screens show the results of running 50 concurrent users performing the following actions with a 1 second delay between each action:

Original Test Configuration Original Test Configuration

Overview Overview

Timeline Report Timeline Report

Load Report Load Report

Aggregate Report Aggregate Report

Monitoring Report Monitoring Report


Setup

Below are the instructions for setting up the server on your local machine. These instructions work fine on the Mac - no promise is made for other OSs.

You may need to "Allow Less Secure Apps" in your gmail account (it's all the way at the bottom). You also may need to "Allow access to your Google account"

Locally (one time only setup)


cd redis-2.8.24
make
cd src/
./redis-server 
npm start

OpenShift

Watch the following video to see all the steps to install the Hapi nodes server to Openshift in action:

<img src="https://i.ytimg.com/s_vi/Js4Kvd9gG6E/2.jpg?sqp=CMzbprQF&rs=AOn4CLDZzm3zHrS-YdAJCi4a-yHXv73NyQ" alt="Snowflake Hot Loading" width="240" height="180" border="10" />


rhc domain create <yournamespace>
rhc app-create mysnowflake  nodejs-0.10 mongodb-2.4 -s
rhc add-cartridge \
http://cartreflect-claytondev.rhcloud.com/reflect?github=transformatordesign/openshift-redis-cart \
-a mysnowflake
cd mysnowflake
git remote add upstream -m master git://github.com/bartonhammond/snowflake-hapi-openshift.git
git pull -s recursive -X theirs upstream master
cp src/config.sample.js src/config.js
git push origin master