apiController.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. // Save result in datastore
  38. data.runId = run.runId;
  39. resultsDatastore.saveResult(data)
  40. .then(function() {
  41. runsDatastore.markAsComplete(run.runId);
  42. // Send result if the user was waiting
  43. if (run.params.waitForResponse) {
  44. res.redirect(302, '/api/results/' + run.runId);
  45. }
  46. })
  47. .fail(function(err) {
  48. debug('Saving results to resultsDatastore failed:');
  49. debug(err);
  50. });
  51. }).fail(function(err) {
  52. console.error('Test failed for %s', run.params.url);
  53. console.error(err);
  54. console.error(err.stack);
  55. runsDatastore.markAsFailed(run.runId);
  56. }).finally(function() {
  57. queue.remove(run.runId);
  58. });
  59. }).fail(function(err) {
  60. console.error('Error or YLT\'s core instanciation');
  61. console.error(err);
  62. console.error(err.stack);
  63. });
  64. // The user doesn't not want to wait for the response, sending the run ID only
  65. if (!run.params.waitForResponse) {
  66. console.log('Sending response without waiting.');
  67. res.setHeader('Content-Type', 'application/json');
  68. res.send(JSON.stringify({runId: run.runId}));
  69. }
  70. });
  71. // Retrive one run by id
  72. app.get('/api/runs/:id', function(req, res) {
  73. var runId = req.params.id;
  74. var run = runsDatastore.get(runId);
  75. if (run) {
  76. res.setHeader('Content-Type', 'application/json');
  77. res.send(JSON.stringify(run, null, 2));
  78. } else {
  79. res.status(404).send('Not found');
  80. }
  81. });
  82. // Retrieve the list of all runs
  83. /*app.get('/api/runs', function(req, res) {
  84. // NOT YET
  85. });*/
  86. // Delete one run by id
  87. /*app.delete('/api/runs/:id', function(req, res) {
  88. deleteRun()
  89. });*/
  90. // Delete all
  91. /*app.delete('/api/runs', function(req, res) {
  92. purgeRuns()
  93. });
  94. // List all
  95. app.get('/api/runs', function(req, res) {
  96. listRuns()
  97. });
  98. // Exists
  99. app.head('/api/runs/:id', function(req, res) {
  100. existsX();
  101. // Returns 200 if the result exists or 404 if not
  102. });
  103. */
  104. // Retrive one result by id
  105. app.get('/api/results/:id', function(req, res) {
  106. getPartialResults(req.params.id, res, function(data) {
  107. return data;
  108. });
  109. });
  110. // Retrieve one result and return only the generalScores part of the response
  111. app.get('/api/results/:id/generalScores', function(req, res) {
  112. getPartialResults(req.params.id, res, function(data) {
  113. return data.scoreProfiles.generic;
  114. });
  115. });
  116. app.get('/api/results/:id/generalScores/:scoreProfile', function(req, res) {
  117. getPartialResults(req.params.id, res, function(data) {
  118. return data.scoreProfiles[req.params.scoreProfile];
  119. });
  120. });
  121. app.get('/api/results/:id/rules', function(req, res) {
  122. getPartialResults(req.params.id, res, function(data) {
  123. return data.rules;
  124. });
  125. });
  126. app.get('/api/results/:id/javascriptExecutionTree', function(req, res) {
  127. getPartialResults(req.params.id, res, function(data) {
  128. return data.javascriptExecutionTree;
  129. });
  130. });
  131. app.get('/api/results/:id/toolsResults/phantomas', function(req, res) {
  132. getPartialResults(req.params.id, res, function(data) {
  133. return data.toolsResults.phantomas;
  134. });
  135. });
  136. function getPartialResults(runId, res, partialGetterFn) {
  137. resultsDatastore.getResult(runId)
  138. .then(function(data) {
  139. var results = partialGetterFn(data);
  140. if (typeof results === 'undefined') {
  141. res.status(404).send('Not found');
  142. return;
  143. }
  144. res.setHeader('Content-Type', 'application/json');
  145. res.send(JSON.stringify(results, null, 2));
  146. }).fail(function() {
  147. res.status(404).send('Not found');
  148. });
  149. }
  150. };
  151. module.exports = ApiController;