apiController.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. var debug = require('debug')('ylt:server');
  2. var YellowLabTools = require('../../yellowlabtools');
  3. var RunsQueue = require('../datastores/runsQueue');
  4. var RunsDatastore = require('../datastores/runsDatastore');
  5. var ResultsDatastore = require('../datastores/resultsDatastore');
  6. var ApiController = function(app) {
  7. 'use strict';
  8. var queue = new RunsQueue();
  9. var runsDatastore = new RunsDatastore();
  10. var resultsDatastore = new ResultsDatastore();
  11. // Create a new run
  12. app.post('/api/runs', function(req, res) {
  13. // Grab the test parameters and generate a random run ID
  14. var run = {
  15. runId: (Date.now()*1000 + Math.round(Math.random()*1000)).toString(36),
  16. params: {
  17. url: req.body.url,
  18. waitForResponse: req.body.waitForResponse !== false
  19. }
  20. };
  21. // Add test to the testQueue
  22. debug('Adding test %s to the queue', run.runId);
  23. var queuePromise = queue.push(run.runId);
  24. // Save the run to the datastore
  25. runsDatastore.add(run, queuePromise.startingPosition);
  26. // Listening for position updates
  27. queuePromise.progress(function(position) {
  28. runsDatastore.updatePosition(run.runId, position);
  29. });
  30. // Let's start the run
  31. queuePromise.then(function() {
  32. runsDatastore.updatePosition(run.runId, 0);
  33. debug('Launching test %s on %s', run.runId, run.params.url);
  34. new YellowLabTools(run.params.url)
  35. .then(function(data) {
  36. debug('Success');
  37. runsDatastore.markAsComplete(run.runId);
  38. // Save result in datastore
  39. data.runId = run.runId;
  40. resultsDatastore.saveResult(data);
  41. // Send result if the user was waiting
  42. if (run.params.waitForResponse) {
  43. res.redirect(302, '/api/results/' + run.runId);
  44. }
  45. }).fail(function(err) {
  46. console.error('Test failed for %s', run.params.url);
  47. console.error(err);
  48. console.error(err.stack);
  49. runsDatastore.markAsFailed(run.runId);
  50. }).finally(function() {
  51. queue.remove(run.runId);
  52. });
  53. }).fail(function(err) {
  54. console.error('Error or YLT\'s core instanciation');
  55. console.error(err);
  56. console.error(err.stack);
  57. });
  58. // The user doesn't not want to wait for the response, sending the run ID only
  59. if (!run.params.waitForResponse) {
  60. console.log('Sending response without waiting.');
  61. res.setHeader('Content-Type', 'application/json');
  62. res.send(JSON.stringify({runId: run.runId}));
  63. }
  64. });
  65. // Retrive one run by id
  66. app.get('/api/runs/:id', function(req, res) {
  67. var runId = req.params.id;
  68. var run = runsDatastore.get(runId);
  69. if (run) {
  70. res.setHeader('Content-Type', 'application/json');
  71. res.send(JSON.stringify(run, null, 2));
  72. } else {
  73. res.status(404).send('Not found');
  74. }
  75. });
  76. // Retrieve the list of all runs
  77. /*app.get('/api/runs', function(req, res) {
  78. // NOT YET
  79. });*/
  80. // Delete one run by id
  81. /*app.delete('/api/runs/:id', function(req, res) {
  82. deleteRun()
  83. });*/
  84. // Delete all
  85. /*app.delete('/api/runs', function(req, res) {
  86. purgeRuns()
  87. });
  88. // List all
  89. app.get('/api/runs', function(req, res) {
  90. listRuns()
  91. });
  92. // Exists
  93. app.head('/api/runs/:id', function(req, res) {
  94. existsX();
  95. // Returns 200 if the result exists or 404 if not
  96. });
  97. */
  98. // Retrive one result by id
  99. app.get('/api/results/:id', function(req, res) {
  100. var runId = req.params.id;
  101. resultsDatastore.getResult(runId)
  102. .then(function(data) {
  103. // This is the pivot format, we might need to clean it first?
  104. // Hide phantomas results
  105. data.toolsResults.phantomas = {};
  106. res.setHeader('Content-Type', 'application/json');
  107. res.send(JSON.stringify(data, null, 2));
  108. }).fail(function() {
  109. res.status(404).send('Not found');
  110. });
  111. });
  112. };
  113. module.exports = ApiController;