An application bootstrapper and plugin framework.
A node.js application framework that leverages the power of a shared event emitter, a simple plugin pattern, and a flexible configuration engine.
var app = require'cantina'createApp;// Boot the application// --------------------// 1. Locates your application root directory (so plugins can reference it).// 2. Creates an `etc` configuration object and loads configuration from a// variety of default sources.// 3. Loads default core plugin(s): utilsappboot// Handle load errors.if err return console.logerr;// Optionally, add default configuration.// (a better practice is to put your configuration in `./etc/conf.json`)appconfaddhttp:host: 'localhost'port: 8080static:path: './public'myplugin:time: 5000;// Handle errors.appon'error'// Save the error to your logs or something.;// Load plugins// ------------// To load a 'plugin', use app.require().//// For example, load the cantina-web plugins like so:apprequire'cantina-web';// Loaders// -------// Cantina provides an api for registering and calling 'loader' methods.// 'Loaders' generally require modules from a folder and make them available// in some way. For example, cantina provides a 'plugins' loader that loads// all the modules in the 'plugins/' directory.appload'plugins';// Start the application// ---------------------// 1. Runs all 'start' hooks asynchronously, in series.// 2. Runs all 'started' hooks asynchronously, in parallel.// 3. Optionally, you can respond to initialization errors with a callback.appstart;;
Cantina plugins get access to the
app object and can extend or use apis attached to it.
Plugins can really do whatever they want, however, there are a few conventions
that can be followed in order to cooperate with the application initialization
// Add some default configuration options.appconfaddsquare:color: 'red'height: 200circle:color: 'blue'radius: 4;// Expose data or an API on the app.appshapes =squares:circles:;// Bind to application events, such as 'error', or custom ones that your// application uses.appon'create:circle'var defaults = appconfget'circle';var circle =color: optionscolor || defaultscolorradius: optionsradius || defaultsradius;appshapescirclespushcircle;;// Add a 'start' hook.// Hooks run asynchronously, so if you setup requires hitting a database or doing// other asynchronous work, you should do that here.apphook'start'addappdbloadCirclesif err return nexterr;circlesforEachappemit'create:circle' circle;;next;;;// Add a 'destroy' hook.apphook'destroy'add// Clean-up if the app is destroyed.next;;;
An important function of Cantina is to centralize your app's configuration.
Cantina delegates to node-etc
to handle many different configuration sources. When you call
following sources will be automatically checked and loaded (by order of
- argv - Command-line arguments parsed by optimist.
- env - Environment variables that match the prefix: 'app_'
- ./etc/ - JSON, JS, and YAML files in
[app root]/etcwill be parsed and added to the config. If the filename is
config.*then the contents will be merged in at the root level of the config. Any other files are assumed to be plugin specific and will be merged into conf keyed by filename.
- package.json - If your package.json contains an
etckey it will be merged into the conf.
app.boot() has finished, you can add more configuration either in your
application or in plugins via
app.conf.set, or any other
means of adding configuration that etc exposes.
Most applications should just store their configuration in
./etc and rely
on plugin defaults and argv for the rest.
Events and hooks should be your go-to solutions for organizing and implementing
application logic. Use
app.emit() when you want to deal with
app.hook() exposes an api for registering asynchronous
tasks. It is powered by stact-hooks.
Developed by Terra Eclipse
Terra Eclipse, Inc. is a nationally recognized political technology and strategy firm located in Santa Cruz, CA and Washington, D.C.