Skip to content

rahulballal/api-server-template

Repository files navigation

API Server Template

A template project for Node.js HTTP API

Key Features

  1. Uses latest LTS node.js (v8.9.x)
  2. Builds a lightweight (Node Alpine) Docker container following best practices.
  3. Uses node-config for easy config management.
  4. A functional Jenkinsfile for CI/CD, setup to be configured with Nomad/Kubernetes container orchestration.
  5. Super fast pino.js logger
  6. Follow these simple steps to use this template.

Description

Web Framework

  • A fast lightweight Node.js framework for building http apis
  • Out of box schema support
  • Strong plugin architecture

Testing

Philosophy

  • All unit and integration tests are colocated with the system under test.

For example my-module.js will be colocated with my-module.spec.js file.

  • All Http/Functional tests are located in the e2e directory
  • Along with popular airbnb config, helps the code lint free!
  • De-facto testing framework for node.js
  • All configuration has been stored in the mocha directory.
  • De-facto assertion framework for node.js
  • De-facto mocking framework for node.js
  • A feature complete Http API and connect based applications
  • De-facto javascript code coverage tool.

CI/CD

CloudFormation to create cloud resources

  • Simple node module to run npm script in sequence or parallel

Why not just use npm scripts or Grunt or Gulp?

We want to ensure that we are able to run scripts after devDependencies have been pruned for the project. One way is to manipulate how and when devDependencies are installed or keep the logic simple. All the CI/CD orchestration is baked into the Rakefile.

Why not bake all CI CD logic in the Jenkinsfile?

From my experience baking deployment/build logic in Jenkinsfile/Groovy is extremely painful. Rakefile is a modern version of a task system much like Make. It is an extremely flexible and testable task system. Having a file like this helps the repository portable with any kind of CI/CD tool. Jenkins has been configured in this case.

NOTE: The CI/CD runtime must include node.js , Ruby and Rake. This is only required at build time

Jenkins with Scripted Pipelines

When a commit happens on master branch

  • Run ci
  • Build Docker container
  • Publish Docker container
  • Publish Nomad Job to default environment (eg: STAGE)

When a commit happens on a hotfix branch

  • Run ci
  • Build Docker container
  • Publish Docker container
  • Publish Nomad Job to PRODUCTION environment

Deploying to an environment using Docker Artifact

  • Go to Jenkins

  • Navigate to the Job

  • Build with Parmeters

  • Fill out the form

    • Provide the ARTIFACT_NAME e.g. api-server-template:latest
    • Pick an environment to deploy
    • Click Build

Container Scheduler / Orchestrator

It is extremely common these days to to use a Container orchestration and scheduling system like :

Because a system like this is considered infrastructure related:

Both these files are consumed in the Jenkinsfile.