Coder Social home page Coder Social logo

node-microsvc-lib's Introduction

node-microsvc-lib

Git Commit Npm Version NPM Vulnerabilities

Installation

npm install node-microsvc-lib --save

Usage

Creating a microservice app:

app.ts

"use strict";

import {Microservice, ConsoleLogger} from "node-microsvc-lib";

// factories/modules
import {RequestLogger} from "./factories/request_logger";
import {HealthCheck} from "./factories/health_check";
import {TestRestCtrl} from "./factories/rest_service";

// configs
import configs = require("./config/config");

const logger = new ConsoleLogger();

// create microservice appv
const app = new Microservice(configs, logger);

app.register_dependency("logger", logger);

app.register_factory("request_logger", RequestLogger);
app.register_factory("test_rest_ctrl", TestRestCtrl);
app.register_factory("health_check", HealthCheck);

process.on("uncaughtException", (err:Error)=>{
  logger.fatal(err);
});

app.init((err?: Error) => {
  if (err)
     return logger.error(err);

  logger.info("APP STARTED");
});

How configuration works

The Microservice instance expects a ServiceConfigs instance. This ServiceConfigs instance requires the base dir __dirname, an instance of AppBaseConfigs and an (optional) instance of IConfigsProvider.

Three sets of configuration values exist:

  • Parameters - that can be of type STRING, BOOL, INT_NUMBER or FLOAT_NUMBER;
  • Feature Flags - always of boolean type;
  • Secrets - always of string type.
ServiceConfigs (Required for Microservice obj instantiation)

This is the object that the Microservice requires to source all its runtime configs.

AppBaseConfigs (Required)

Optional instance that fetches all config values from an external service such as consul or hashicorp vault.

IConfigsProvider (Optional)

Optional instance that fetches all config values from an external service such as consul or hashicorp vault.

Load order / precedence

The order of loading:

  1. params.js file - ServiceParams instance gets loaded along with the default values;
  2. params.ENV_NAME.js file - the one that overrides values in ServiceParams per env;
  3. IConfigsProvider - if a correspondent key name exists the value from the provider overrides the current one;
  4. Environment vars - all parameters can be overridden by passing an uppercase env var with the param key name

In summary, env vars always win (if defined).

Example of config code structure

See the ref implementaion

config/config.ts

"use strict";

import {ServiceConfigs, AppBaseConfigs} from "node-microsvc-lib";

let app_base_confs = new AppBaseConfigs();
app_base_confs.env = process.env.APP_ENV || 'dev_local';
app_base_confs.solution_name = "my_solution";
app_base_confs.app_name = "my_app";
app_base_confs.app_version = "0.0.1";
app_base_confs.app_api_prefix = "";
app_base_confs.app_api_version = "1";

export = new ServiceConfigs(__dirname, app_base_confs, null);

config/params.ts

"use strict";

import {ServiceParams, ServiceParam, PARAM_TYPES, ServiceFeatureFlag, ServiceSecret} from "node-microsvc-lib";
let params = new ServiceParams();

params.add_param(new ServiceParam("test_param",
	PARAM_TYPES.STRING, "default_val",
	"test param to be overridden by env_var"));
 
params.add_feature_flag(new ServiceFeatureFlag("RUN_EXPRESS_APP",
	true, "start the express application"));

params.add_secret(new ServiceSecret("secret1", null, "db password example"));

export = params;

config/params.APP_ENV.ts (optional file see step 2 above)

"use strict";

import {ServiceParams, ServiceParam, PARAM_TYPES, AppBaseConfigs} from "node-microsvc-lib";

module.exports = function(app_base_confs:AppBaseConfigs, service_params:ServiceParams){
	// do whatever overrides with it

	service_params.add_param(new ServiceParam("test_param", PARAM_TYPES.STRING,
		"per_env_value", "kafka broker connection string - ex: 127.0.0.1:9092"));
};

Pre-requisites for contributing

NVM - Node Version Manager - https://github.com/creationix/nvm

curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.34.0/install.sh | bash

After NVM is installed, execute this to download and install the correct node version:

nvm install 10.15.0

node-microsvc-lib's People

Contributors

pedrosousabarreto avatar denisdipaolo avatar kondrashov-do avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.