rest-now
Version:
Start it running, dump some HTML/Bootstrap in the public dir, and you've got a webapp.
287 lines (246 loc) • 7.71 kB
JavaScript
// Generated by CoffeeScript 1.3.3
(function() {
var api, collection, collections, create, dbhost, dbname, dbport, document, errors, files, find, fs, mongo, newConnection, node_static, nostrict, notAPI, query, remove, removeAll, restify, root, server, static_server, strict, update;
restify = require('restify');
node_static = require('node-static');
mongo = require('mongodb');
fs = require('fs');
static_server = null;
newConnection = function(options) {
return new mongo.Db(dbname, new mongo.Server(dbhost, dbport, {
auto_reconnect: true
}), options);
};
strict = function(func) {
return function(req, res, next) {
return newConnection({
strict: true
}).open(function(err, db) {
if (err != null) {
return res.send(500, 'could not connect to database ' + dbname);
}
return func(req, res, next, db);
});
};
};
nostrict = function(func) {
return function(req, res, next) {
return newConnection({}).open(function(err, db) {
if (err != null) {
return res.send(500, 'could not connect to database ' + dbname);
}
return func(req, res, next, db);
});
};
};
server = restify.createServer({
name: 'teamconnect'
});
server.use(restify.queryParser({
mapParams: false
}));
server.use(restify.bodyParser({
mapParams: false
}));
api = function(func) {
return function(req, res, next) {
if (req.header('Accept').indexOf('application/json') >= 0) {
func(req, res, next);
next(false);
}
return next();
};
};
errors = {
collectionNotFound: function(coll) {
return '/' + coll + ' could not be found';
},
docNotFound: function(coll, pk) {
return '/' + coll + '/' + pk + ' could not be found';
}
};
find = function(db, coll, query, res, limit, skip) {
return db.collection(coll, function(err, c) {
var cursor, result;
if (err != null) {
db.close();
return res.send(404, errors.collectionNotFound(coll));
}
result = [];
cursor = c.find(query);
if (skip != null) {
cursor.skip(skip);
}
if (limit != null) {
cursor.limit(limit);
}
cursor.toArray(function(err, docs) {
return res.json(200, docs.map(function(e, i, arr) {
return {
href: '/' + coll + '/' + e._id.toHexString()
};
}));
});
return db.close();
});
};
collections = api(strict(function(req, res, next, db) {
return db.collectionNames(function(err, names) {
res.json(200, names.filter(function(e) {
return e.name.split('.')[1] !== 'system';
}).map(function(e, i, arr) {
return {
href: '/' + e.name.split('.')[1]
};
}));
return db.close();
});
}));
collection = api(strict(function(req, res, next, db) {
var limit, skip;
limit = parseInt(req.query.limit);
skip = parseInt(req.query.skip);
delete req.query.limit;
delete req.query.skip;
return find(db, req.params.collection, req.query, res, limit, skip);
}));
query = api(strict(function(req, res, next, db) {
return find(db, req.params.collection, req.body, res, parseInt(req.query.limit), parseInt(req.query.skip));
}));
document = api(strict(function(req, res, next, db) {
return db.collection(req.params.collection, function(err, c) {
if (err != null) {
db.close();
return res.send(404, errors.collectionNotFound(req.params.collection));
}
c.findOne({
_id: mongo.BSONPure.ObjectID.createFromHexString(req.params.pk)
}, function(err, doc) {
if (err != null) {
db.close();
return res.send(404, errors.docNotFound(req.params.collection, req.params.pk));
}
return res.json(200, doc);
});
return db.close();
});
}));
files = function(req, res, next) {
if (req.header('Accept').indexOf('application/json') === -1) {
static_server.serve(req, res, next);
next(false);
}
return next();
};
create = api(nostrict(function(req, res, next, db) {
return db.collection(req.params.collection, function(err, c) {
if (err != null) {
db.close();
return res.send(err);
}
return c.insert(req.body, {
safe: true
}, function(err, result) {
res.json(201, result.length === 1 ? result[0] : result);
return db.close();
});
});
}));
update = api(nostrict(function(req, res, next, db) {
return db.collection(req.params.collection, function(err, c) {
var id;
if (err != null) {
db.close();
return res.send(err);
}
update = req.query.overwrite ? req.body : {
$set: req.body
};
id = mongo.BSONPure.ObjectID.createFromHexString(req.params.pk);
return c.update({
_id: id
}, update, {
safe: true
}, function(err, count) {
db.close();
if (err != null) {
return res.send(err);
}
return document(req, res, next);
});
});
}));
remove = api(nostrict(function(req, res, next, db) {
return db.collection(req.params.collection, function(err, c) {
if (err != null) {
db.close();
return res.send(404, errors.collectionNotFound(req.params.collection));
}
c.remove({
_id: mongo.BSONPure.ObjectID.createFromHexString(req.params.pk)
}, function(err, doc) {
if (err != null) {
db.close();
return res.send(404, errors.docNotFound(req.params.collection, req.params.pk));
}
return res.send(200, '/' + req.params.collection + '/' + req.params.pk + ' deleted');
});
return db.close();
});
}));
removeAll = api(nostrict(function(req, res, next, db) {
db.collection(req.params.collection, function(err, c) {
if (err != null) {
db.close();
return res.send(404, errors.collectionNotFound(req.params.collection));
}
c.remove({});
return res.send(200, 'all records from /' + req.params.collection + ' deleted');
});
return db.close();
}));
notAPI = function(req, res, next) {
if (req.header('Accept').indexOf('application/json') === -1) {
res.send(406, "Must have explicit Accept: application/json to access api.");
return next(false);
}
};
server.get('/:collection', collection, files);
server.post('/:collection/query', query, files);
server.get('/:collection/:pk', document, files);
server.get(/^\/.*/, collections, files);
server.post('/:collection', create, notAPI);
server.put('/:collection/:pk', update, notAPI);
server.del('/:collection', removeAll, notAPI);
server.del('/:collection/:pk', remove, notAPI);
root = null;
try {
fs.lstatSync('./public');
root = './public';
} catch (err) {
root = './';
}
dbname = 'test';
dbport = 27017;
dbhost = '127.0.0.1';
exports.start = function(port, path, db, dbserver) {
var p, _ref;
p = port != null ? port : 8000;
if (root == null) {
root = path;
}
if (dbname == null) {
dbname = db;
}
if (dbserver != null) {
_ref = dbserver.split(':'), dbhost = _ref[0], dbport = _ref[1];
}
dbport = parseInt(dbport);
static_server = new node_static.Server(root);
return server.listen(p, function() {
console.log('Server listening on port ' + p);
return console.log('db host: ' + dbhost + ' port: ' + dbport + ' name: ' + dbname);
});
};
return exports;
}).call(this);