apiController.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. getPartialResults(req.params.id, res, function(data) {
  101. return data;
  102. });
  103. });
  104. // Retrieve one result and return only the generalScores part of the response
  105. app.get('/api/results/:id/generalScores', function(req, res) {
  106. getPartialResults(req.params.id, res, function(data) {
  107. return data.scoreProfiles.generic;
  108. });
  109. });
  110. app.get('/api/results/:id/generalScores/:scoreProfile', function(req, res) {
  111. getPartialResults(req.params.id, res, function(data) {
  112. return data.scoreProfiles[req.params.scoreProfile];
  113. });
  114. });
  115. app.get('/api/results/:id/rules', function(req, res) {
  116. getPartialResults(req.params.id, res, function(data) {
  117. return data.rules;
  118. });
  119. });
  120. app.get('/api/results/:id/javascriptExecutionTree', function(req, res) {
  121. getPartialResults(req.params.id, res, function(data) {
  122. return data.javascriptExecutionTree;
  123. });
  124. });
  125. app.get('/api/results/:id/toolsResults/phantomas', function(req, res) {
  126. getPartialResults(req.params.id, res, function(data) {
  127. return data.toolsResults.phantomas;
  128. });
  129. });
  130. function getPartialResults(runId, res, partialGetterFn) {
  131. resultsDatastore.getResult(runId)
  132. .then(function(data) {
  133. var results = partialGetterFn(data);
  134. if (typeof results === 'undefined') {
  135. res.status(404).send('Not found');
  136. return;
  137. }
  138. res.setHeader('Content-Type', 'application/json');
  139. res.send(JSON.stringify(results, null, 2));
  140. }).fail(function() {
  141. res.status(404).send('Not found');
  142. });
  143. }
  144. };
  145. module.exports = ApiController;