bouquins/bin/bootstrap
2014-01-22 20:20:43 +01:00

67 lines
1.5 KiB
JavaScript
Executable File

#!/usr/bin/env nodejs
/**
* Bouquins bootstrap.
* TODO license
*
* Load configuration
* Check configuration
* Launch http server
* Initialise router
*/
APP_NAME='Bouquins';
DEFAULT_CONF='./config/config.json';
var bouquins = require('../lib/bouquins');
console.log('Bootstraping ' + APP_NAME);
// TODO argument conf file
var configfile = DEFAULT_CONF;
bouquins.loadconfig(configfile, function(err, config) {
if (err) {
console.error('Fatal error, cannot load config: ' + err);
console.log(err.stack);
process.exit(1);
}
// global config
GLOBAL.config = config;
// global logger
GLOBAL.logger = bouquins.initLogger();
logger.debug(config);
// database
bouquins.initDB(function(err) {
if (err) {
logger.error('Fatal error, cannot load database: ' + err);
logger.error(err.stack);
process.exit(1);
}
});
start();
});
/**
* Initialise application
*/
function start(){
logger.info('Starting '+APP_NAME);
var starReqListener=function(req, res) {
// defaut request listener
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end(APP_NAME + ' is starting');
}
// launch http server, wait config and router
var server = require('http').createServer(starReqListener)
.listen(config.httpPort);
logger.info('Listening on '+config.httpPort);
// TODO check config: files/dir do exist, database exist, can read ...
// init router
var router = bouquins.makeRouter();
server.on('request', function(req, resp) {
router.request(req, resp);
}).removeListener('request', starReqListener);
}