node-service-commons

0.91.40 • Public • Published

Node Service Commons

  ___              _           ___                             
 / __| ___ _ ___ _(_)__ ___   / __|___ _ __  _ __  ___ _ _  ___
 \__ \/ -_) '_\ V / / _/ -_) | (__/ _ \ '  \| '  \/ _ \ ' \(_-<
 |___/\___|_|  \_/|_\__\___|  \___\___/_|_|_|_|_|_\___/_||_/__/
                                                                          

An MVC platform of common JSON/REST services for node / web applications implemented for classical instantiation and inheritance.

NPM version Build Status Dependency Status

Introduction

The Node Service Commons is a set of components that support REST / web service containers. The MVC implementation includes factories, data access base objects, delegates, web and data service objects. Implementation uses Crockford-style classical construction (but no ECMA6 classes). The associated docker container helps create a containerized implementation.

Node 6.x is required for the "future" branch. Future changes include modifying the REST end points to use standard verbs and reimplementation using promises, es6, web-sockets etc. The current version will be preserved in a pre-6.0 branch.

Requires minimum node 4. If you are using an older version of node, please use the pre-4.0 branch.

Features

  • includes start and graceful shutdown hooks
  • domain based key generation for no-sql databases
  • includes docker file to help containerize your service applications
  • brings order to the chaos

Controllers Package

A collection of startup and control objects. Most classes are designed to be extended by concrete web control and configuration objects.

CommonBootstrap

This object is used to parse command line inputs to set environment, logfile, configuration, etc. Its primary method, parseCommandLine() is used to parse an array of strings, typically process.argv and extract the environment name (development, staging, test, production, etc), the config and log files. For example:

    var Bootstrap = require('node-service-commons').controllers.CommonBootstrap;

    // instantiate with the application version
    var bootStrap = new BootStrap( Config.VERSION );

    // parse the options and set defaults
    var options = bootStrap.parseCommandLine( process.argv );

    assert options.env === 'development';

AbstractApplicationFactory

The AbstractApplicationFactory constructs components and services required for a typical web service. Services include middleware, loggers, configurations, etc.

Typical Use:

    // called by the concrete ApplicationFactory (this) with options/configuration

    AbstractApplicationFactory.extend( this, options );

Delegates

CommonValidator

The common validator provides a base class to validate input values and return all detected errors by populating a supplied list. This approach enables returning all detected errors to the client application with associated field designators.

Most of the vaidation work is done using lodash or validator functions.

MiddlewareDelegate

The middleware delegate provides standard middleware to check API key, enable cross domain access, and provide a shutdown hook to gracefully bring service containers down through a local HTTP request. A typical shutdown request would look like this:

    curl -d token=<api-token> http://127.0.0.1:<port>/shutdown

The standard delegate also includes a X-API-Key filter to require all requests to use the key. This also enforces the same-domain rule which can be difficult in development. So, there is an options switch that can be set in development to ignore the key requirement. This needs to be enforced on both server and client.

Models

ServiceRoute

A simple datamodel that contains route info including method, url, and service function. ServiceRoute objects are defined in named WebService containers to enable auto-wiring on construction.

ServiceResponse

The ServiceResponse is a wrapper for all JSON returns. There are two possible status conditions: ok or failed. Normal returns include a status of ok, a timestamp, version and the model or list payload. Error or failed returns include a status of failed, a timestamp, version and the reason for the failure.

Data Access Objects

Abstract NoSQL DAO

Optimized for redis but can support others. The standard implementation includes these methods:

  • findById - return the model located by id
  • query - generic query that returns all rows for the given domain
  • insert - insert a model; if id is missing, create one
  • update - update a model
  • createModelId - create a standard uuid-like id
  • createDomainKey - create a key combining the domain name and id
  • parseModel - parse the domain model

All of these methods work out of the box and are easily overridden to provide specific implementations.

Services

AbstractWebService

AbstractWebService provides a base class for all web services. Its methods include creating ServiceReponse objects for ok/failed model or list responses. It also supports digest calculation to insure the the payload is delivered with the correct signature.

AbstractDataService

The AbstractDataService provides a base class used to connect web services from the API to the back end services. Data service usually connects to a data access object but can also connect to file system, cash, other servers or any appropriate service.

IndexPageService

The IndexPageService presents a very simple HTML page displaying the application title, version and a timestamp. A more elaborate page can be defined through configuration if needed.

WebStatusService

WebStatusService is used to report the web container's current status. Values include up-time, available memory, the environment name, version, etc.

Errors and warnings are tracked by listening for process events 'error' and 'warning'. To enable error and warning listeners, simply invoke 'initListeners()' on the web status service. This is usually done in the application's implementation of ApplicationFactory like this:

const services = factory.createWebServices( factory.createServiceFactory(), webServiceList );
const service = services.find(svc => svc.serviceName === 'WebStatusService');
service.initListeners();

or like this

const services = factory.createWebServices( factory.createServiceFactory(), webServiceList );
services.forEach(service => {
    if (typeof service.initListeners === 'function') {
        service.initListeners();
    }
});

Accessing web status shows the warning and error count. Here is a sample status dump:

{ status: 'ok',
  ts: 1464107033676,
  version: '1.0',
  webStatus: {
     version: '00.91.60',
     env: 'production',
     epoch: '2016-05-22T19:25:07.015Z',
     uptime: '01 days+20:58:46',
     warnings: 0,
     errors: 0,
     process: { pid: 1, title: 'node', vers: 'v4.4.3' },
     totalmem: 1928470528,
     freemem: 261324800,
     loadavg: [ 0.00341796875, 0.02392578125, 0.04541015625 ],
     arch: 'x64' 
   } 
}

This format may change in future versions but typically to add new attributes.

<<<<<<< HEAD

Docker Container

=======

Docker

There is a sample docker file and associated build script in the docker folder. The container script is based on the small alpine disro and includes node 6.10 (no npm). The intent is for this to be a template used for your application.

Create the container with make container. From there you can create a run script to "volume" in your source and start your application. Examples to come soon.

d20d397613ada43c840905241bc8cb3d51a33c50

Mocks

  • MockExpress
  • MockAgent (super agent, requires version 2)
  • MockRedisClient

Common Web Service Architecture

web service architecture

find more info here...

TODO:

  • create demo implementation
  • example of method overrides

Copyright © 2014-2018, rain city software | Version 0.91.40

Versions

Current Tags

VersionDownloads (Last 7 Days)Tag
0.91.408latest

Version History

VersionDownloads (Last 7 Days)Published
0.91.408
0.91.390
0.91.388
0.91.371
0.91.360
0.91.350
0.91.341
0.91.330
0.91.3220
0.91.3129
0.91.290
0.91.280
0.91.260
0.91.240
0.91.230
0.91.210
0.91.200
0.91.190
0.91.180
0.91.170
0.91.161
0.91.150
0.91.141
0.91.130
0.91.120
0.91.110
0.91.100
0.90.890
0.90.880
0.90.870
0.90.860
0.90.840
0.90.830
0.90.820
0.90.810
0.90.800
0.90.790
0.90.780
0.90.770
0.90.760
0.90.750
0.90.740
0.90.730
0.90.720
0.90.710
0.90.700
0.90.690
0.90.680
0.90.670
0.90.660
0.90.650
0.90.640
0.90.630
0.90.620
0.90.610
0.90.600
0.90.590
0.90.580
0.90.570
0.90.560
0.90.550
0.90.540
0.90.530
0.90.520
0.90.510
0.90.500
0.90.490
0.90.480
0.90.470
0.90.460
0.90.450
0.90.440
0.90.430
0.90.420
0.90.410
0.90.400
0.90.390
0.90.380
0.90.370
0.90.360
0.90.350
0.90.340
0.90.330
0.90.320
0.90.310
0.90.300
0.90.290
0.90.280
0.90.270
0.90.260
0.90.250
0.90.240
0.90.230
0.90.221
0.90.210
0.90.200
0.90.190
0.90.180
0.90.171
0.90.160
0.90.150
0.90.140
0.90.130
0.90.120
0.90.110

Package Sidebar

Install

npm i node-service-commons

Weekly Downloads

71

Version

0.91.40

License

Apache-2.0

Last publish

Collaborators

  • darryl.west