Skip to content

Commit

Permalink
First draft of api command line tool
Browse files Browse the repository at this point in the history
pvorb committed Feb 17, 2012

Partially verified

This commit is signed with the committer’s verified signature. The key has expired.
danielabrozzoni’s contribution has been verified via GPG key.
We cannot verify signatures from co-authors, and some of the co-authors attributed to this commit require their commits to be signed.
1 parent a8ec03c commit ab0842f
Showing 2 changed files with 82 additions and 2 deletions.
71 changes: 71 additions & 0 deletions bin/api.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#!/usr/bin/env node

var confdir = require('confdir');
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;

fs.readFile(path.resolve(confdir, 'api.json'), 'uft8', function (err, conf) {
conf = JSON.parse(conf);

conf.root = path.resolve(confdir, '..');

var log = fs.createWriteStream(path.resolve(conf.root, conf.logFile),
{ flags: 'w' });
var app = new require('api')(conf.protocol).Server();

// 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);

resp.end(ejs.render(tpl, { locals: { code: code, request: req } }));
log.write('error: '+code+' '+req.url+'\n');
});
});

// for every registered module
conf.modules.forEach(function (mod) {
var module = require(path.resolve(conf.root, conf.directories.modules,
mod+'.js')

// read module specific configuration file
fs.readFile(path.resolve(confdir, mod+'.json'), 'utf8',
function (err, data) {
if (err)
die(err);

var modConf = JSON.parse(data);

// hook module into app
module(app, log, modConf, conf);
console.log('Module '+mod+' up and running.');
});
});

app.listen(conf.socket.path, function () {
fs.chmod(conf.socket.path, conf.socket.permissions, function (err) {
if (err)
die(err);

console.log('Server listening on '+socket+'.');
});
});
});
});
13 changes: 11 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -11,8 +11,17 @@
"bugs": {
"url": "https://github.com/pvorb/node-api/issues"
},
"main": "api.js",
"main": "./api.js",
"bin": {
"api": "./bin/api.js"
},
"engines": {
"node": ">=0.4.0"
}
},
"licenses": [
{
"type": "MIT",
"url": "http://vorb.de/license/mit.html"
}
]
}

0 comments on commit ab0842f

Please sign in to comment.