cnnlabs / cnn-hapi

CNN Hapi
10 stars 4 forks source link

CNN Hapi

Basic Hapi server with some baked in features that can be pulled in as a dependency of another application to extend as needed.

build node npm npm-downloads dependency-status

Features include:

Requirements

Node 6.0.0+

Installation

$ npm install

Usage

Look at the /example/app.js to see an example of how this can be pulled in as a dependency. You can see it running by doing the following.

$ PORT=5000 node example/app.js
info Server running at http://0.0.0.0:5000
info Server name: testHarness
info Server version: 0.1.0
info Server maxListeners: 1000
info Server environment: development
info Server in debug mode: true
160915/014027.438, [ops] memory: 65Mb, uptime (seconds): 5.705, load: [1.30322265625,1.486328125,1.5888671875]
160915/014032.438, [ops] memory: 58Mb, uptime (seconds): 10.706, load: [1.35888671875,1.49462890625,1.5908203125]

You can also navigate to localhost:5000 and see a served page.

Swagger documentation - localhost:5000/documentation

Healthcheck monitoring - localhost:5000/__health

Testing

$ cd <cnn-hapi-root> $ npm run example-server This will run the example server in the ./example directory.

ENV VARS

serverInstance(options)

The following options set defaults at the server level and can override CNN-Hapi Defaults Populate notes are in order of priority. Example: populate: process.env.SOMEVALUE || options.someValue. In that example if process.env.SOMEVALUE is not set it will default to options.someValue, etc, etc Manual override possibilites are expressed in options.someValue

options is an object that can take the following keys

Override caching on individual routes

Using the reply.header() function can set headers on a singular route

{
    method: 'GET',
    path: '/override-headers',
    handler: (request, reply) => {
      reply('Peep the response headers in swagger docs')
      .header('Cache-Control', '2')
      .header('Surrogate-Control', 'baz');
    },
    config: {
      description: 'Example route for demonstrating how to  override headers by route',
      tags: ['api']
    }
  },

For explicit usage check this implementation in ./example/routes

Test

$ npm run example-server

It runs example server located in ./example. Pass in ENV vars through the above command or hardcode into the package.json located in the ./example directory.

Go to {HOST}:{PORT}/documentation to view the new swagger docs and to test current CNN-Hapi logic.