Home

Awesome

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!

ServicesJavascriptnoSQL
<img alt="OpenShift" src="https://cloud.githubusercontent.com/assets/1282364/12080283/a0c8c4cc-b21b-11e5-8a70-e62481fc9ea6.png" width="100px"/><img alt="hapi" src="https://cloud.githubusercontent.com/assets/1282364/12080288/b588af62-b21b-11e5-822c-bbb8506796eb.png" width="100px"><img alt="mongodb" src="https://cloud.githubusercontent.com/assets/1282364/12080294/e09dcd22-b21b-11e5-8a71-49c44f7d33d3.png" width="100px"/>
<img alt="jmeter" src="https://cloud.githubusercontent.com/assets/1282364/12080302/2f8719d4-b21c-11e5-9298-5b002bfd4134.png" width="100px"/><img alt="swagger" src="https://cloud.githubusercontent.com/assets/1282364/12080584/e7cd2fda-b224-11e5-9ca4-6ecd236ce30f.png" width="100px"/><img alt="redis" src="https://cloud.githubusercontent.com/assets/1282364/12080296/f130324c-b21b-11e5-8866-5a5adbdb5391.png" width="100px"/>
<img alt="blazeMeter" src="https://cloud.githubusercontent.com/assets/1282364/12080516/52df66c4-b222-11e5-99a0-454295f6eabb.png" width="100px"/><img alt="nodeMailer" src="https://cloud.githubusercontent.com/assets/1282364/12080598/457da8c6-b225-11e5-85a7-1710b9ae898e.png" width="100px"/><img alt="mongoose" src="https://cloud.githubusercontent.com/assets/1282364/12080621/b85fa1fa-b225-11e5-8c96-65029d50033d.png" width="100px"/>

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.

<a href="http://www.youtube.com/watch?feature=player_embedded&v=HKDw5po4TYM" target="_blank"><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" /></a>

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 
  hapi: {
    port: 5000,
    ip: '127.0.0.1'
  }

  HAPI: {
    local: {
      url: 'http://127.0.0.1:5000'
    },
    remote: {
      url: 'enter your remote url here'
    }
  }  
npm start

OpenShift

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

<a href="http://www.youtube.com/watch?feature=player_embedded&v=Js4Kvd9gG6E" target="_blank"><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" /></a>


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