#!/usr/bin/env node var express = require('express') http = require('http') path = require('path') fs = require('fs') configs = process.argv.slice(2) usage = "Usage: hoster [[+]:] [[[+]:] ...]\n Where must conform to the hoster protocol" if (!configs.length) { console.log(usage); process.exit(1); } var _features = {}; console.log('* Working directory:', process.cwd()); /** Attach services to the current app @param services [ {mount: , handler: Function (app)}] ex: app.attach([{ mount: '/blog', handler:require('./blog.js') }, { handler: require('middleware1.js') }]); */ function attach(services) { var self = this; services.forEach(function (service) { if (service.mount) { // Middleware handler var mountApp = newApp(); mountApp.set('parent', self); var configurator = self.get('configurator'); if (configurator) configurator(service.mount, mountApp); service.handler(mountApp); if (service.mount.length) { mountApp.set('base', service.mount); console.log('* Mounting', service.handler.name || "(no-name)", 'middleware at', service.mount); self.use(service.mount, mountApp); } else { console.log('* Adding', service.handler.name || "(no-name)", 'middleware'); self.use(mountApp); } } else { // Configuring handler console.log('* Loading', service.handler.name || "(no-name)", 'service'); service.handler(self); } }); } // Default app settings function newApp() { var app = express() app.set('port', process.env.PORT || 3000); app.set('root', path.resolve(process.cwd())); app.set('base', ''); app.set('parent', null); app.set('views', app.get('root') + '/views'); app.set('configurator', function (mountPoint, app) { }); // Middeware configurator default: noop app.attach = attach; app.features = function (name, settings) { if (_features[name]) { console.log('* Applying feature', name, settings); _features[name](app, settings); } } return app; } function load(filename) { console.log('Load', filename); var firstLetter = filename.substr(0,1); if (firstLetter === '.' || firstLetter === '/') { return require(path.resolve(filename)); } return require(filename); } // Load services var services = []; configs.forEach(function (config) { var match = config.match(/^(.*):(.+)$/); if (match) { if (match[1].substr(0,1) === '+') { console.log('* Loading feature', match[1].substr(1)); _features[match[1].substr(1)] = load(match[2]); return; } services.push({ mount: match[1], handler: load(match[2]) }); return; } services.push({ handler: load(config) }); }); // Create base app var app = newApp(); app.attach(services); // Start server var server = http.createServer(app); server.listen(app.get('port'), function(){ console.log("* Server is listening on port", app.get('port')); });