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

v0.1.0

Published

App Middleware to control api's by SLA using AML

Downloads

5

Readme

Governify-npm

This is BETA module and may have bugs and don't work correctly. It is intended for qualified beta testers only and must not be used in production systems.

The node module to control API using Governify tools. This module is a middleware which you can use on ExpressJS or ConnectJS.

Intallation

On your application package run next command:

$ npm install governify

Example

To control the api you must use governify.control(app, [options]) see Options Object

var governify = require('governify');
var express = require('express');
var app = express();
var port = 9999;

governify.control(app, options = {
	datastore: "http://datastore.governify.io/api/v6.1",
	namespace: "default",
	apiKeyVariable: "apikey",
	defaultPath: "/api",
	customMetrics: [
		{
			method: 'POST,GET',
			term: 'RequestTerm',
			metric: 'Requests',
			calculate: function(currentValue, req, res, callback){
				//asyncronousCalculation
				callback( parseInt(actualValue) + 1 );
			}
		},
		{
			metric: 'AVGResponseTime',
			calculate: function(currentValue, req, res, callback){
				//asyncronousCalculation
				callback( res._headers['x-response-time'] );
			}
		}
	]
});

var birds = [
	{
		"id" : "234h235buh45bhy456",
		"specie" : "Halcon",
		"place" : "Doñana",
		"legDiameter" : 1.0,
		"wingSize" : 10.0,
		"eggs" : 10,
		"hatches" : 2
	}
]
app.get("/api/v1/birds", function(req, res){
	res.send(birds);
	res.end();
});

app.listen(port, function(){
	console.log("App listening on port: ", port);
});

NOTE: You must do requests with ?apikey=:key. For example:

curl -X GET http://localhost:9999/api/v1/birds?apikey=proUser1

Options object

| Field Name | Type | Description | | :--------- | :------------:| :------------| | datastore | string| Optional This is the endpoint URL where the service that stores and analyzes the agreement is located. NOTE: If you want to use our datastore, you haven't to give value to this field and the module is going to use datastore by default. | | namespace | string| Optional This field can be used to make out two type of agreement or two type of service, e.g. if you have two services named "api1" and "api2" you can store, analyze and check by different namespaces. By default: "default" | | apiKeyVariable | string | Optional This field defines the name of url param which will be checked and that will contain the key to identify the agreement of current request. By default: "apikey"| | defaultPath | string| Optional This field defines the default path that is used by middleware for metrics without path field. By default: "/" | | customMetrics | [metricObject](#metricsObject)| Optional This field defines the middelwares to control term with custom metrics |

Metric object

This object defines fileds to create a middleware and to assosiate an agreement term to it.

| Field Name | Type | Description | | :--------- | :------------:| :------------| | path | string| Optional Path over the middleware is applicated. | | method | string| Optional Method over the middleware is applicated. | | term | string | Optional Middleware is assosiated to this term, that must be specified on the SLA. | | metric | string| Optional The metric that will be update by this middleware. | | calculate | Function| Optional This is a function that calculates the value of metric. If you need to calculate it asynchronous you must use: callback(value), else you use: return value. |

Example

  • Example 1

{
	datastore: "http://datastore.governify.io/api/v6.1",
	namespace: "default",
	apiKeyVariable: "apikey",
	path: "/api",
	customMetrics: [
		{
			path: "/api",
			method: "POST",
			term: 'ResourcesTerm',
			metric: 'Resources',
			calculate: function(currentValue, req, res, callback){
				//asynchronousCalculation
				db.find({}, function(contact){
					callback( contact.lenght );
				});				
			}
		}
	]
}

On this example you must do request with ?apikey=:key. For example:

curl -X GET http://localhost:9999/api/v1/birds?apikey=proUser1

The URL that will be used to check if "key" is authorized is:

http://datastore.governify.io/api/v6.1/default/agreements/proUser1

And customMetrics creates a middleware that checks if ResourceTerm is fulfilled and updates the Resources metric with the value that is calculated asynchronously.

  • Example 2

{
	datastore: "http://datastore.governify.io/api/v6.1",
	namespace: "service1",
	apiKeyVariable: "user",
	path: "/api",
	customMetrics: [
		{
			path: "/api",
			method: "POST",
			term: 'RequestTerm',
			metric: 'Requests',
			calculate: function(currentValue, req, res, callback){
				//synchronousCalculation
				return actualValue + 1;			
			}
		}
	]
}

On this example you must do request with ?user=:key. For example:

curl -X GET http://localhost:9999/api/v1/birds?user=proUser1

The URL that will be used to check if "key" is authorized is:

http://datastore.governify.io/api/v6.1/service1/agreements/proUser1

And customMetrics creates a middleware that checks if RequestTerm is fulfilled and updates the Requests metric with the value that is calculated synchronously.