little-spas.js

/*

The MIT License (MIT)
Copyright (c) 2012 Donovan Buck

Permission is hereby granted, free of charge, to any person obtaining a copy of this
software and associated documentation files (the "Software"), to deal in the Software
without restriction, including without limitation the rights to use, copy, modify,
merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to the following
conditions:

The above copyright notice and this permission notice shall be included in all copies
or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

*/

Module dependencies.

var 

Built in modules

    http = require('http')
  , url = require('url')
  , querystring = require('querystring')
  , fs = require('fs')
  , spawn = require('child_process').spawn
  

My modules

  , bundles = require('./bundles')
  , engine = require('./lib/engine')
  , oauth2 = require('./lib/oauth2')
  

Other Dependencies

  , nconf = require('nconf')
  , director = require('director')
  , _ = require('underscore')._
  , cronJob = require("cron").CronJob
;

Use nconf to grab commandline params and read config.json

nconf.argv().file({ file: 'config.json' });

If --dev is passed use the development config section

var config = nconf.get('dev') ? nconf.get('development') : nconf.get('live');

Run little-spas as a service

if (nconf.get('service')) {

Specify output and error log files

	var	out = fs.openSync('./logs/spasout.log', 'a'),
		err = fs.openSync('./logs/spaserr.log', 'a');
	

Spawn the process

	var spasService = spawn('node', ['little-spas', nconf.get('dev') ? '--dev' : ''], {
		detached: true,
		stdio: [ 'ignore', out, err ]
	});
	
	spasService.unref();
	
	console.log("little-spas is listening on port " + config.port);
	
	process.exit();
	
} else {

Our Routes

	var router = new director.http.Router({
		

This is the return route for authentication services

		'/auth': {
			get: function() {
				if (_.has(querystring.parse((url.parse(this.req.url).query)), 'code')) {
					oauth2.saveCode ( this.res, querystring.parse((url.parse(this.req.url).query)).state.split(','), querystring.parse((url.parse(this.req.url).query)).code);
				}
			}
		},
		

This route needs to be removed and changed to a command line call. Anybody could revoke creds right now

		'/revoke/:cred': {
			get: function(cred) {
				oauth2.revoke ( this.res, cred );
			}
		},
		

A bundle is being requested

		'/bundle/:bid': {
	    	get: function(bid) { 
	    		engine.fulfill ( this.res, bid, bundles[bid], querystring.parse((url.parse(this.req.url).query)).callback );
	    	}
		}
	});
	

Schedule jobs defined in bundles

	_.each(bundles, function (bundle, bid) {
		_.each(bundle, function (api, key) {
			if (api.schedule) {
				new cronJob(api.schedule, function(){
		    		engine.refresh(api, key, bid, bundle);  
		    	}, null, true);
		    }
		});
	});
	

Create our server

	var server = http.createServer(function (req, res) {
		router.dispatch(req, res, function (err) {
			if (err) {
				res.writeHead(404);
				res.end();
			}
		});
	});
	
	server.listen(config.port);

}