apiController.js 6.1 KB

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