apiController.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. var debug = require('debug')('ylt:server');
  2. var ylt = require('../../index');
  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. partialResult: req.body.partialResult || null
  20. }
  21. };
  22. // Add test to the testQueue
  23. debug('Adding test %s to the queue', run.runId);
  24. var queuePromise = queue.push(run.runId);
  25. // Save the run to the datastore
  26. runsDatastore.add(run, queuePromise.startingPosition);
  27. // Listening for position updates
  28. queuePromise.progress(function(position) {
  29. runsDatastore.updatePosition(run.runId, position);
  30. });
  31. // Let's start the run
  32. queuePromise.then(function() {
  33. runsDatastore.updatePosition(run.runId, 0);
  34. debug('Launching test %s on %s', run.runId, run.params.url);
  35. ylt(run.params.url)
  36. .then(function(data) {
  37. debug('Success');
  38. // Save result in datastore
  39. data.runId = run.runId;
  40. resultsDatastore.saveResult(data)
  41. .then(function() {
  42. runsDatastore.markAsComplete(run.runId);
  43. // Send result if the user was waiting
  44. if (run.params.waitForResponse) {
  45. // If the user only wants a portion of the result (partialResult option)
  46. switch(run.params.partialResult) {
  47. case 'generalScores':
  48. res.redirect(302, '/api/results/' + run.runId + '/generalScores');
  49. break;
  50. case 'rules':
  51. res.redirect(302, '/api/results/' + run.runId + '/rules');
  52. break;
  53. case 'javascriptExecutionTree':
  54. res.redirect(302, '/api/results/' + run.runId + '/javascriptExecutionTree');
  55. break;
  56. case 'phantomas':
  57. res.redirect(302, '/api/results/' + run.runId + '/toolsResults/phantomas');
  58. break;
  59. default:
  60. res.redirect(302, '/api/results/' + run.runId);
  61. }
  62. }
  63. })
  64. .fail(function(err) {
  65. debug('Saving results to resultsDatastore failed:');
  66. debug(err);
  67. res.status(500).send('Saving results failed');
  68. });
  69. })
  70. .fail(function(err) {
  71. console.error('Test failed for URL: %s', run.params.url);
  72. console.error(err.toString());
  73. runsDatastore.markAsFailed(run.runId, err.toString());
  74. res.status(400).send('Bad request');
  75. })
  76. .finally(function() {
  77. queue.remove(run.runId);
  78. });
  79. }).fail(function(err) {
  80. console.error('Error or YLT\'s core instanciation');
  81. console.error(err);
  82. console.error(err.stack);
  83. });
  84. // The user doesn't not want to wait for the response, sending the run ID only
  85. if (!run.params.waitForResponse) {
  86. console.log('Sending response without waiting.');
  87. res.setHeader('Content-Type', 'application/json');
  88. res.send(JSON.stringify({runId: run.runId}));
  89. }
  90. });
  91. // Retrive one run by id
  92. app.get('/api/runs/:id', function(req, res) {
  93. var runId = req.params.id;
  94. var run = runsDatastore.get(runId);
  95. if (run) {
  96. res.setHeader('Content-Type', 'application/json');
  97. res.send(JSON.stringify(run, null, 2));
  98. } else {
  99. res.status(404).send('Not found');
  100. }
  101. });
  102. // Retrieve the list of all runs
  103. /*app.get('/api/runs', function(req, res) {
  104. // NOT YET
  105. });*/
  106. // Delete one run by id
  107. /*app.delete('/api/runs/:id', function(req, res) {
  108. deleteRun()
  109. });*/
  110. // Delete all
  111. /*app.delete('/api/runs', function(req, res) {
  112. purgeRuns()
  113. });
  114. // List all
  115. app.get('/api/runs', function(req, res) {
  116. listRuns()
  117. });
  118. // Exists
  119. app.head('/api/runs/:id', function(req, res) {
  120. existsX();
  121. // Returns 200 if the result exists or 404 if not
  122. });
  123. */
  124. // Retrive one result by id
  125. app.get('/api/results/:id', function(req, res) {
  126. getPartialResults(req.params.id, res, function(data) {
  127. return data;
  128. });
  129. });
  130. // Retrieve one result and return only the generalScores part of the response
  131. app.get('/api/results/:id/generalScores', function(req, res) {
  132. getPartialResults(req.params.id, res, function(data) {
  133. return data.scoreProfiles.generic;
  134. });
  135. });
  136. app.get('/api/results/:id/generalScores/:scoreProfile', function(req, res) {
  137. getPartialResults(req.params.id, res, function(data) {
  138. return data.scoreProfiles[req.params.scoreProfile];
  139. });
  140. });
  141. app.get('/api/results/:id/rules', function(req, res) {
  142. getPartialResults(req.params.id, res, function(data) {
  143. return data.rules;
  144. });
  145. });
  146. app.get('/api/results/:id/javascriptExecutionTree', function(req, res) {
  147. getPartialResults(req.params.id, res, function(data) {
  148. return data.javascriptExecutionTree;
  149. });
  150. });
  151. app.get('/api/results/:id/toolsResults/phantomas', function(req, res) {
  152. getPartialResults(req.params.id, res, function(data) {
  153. return data.toolsResults.phantomas;
  154. });
  155. });
  156. function getPartialResults(runId, res, partialGetterFn) {
  157. resultsDatastore.getResult(runId)
  158. .then(function(data) {
  159. var results = partialGetterFn(data);
  160. if (typeof results === 'undefined') {
  161. res.status(404).send('Not found');
  162. return;
  163. }
  164. res.setHeader('Content-Type', 'application/json');
  165. res.send(JSON.stringify(results, null, 2));
  166. }).fail(function() {
  167. res.status(404).send('Not found');
  168. });
  169. }
  170. };
  171. module.exports = ApiController;