Skip to content

Commit

Permalink
Update binary
Browse files Browse the repository at this point in the history
  • Loading branch information
pvorb committed Feb 26, 2012
1 parent 7282551 commit 8558b94
Showing 1 changed file with 39 additions and 19 deletions.
58 changes: 39 additions & 19 deletions bin/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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');
Expand All @@ -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);

Expand All @@ -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.');
}
}
});
});

0 comments on commit 8558b94

Please sign in to comment.