Introducing a yeoman generator for drachtio

I've been heartened to see the rapid adoption of drachtio over the past year by many folks who are building SIP server applications. But as the guy who probably still writes the most drachtio applications, I've often found myself copying and pasting boilerplate when I start up a new project. (Bad Dave! Bad!)

And I've also developed some ways of testing my drachtio apps using docker that I think others might benefit from.

Enter yeoman!

For some time now, I've been meaning to create a yeoman generator for drachtio, and yesterday I finally went and did it. With this generator, you get a basic drachtio app template, and (optionally) a working test suite. Of course, you get my opinionated choice of some packages as well:

  • pino (for logging)
  • config (for config file management)

and the generated app will be using ES6 syntax and requiring a fairly recent-ish version of Node.js

To use it, first make sure you have yeoman installed:

$ sudo npm install -g yo

Then install the drachtio generator:

$ sudo npm install -g generator-drachtio

Now you are ready to create a new project!

Create a fresh directory, change into it and then run yo drachtio, answering a few basic questions:

$ mkdir my-app && cd $_
$ yo drachtio
Enter the application name: my-app

Which sip request types will you handle: invite, register, info, options

Does your app need to use drachtio-fsmrf for ivr/media control?: Yes

Do you want a (docker-based) test suite created? Yes

At that point yeoman will scaffold out a basic app template for you.

A word on testing

Everyone agrees we should write test cases, but sometimes they are quite the pain to build, especially when we are talking about SIP server types of applications.

After quite a bit of experimenting, I've settled on creating my test cases using docker and docker-compose. (Of course, this requires you have docker installed on your laptop, but if you don't should anyways for all its other goodness).

The test suite that gets scaffolded out for you (if you request it) creates a docker-compose file (found in ./test/docker-compose-testbed.yaml) that creates a docker network and then starts up a drachtio container as well as a sipp container.

I find sipp is a good way to have a simple test UAC or UAS client that can send SIP transactions to the drachtio server and application that are under test. Additionally, if you are using the drachtio-fsmrf package in your app, it will throw a freeswitch container into the docker network for you as well.

As per usual, you run the tests by:

$ npm test

Try it out!

You will see that test stubs have been provided for each of the SIP request methods that you indicated you want to handle in your app.

Of course, to start with they are pretty simple (incoming INVITEs are connected either to freeswitch or the sipp UAS; other incoming request types are responded to with a 480 response), but the beauty of it is that the test scaffolding is all in place and should be pretty easy to build on from there.

You can also run code coverage reports:

$ npm run coverage

and lint your code:

$ npm run jslint

Little help?

I'm by no means a yeoman expert, and this generator is lacking a few niceties (e.g. doesn't prompt for things like author, description, license, etc). If anyone would like to help improve it I would love PRs on github