service.js | |
---|---|
/*
* service.js: RESTful JSON-based web service for the drone module.
*
* (C) 2010, Nodejitsu Inc.
*
*/
var journey = require('journey'),
haibu = require('haibu'); | |
function createRouter (dron, logger)@drone {Drone} Instance of the Drone resource to use in this router.Creates the Journey router which represents the | exports.createRouter = function (drone) { |
TODO (indexzero): Setup token-based auth for Drone API servers Create the Router instance. | var router = new(journey.Router)({
strict: false,
strictUrls: false,
api: 'basic'
}); |
Default Root
| router.root.bind(function (response) {
response.send(400, {}, { message: 'No drones specified' });
}); |
Version Binding
TODO (indexzero): Consume | router.get(/\/version[\/]?/).bind(function (response) {
response.send(200, {}, { version: journey.version.join('.') });
});
router.path('/config', haibu.config.addRoutes()); |
Drones ResourceRoutes for RESTful access to the Drone resource. | router.path('/drones', function () { |
List Drones
| this.get().bind(function (response) {
var data = { drones: drone.list() };
response.send(200, {}, data);
}); |
Show Drone
| this.get(/\/([\w|\-]+)/).bind(function (response, id) {
var data = drone.show(id);
if (typeof data === 'undefined') {
response.send(404, {}, { message: 'No drone(s) found for application ' + id });
}
else {
response.send(200, {}, data);
}
}); |
Start Drone
| this.post(/\/([\w|\-]+)\/start/).bind(function (response, id, data) {
drone.start(data.start, function (err, result) {
if (err) {
return response.send(500, {}, { error: err });
}
response.send(200, {}, { drone: result.drone });
});
}); |
Stop Drone
| this.post(/\/([\w|\-]+)\/stop/).bind(function (response, id, data) {
drone.stop(data.stop.name, function (err, result) {
if (err || !result) {
return response.send(500, {}, { error: err || new Error('Unknown error from drone.') });
}
response.send(200);
});
}); |
Restart Drone
| this.post(/\/([\w|\-]+)\/restart/).bind(function (response, id, data) {
drone.restart(data.restart.name, function (err, drones) {
if (err) {
return response.send(500, {}, { error: err });
}
response.send(200, {}, { drones: drones });
});
});
|
Clean Drone
| this.post(/\/([\w|\-]+)\/clean/).bind(function (response, id, data) {
drone.clean(data, function (err, drones) {
if (err) {
return response.send(500, {}, { error: err });
}
response.send(200, {}, { clean: true });
});
});
});
return router;
};
|