tonto

Tonto.js: Apache Config File Generator

 _____ ___  _  _ _____ ___
|_   _/ _ \| \| |_   _/ _ \
  | || (_) | .` | | || (_) |
  |_| \___/|_|\_| |_| \___/.JS
 APACHE CONFIG FILE GENERATOR

Tonto.js

This library helps with automating the generation of Apache web server configuration files. It provides a native camelCased javascript function API which support all native (and native mod) configuration directives from Apache versions 2.4, 2.2, and 2.0:

Additionally, Tonto is easily configured at instantiation to support any custom directive that you may have, (such as 3rd-party mod directives like 'PassengerRoot').

This library is named after the Tonto (Dilzhę́’é) people, who are one of the Western Apache groups from North America. Long ago, their enemies called them "foolish", "wild", "crazy", and "those who you don't understand" for speaking and doing things differently than their neighbors. Today, they are know throughout the art communities for their superior fine crafting.

Use NPM to install the tonto package into your node.js project:

$ npm install tonto --save

1. Each instance of Tonto is a version-specific apache config document object that you add directives to by calling it's directive functions:

var tonto = new Tonto('2.4');

2. There are solo directive functions that take a single value argument, and block directive functions which take a sub-directive setter object as the second argument:

tonto.serverName('somesite.com');
tonto.virtualHost('*:80', function (subDirectiveDocument) {
  // Here, you can call any directive function directly on subDirectiveDocument, and it will be added as a sub-directive. 
  subDirectiveDocument.serverName('somesite.com');
});

3. When the document object has all directives added to it, you can render the document to string by calling:

tonto.render();

4. All directives are chainable:

var renderedDocument = tonto
  .loadModule('some_module /some/path/to/module.so')
  .serverSignature('Off')
  .traceEnable('Off')
  .render();

Tonto.js comes with built-in support for all native (and native mod) directives in versions 2.4, 2.2, and 2.0. Additionally, you can specify any number of extra directives.

You specify which version you want to use during instantiation of the constructor:

var twoFour = Tonto('2.4');
Object.keys(twoFour).length; // 594 
 
var twoTwo = Tonto('2.2');
Object.keys(twoTwo).length; // 419 
 
var twoZero = Tonto('2.0');
Object.keys(twoZero).length; // 364 

Whereas Apache directives are typed in TitleCase, tonto converts each of the directives into camelBackCase named functions.

For example:

  • LoadModule is added to the document with: .loadModule('some_module /some/path/to/module.so')
  • SSLCertificateKeyFile is added to the document with: .sslCertificateKeyFile('/some/path/to/some_key.pem')
  • VirtualHost is added to the document with: .virtualHost('*:80', subDirectiveSetter)

Block directives require a function as the second argument. This function has one argument itself, which is a clean sub-document. You can call any directive from the main document on a sub-document.

Here is an example of a sub-directive setter defined as a named function:

tonto.virtualHost('*:80', subDirectiveSetter);
 
function subDirectiveSetter(subDirectives) {
  subDirectives.serverName('somesite.com');
}

This example will render:

<VirtualHost *:80>
  ServerName somesite.com
</VirtualHost>

If you are using a 3rd party mod that provides custom directives, there is an easy way to extend tonto.js with custom functions for this purpose:

tonto.extend([
  'CustomDirective',
  '<CustomBlock>'
]);
 
tonto.customDirective('some value'); // Solo directive 
 
tonto.customBlock('some value', function (subDirectives) {
  // Because CustomBlock is surrounded in < and >, it is processed as a block directive 
});