spincycle
Version:
A reactive message router and object manager that lets clients subscribe to object property changes on the server
211 lines (194 loc) • 6.93 kB
JavaScript
// Generated by CoffeeScript 1.12.6
(function() {
var HttpMethod, basicAuth, bodyParser, cookie, debug, url, uuid,
bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
uuid = require('node-uuid');
url = require('url');
basicAuth = require('basic-auth');
cookie = require('cookie');
bodyParser = require('body-parser');
debug = process.env["DEBUG"];
HttpMethod = (function() {
HttpMethod.httproutes = [];
HttpMethod.props = {};
function HttpMethod(messageRouter, app, basePath) {
this.makeRESTful = bind(this.makeRESTful, this);
var doSend;
this.app = app;
app.use(bodyParser.json());
this.restPath = '/rest/';
doSend = function(req, res, url_parts) {
var cookies, ip, message, p, part, port, target, user;
user = basicAuth(req);
if (user) {
console.log('basic auth user detected');
console.dir(user);
if (HttpMethod.props && HttpMethod.props.user && HttpMethod.props.user.name) {
if (HttpMethod.props.user.name !== user.name && HttpMethod.props.user.pass !== user.pass) {
console.log('wrong username or password provided');
res.json({
status: e.general.NOT_ALLOWED,
info: 'wrong basic auth username or pass.',
payload: {
error: 'Basic Auth Failure'
}
});
return;
}
}
}
ip = req.connection.remoteAddress;
port = req.connection.remotePort;
cookies = cookie.parse(req.headers.cookie || '');
target = HttpMethod.httproutes[req.query.target];
if (target) {
console.log('Express request from ' + ip + ':' + port + ' target is "' + req.query.target + '" cookies are ' + req.headers.cookie);
message = {
client: ip + ':' + port,
target: req.query.target,
messageId: url_parts.messageId || uuid.v4(),
sessionId: cookies.sid
};
for (p in url_parts) {
part = url_parts[p];
message[p] = part;
}
message.replyFunc = function(reply) {
if (reply.statuscode) {
res.status(reply.statuscode);
}
if (reply.status === 'FAILURE') {
res.status(404);
}
res.json(reply);
if (debug) {
return console.log('HttpMethod calling target ' + target);
}
};
return target(message);
} else {
return res.json({
error: 'target not found'
});
}
};
this.doSend = doSend;
app.get(basePath, function(req, res) {
var url_parts;
url_parts = req.query;
return doSend(req, res, url_parts);
});
app.post(basePath, function(req, res) {
var k, ref, url_parts, v;
url_parts = req.query;
ref = req.body;
for (k in ref) {
v = ref[k];
url_parts[k] = v;
}
return doSend(req, res, url_parts);
});
"app.put basePath, (req, res) ->\n if debug then console.log 'Alternate PUT handler. params are'\n if debug then console.dir req.params\n if debug then console.log 'query is'\n if debug then console.dir req.query\n url_parts = req.body\n doSend(req, res, url_parts)";
messageRouter.addMethod('express', this);
}
HttpMethod.prototype.registrationFunc = function(targetName, targetFunc, props) {
this.props = props;
return HttpMethod.httproutes[targetName] = targetFunc;
};
HttpMethod.prototype.makeRESTful = function(type) {
var createone, deleteone, getone, listall, updateone;
listall = (function(_this) {
return function(req, res) {
var url_parts;
url_parts = req.query;
req.query.type = type;
req.query.target = '_list' + type + 's';
return _this.doSend(req, res, url_parts);
};
})(this);
createone = (function(_this) {
return function(req, res) {
var url_parts;
url_parts = req.query;
req.query.type = type;
req.query.obj = {
type: req.query.type
};
req.query.target = '_create' + type;
return _this.doSend(req, res, url_parts);
};
})(this);
getone = (function(_this) {
return function(req, res) {
var url_parts;
url_parts = req.query;
req.query.id = req.params.id;
req.query.type = type;
req.query.target = '_get' + type;
return _this.doSend(req, res, url_parts);
};
})(this);
updateone = (function(_this) {
return function(req, res) {
var url_parts;
if (debug) {
console.log('PUT handler. params are');
}
if (debug) {
console.dir(req.params);
}
if (debug) {
console.log('query is');
}
if (debug) {
console.dir(req.query);
}
if (debug) {
console.log('body is');
}
if (debug) {
console.dir(req.body);
}
url_parts = req.body;
req.query.id = req.params.id;
url_parts.apitoken = req.query.apitoken;
url_parts.sessionId = req.query.sessionId;
req.query.type = type;
req.query.obj = req.body.obj;
if (typeof url_parts.obj === 'string') {
url_parts.obj = JSON.parse(url_parts.obj);
}
req.query.target = '_update' + type;
return _this.doSend(req, res, url_parts);
};
})(this);
deleteone = (function(_this) {
return function(req, res) {
var url_parts;
url_parts = req.query;
req.query.id = req.params.id;
req.query.type = type;
req.query.obj = {
id: req.query.id,
type: req.query.type
};
req.query.target = '_delete' + type;
return _this.doSend(req, res, url_parts);
};
})(this);
console.log('adding REST paths for "' + (this.restPath + type) + '"');
this.app.route(this.restPath + type).get(listall).post(createone);
this.app.route(this.restPath + type + '/:id').get(getone);
this.app.route(this.restPath + type + '/:id').put(updateone);
this.app.route(this.restPath + type + '/:id')["delete"](deleteone);
return this.app.get('/foo', function(req, res, next) {
return console.log('foo', function(req, res, next) {
return console.log('reject foo');
});
});
};
return HttpMethod;
})();
module.exports = HttpMethod;
}).call(this);
//# sourceMappingURL=HttpMethod.js.map