Meutel
0d0f564475
boostrap: start webserver, router catches requests routeur: create outputter and endpoint, trigger actions outputter: write in specified format action: act on resource
58 lines
1.3 KiB
JavaScript
Executable File
58 lines
1.3 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();
|
|
start(config);
|
|
});
|
|
|
|
/**
|
|
* Initialise application
|
|
*/
|
|
function start(config){
|
|
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);
|
|
}
|
|
|