npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

governify-commons

v1.19.1-0

Published

Governify Commons used for Governify microservices

Downloads

180

Readme

Governify-Commons

NPM

This module is intended for use in all microservices of Governify infrastructure. It allows for abstract the functions that are mostly used in the code, to have a better control and parametrize the functions call.

Currently Governify-Commons implements the following:

  • Governify infrastructure urls management
  • A httpClient that should be used for all the calls (This httpClient is an Axios wrapper)
  • A logger able to rotate files and change logging levels in execution time
  • A configuration manager compatible with local or external files
  • Utils functions commonly used in all the microservices

It also features a middleware in /commons to modify configuration parameters and show microservice information.

Implementations

The module should be initiated at the entrypoint of the service as soon as possible in the code with:

const governify = require('governify-commons')

After that, you must call the module init function that will load the infrastructure and all the configurations:

governify.init().then(function(commonsMiddleware) {
    //All the code for initializing the microservice
    }
);

This example is for running without any configuration in it. Also the commons Middleware will be resolved for it to be instantiated in the server. This is explained in the last part of this article.

Once you have called the init function, you can require governify in other classes of your microservice without having to initialize it again.

If you require governify-commons before the init function being completed, the infrastructure urls or the configuration can have null values.

infrastructure

In order to call another service in the infrastructure you must implement the code as following:

const governify = require('governify-commons');
governify.infrastructure.getService('internal.registry').get('/echo/');
governify.infrastructure.getService('internal.registry').post('/echo/', {body}, {config});

You can also get only the service url with:

const governify = require('governify-commons');
governify.infrastructure.getServiceURL('external.registry')
//This returns the service url that is stored in the loaded infrastructure file as (external.registry)

httpClient

If you want to call a service that is not specified in the infrastructure file, you should use the httpClient as follows:

const governify = require('governify-commons');
governify.httpClient.get('http://api-echo.herokuapp.com/echo/');

The syntax is the same as axios, the code above is the equivalent of:

const axios = require('axios');
axios.get('http://api-echo.herokuapp.com/echo/');

You can read axios documentation to see all the posible functions: Axios NPM documentation

logger

Governify Commons integrates a logger consisting in 5 different logging levels and integrating automatic file rotation. The logger is obtained by executing the getLogger() function from the commons instantiation.

const logger = require('governify-commons').getLogger();

logger.debug("Debug message");
logger.info("Info message");
logger.warn("Warn message");
logger.error("Error message");
logger.fatal("Fatal error message");

Messages can be tagged by marking the logger object with .tag(tagName) function and store this configuration in the variable or in the actual logging call:

const logger = require('governify-commons').getLogger().tag("tag1");

// Will log a message tagged with "tag1"
logger.info("Info message");

// Will log a message tagged with both "tag1" and "tag2" tags
logger.tag("tag2").warn("Warn message");

// Will log a message only tagged with "tag1" because the logger also tagged with tag2 wasn't stored in the logger variable
logger.error("Error message");
OUTPUT

[2021-07-06T09:06:17.395Z] [info ] [NOTRA] [tag1] Info message
[2021-07-06T09:06:17.397Z] [warn ] [NOTRA] [tag1, tag2] Warn message
[2021-07-06T09:06:17.398Z] [error] [NOTRA] [tag1] Error message

configurator

In order to load a configuration to the module, you have to specify it in the init function:

governify.init({
    configurations: [
        {
            name: 'configName',
            location: './configurations/configName.' + (process.env.NODE_ENV || 'development') + '.yaml',
            default: true
        }
    ]
}).then(function (commonsMiddleware) {
    //All the code for initializing the microservice
}
);

After that you can load the configuration as follows:

let config = governify.configurator.getConfig('configName');

All the values of the configurations can be replace with Environment variables. For example: With a config named 'exampleConfig' and the following value:

test:
    childrenTest: originalValue
serverPort: 8080

In order to replace a value of this config, you can specify the following env vars:

GOV_CONFIG_exampleConfig_test_childrenTest=replacedValue
GOV_CONFIG_exampleConfig_serverPort=9000

The result config when you execute the code will be:

governify.configurator.getConfig('configName');

Result:

test:
    childrenTest: replacedValue
serverPort: 9000

utils

The most used functions of the utils are:

const governify = require('governify-commons');
//This function read the content of a file in a local storage path or from a external url
let object = governify.utils.loadObjectFromFileOrURL(url or fileLocation);

You can check utils.js file to see all posible functions.

Middleware

By calling the init function, it resolves a middleware. It will serve a set of functions in the /commons endpoint. Make sure it is used after required middlewares for the correct functioning of the server are already instantiated like the body parser and cors if needed.

app.use(
    bodyParser.urlencoded({
        limit: '50mb',
        extended: 'true'
    })
);

app.use(
    bodyParser.json({
        limit: '50mb',
        type: 'application/json'
    })
);

app.use(cors());
app.use(commonsMiddleware);

These are the available endpoints:

  • [GET] /commons - Returns information about the microservice commons is running on, such as the name and version of the microservice, the version of the commons installed and if the requests are being logged.
  • [GET] /commons/infrastructure - Returns the services infrastructure in a JSON format.
  • [POST] /commons/infrastructure/update - Updates the microservice infrastructure by requesting the infrastructure.yaml file again.
  • [GET] /commons/requestlogging - Answers with an "Enabled" or "Disabled" message indicating if commons is loggin the requests information.
  • [POST] /commons/requestlogging/enable - Enables the request logging.
  • [POST] /commons/requestlogging/disable - Disables the request logging.
  • [POST] /commons/requestlogging/swap - Inverts the status of the request logging.
  • [GET] /commons/logger - Returns the actual logger configuration (levels, file rotation, etc)
  • [POST] /commons/logger - Receives a JSON (with the same structure as the GET) to substitute the logger configuration.

Update and publish

TODO