From 8558b9414ab0748e81d4414e7d2799e7f4f2aafc Mon Sep 17 00:00:00 2001 From: Paul Vorbach Date: Mon, 27 Feb 2012 00:41:06 +0100 Subject: [PATCH] Update binary --- bin/api.js | 58 ++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 39 insertions(+), 19 deletions(-) diff --git a/bin/api.js b/bin/api.js index 0769156c8..1734ffe78 100644 --- a/bin/api.js +++ b/bin/api.js @@ -5,15 +5,6 @@ var fs = require('fs'); var path = require('path'); var ejs = require('ejs'); -function die(log, err) { - // ignore errors when closing log file - try { - log.closeSync(log); - } catch (err) {} - - throw err; -} - confdir(process.cwd(), 'conf', function (err, confdir) { if (err) throw err; @@ -27,17 +18,33 @@ confdir(process.cwd(), 'conf', function (err, confdir) { conf.root = path.resolve(confdir, '..'); - var log = fs.createWriteStream(path.resolve(conf.root, conf.logFile), - { flags: 'w' }); + // log file stream + var logFile = path.resolve(conf.root, conf.logFile); + var mode = 'w'; // create file by default + // if file already exists, open it + if (path.existsSync(logFile)) + mode = 'r+'; + + // open log file stream + var log = fs.createWriteStream(logFile, { flags: mode }); + var app = new require('api')(conf.protocol).Server(); + function die(err) { + // ignore errors when closing log file + try { + log.closeSync(log); + } catch (err) {} + throw err; + } + // error handling app.error = function error(code, req, resp) { resp.writeHead(code, { 'Content-Type': 'text/html' }); fs.readFile(path.resolve(conf.root, conf.directories.templates, code+'.tpl'), 'uft8', function (err, tpl) { if (err) - die(err); + return die(err); resp.end(ejs.render(tpl, { locals: { code: code, request: req } })); log.write('error: '+code+' '+req.url+'\n'); @@ -53,7 +60,7 @@ confdir(process.cwd(), 'conf', function (err, confdir) { fs.readFile(path.resolve(confdir, mod+'.json'), 'utf8', function (err, data) { if (err) - die(err); + return die(err); var modConf = JSON.parse(data); @@ -63,13 +70,26 @@ confdir(process.cwd(), 'conf', function (err, confdir) { }); }); - app.listen(conf.socket.path, function () { - fs.chmod(conf.socket.path, conf.socket.permissions, function (err) { + if (conf.connection.port) + if (conf.connection.hostname) + app.listen(conf.connection.port, conf.connection.hostname, listen); + else + app.listen(conf.connection.port, listen); + else + app.listen(conf.connection.path, listen); + + function listen() { + if (conf.connection.path) + fs.chmod(conf.connection.path, conf.connection.permissions, listening); + else + listening(); + + function listening(err) { if (err) - die(err); + return die(err); - console.log('Server listening on '+socket+'.'); - }); - }); + console.log('Server up and running.'); + } + } }); });