Friday, 10 May 2013

Instant RabbitMQ Messaging Application Development How-to


My book Instant RabbitMQ Messaging Application Development How-to is available to pre-order.

I think its a great book if you are interested in messaging and scaling applications; the examples are in Node.js.




Wednesday, 24 April 2013

super grass, a node.js monitoring tool


  ___ _   _ _ __   ___ _ __    __ _ _ __ __ _ ___ ___ 
 / __| | | | '_ \ / _ \ '__|  / _` | '__/ _` / __/ __|
 \__ \ |_| | |_) |  __/ |    | (_| | | | (_| \__ \__ \
 |___/\__,_| .__/ \___|_|     \__, |_|  \__,_|___/___/
           | |                 __/ |                  
           |_|                |___/                   

super grass is a node.js monitoring tool supporting

  • web apis
  • Redis
  • mongodb
  • rabbitmq

This tool allows you to define various resources to be monitored for a heatbeat; at a configurable interval.
A report is generated at these intervals and an event is fired; which you can hook into allowing you to analyse and then perform your preferd form of notification and/or logging.

example

The code is simple; you create a super-grass object; start watching and wait for a response; thesnitch event to fire.
var SuperGrass = require('super-grass')
    , options = require('./options');

var superGrass = new SuperGrass(options);
superGrass.watch();

superGrass.on('snitch', function(report) {
  console.log("RESULTS", report);
  //mail here
  //log here
});
The report returned contains an array of activity; each resource is returned with a failure identifier; each line in the report represents a single retry.
{ name: 'api for airasoul.net', failed: false },
{ name: 'api for airasoul.net', failed: false },
{ name: 'api for airasoul.net', failed: false },
{ name: 'api for 127', failed: true },
{ name: 'api for 127', failed: true },
{ name: 'api for 127', failed: true },
{ name: 'local mongo', failed: true },
{ name: 'local mongo', failed: true },
{ name: 'local mongo', failed: true },
{ name: 'redis local', failed: false },
{ name: 'redis local', failed: false },
{ name: 'redis local', failed: false },
{ name: 'local rabbitmq', failed: false },
{ name: 'local rabbitmq', failed: false },
{ name: 'local rabbitmq', failed: false }
At this point you could log this information; send an email or sms; its in your hands..

options

super-grass; requires an options object with the following properties.
module.exports = {
  settings: {
    interval: "10000",
    retry: "3",
    retryTimeout: "500"
  }
, resources: 
  [{
    name: "api for airasoul.net",
    type: "api",
    host: "http://airasoul.net",
    enabled : true
  },
  {
    name: "api for blog.airasoul.net",
    type: "api",
    host: "http://blog.airasoul.net",
    enabled : true
  },
  { 
    name: "local mongo",
    type: "mongo",
    host: "localhost",
    database: "staging",
    port: 27017,
    enabled : true
  },
  {
    name: "redis local",
    type: "redis",
    host: "http://127.0.0.1",
    port: 6379,
    enabled : true
  },
  {
    name: "local rabbitmq",
    type: "rabbit",
    host: "http://127.0.0.1:15672/api/overview",
    username: "guest",
    password: "guest",
    enabled : true
  }]
}
This settings section contains:
  • interval - the interval between notifications
  • retry - the number of retries for a resource
  • retryTimeout - a timeout value between retries
  • resources - a list of resources to be monitored

todo

  • Support REST POST/PUT/DELETE requests
  • An administration tool which lists the reported activity

Thursday, 28 February 2013

Socket.IO Real-time Web Application Development

So I have spent the last month reviewing a book for Packt publishing, titled, Socket.IO Real-time Web Application Development by Rohit Rai.

An interesting experience..

Anyway; if you are interested in Socket.IO/Node.js, I would highly recommend this book.

Monday, 14 January 2013

Building restful HTTP applications with ASP.NET Web API; assisted by specifications - API Keys

Let me see your identification.


You don't need to see his identification. Not yet anyway, lets change that. We would like to secure our 'Add new droid' feature by asking users to provide an API Key so we can restrict those adding new droids.



Feature File
Lets update our 'Add new droid' feature file with a new column 'Key' and a new example 'Add a valid droid with invalid key'; this example will return a status code of 401 'Unauthorised'; our existing examples will be given valid keys and will continue to respond as originally specified.


Implementation
There are a couple of approaches to handling identification and authorisation with ASP.NET Web API. The two most common approaches are to use a HTTP Message Handler or a Filter attribute.

This example will implement API Keys with a Filter attribute. The filter will simply inspect and then compare an API Key from the HTTP requests header; here are the steps we need to take:

  • Pull API Key out of the HTTP request headers
  • Get valid API Key from repository/config (not important..)
  • Compare keys, and return true or false

Lets define our specification for handling authorisation; creating an authorisation filter requires we extend AuthorizationFilterAttribute and override the OnAuthorization method.

Here is our specifications context, basically we are mocking out a token generator which has a method 'GetToken' that accepts an unencrypted key and returns an encrypted key:


Here are the specifications for our two scenarios:
  • request with an invalid key
  • request with a valid key



Here is our authorization filter; it simply grabs the API key from the request headers; passes this key to our token generators 'GetToken' method and checks the response against a stored encrypyed key.


Lets add an unencrypted key to our web.config file; this would ideally be held somewhere safe or if you planned to host multiple API keys in a database.
<add key="AuthenticationKey" value="TheseAreTheDroidsYouAreLookingFor" />

Now add our new attribute to our droids controller POST method:
[ApiAuthorizationFilter]
public HttpResponseMessage Post(Droid droid)

Now whenever a request comes into this method the filter will be executed and will check the requests headers for a key.

GitHub
The code for this series of posts can be found here: github.com/AndrewKeig/droids

My next post in this series will discuss hosting.

Thursday, 10 January 2013

Building restful HTTP applications with ASP.NET Web API; assisted by specifications - CRUD

All right shut up! I'll take this one..


Droid acquisition is no easy task and we need to implement the 'Create a new droid' feature:
Action HTTP method URI
Create a new droid POST /api/droids



Feature, Create a new droid
Below is our feature file for create a new droid; it includes a scenario with two examples,
  • Add a valid droid
  • Add an invalid droid


Implement, Create a new droid
Our specifications are pretty simple; for a valid request we check the response code is set to Created, and we should have a location set in the headers to represent our new resource. An invalid request should return a BadRequest response.


Here is our controller method for creating droids.


GitHub
The code for this series of posts can be found here: github.com/AndrewKeig/droids

My next post in this series will discuss security.