apiController.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. // Retrieve the list of all runs
  12. /*app.get('/api/runs', function(req, res) {
  13. // NOT YET
  14. });*/
  15. // Create a new run
  16. app.post('/api/runs', function(req, res) {
  17. // Grab the test parameters and generate a random run ID
  18. var run = {
  19. runId: (Date.now()*1000 + Math.round(Math.random()*1000)).toString(36),
  20. params: {
  21. url: req.body.url,
  22. waitForResponse: req.body.waitForResponse !== false
  23. }
  24. };
  25. // Add test to the testQueue
  26. debug('Adding test %s to the queue', run.runId);
  27. var queuePromise = queue.push(run.runId);
  28. // Save the run to the datastore
  29. runsDatastore.add(run, queuePromise.startingPosition);
  30. // Listening for position updates
  31. queuePromise.progress(function(position) {
  32. runsDatastore.updatePosition(run.runId, position);
  33. });
  34. // Let's start the run
  35. queuePromise.then(function() {
  36. runsDatastore.updatePosition(run.runId, 0);
  37. debug('Launching test %s on %s', run.runId, run.params.url);
  38. new YellowLabTools(run.params.url)
  39. .then(function(data) {
  40. debug('Success');
  41. runsDatastore.markAsComplete(run.runId);
  42. // Save result in datastore
  43. data.runId = run.runId;
  44. resultsDatastore.saveResult(data);
  45. // Send result if the user was waiting
  46. if (run.params.waitForResponse) {
  47. res.redirect(302, '/api/results/' + run.runId);
  48. }
  49. }).fail(function(err) {
  50. console.error('Test failed for %s', run.params.url);
  51. console.error(err);
  52. console.error(err.stack);
  53. runsDatastore.markAsFailed(run.runId);
  54. }).finally(function() {
  55. queue.remove(run.runId);
  56. });
  57. }).fail(function(err) {
  58. console.error('Error or YLT\'s core instanciation');
  59. console.error(err);
  60. console.error(err.stack);
  61. });
  62. // The user doesn't not want to wait for the response, sending the run ID only
  63. if (!run.params.waitForResponse) {
  64. console.log('Sending response without waiting.');
  65. res.setHeader('Content-Type', 'application/json');
  66. res.send(JSON.stringify({runId: run.runId}));
  67. }
  68. });
  69. // Retrive one run by id
  70. app.get('/api/runs/:id', function(req, res) {
  71. var runId = req.params.id;
  72. var run = runsDatastore.get(runId);
  73. if (run) {
  74. res.setHeader('Content-Type', 'application/json');
  75. res.send(JSON.stringify(run, null, 2));
  76. } else {
  77. res.status(404).send('Not found');
  78. }
  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. // Retourne 200 si existe ou 404 si n'existe pas
  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;