sandboxApp.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /* global __dirname */
  2. /* jshint strict:false */
  3. "use strict";
  4. var express = require('express');
  5. var bodyParser = require('body-parser');
  6. var slug = require('slug');
  7. var beautify = require("json-beautify");
  8. var _ = require('lodash');
  9. var path = require('path');
  10. var Mustache = require('mustache');
  11. var app = express();
  12. var fs = require('fs');
  13. var Promise = require('bluebird'); Promise.promisifyAll(fs);
  14. var sandboxTpml = fs.readFileSync(__dirname + '/html/template.mustache.html').toString();
  15. var ExampleStore = require('./lib/ExampleStore');
  16. var examplesDir = __dirname + '/exemples';
  17. // var examplesDir = __dirname + '/test/integration/test-examples';
  18. var exStore = new ExampleStore(examplesDir);
  19. var {
  20. readFileAsync,
  21. readFilesAsync
  22. } = require('./lib/fsio');
  23. var {
  24. getAcceptLanguage,
  25. getIndexBare,
  26. getIndexRepo,
  27. getIndexExample
  28. } = require('./lib/indexHandlers')(exStore, examplesDir);
  29. /**
  30. * Initialize example store
  31. */
  32. exStore.init();
  33. // .then(() => console.log(exStore.getMenu()));
  34. /**
  35. * Initialize Express app: root folder as static, body parsers
  36. */
  37. app.use(express.static(__dirname));
  38. app.use(bodyParser.json());
  39. app.use(bodyParser.urlencoded({ extended: true }));
  40. app.use(getAcceptLanguage);
  41. function addExample(slug, title) {
  42. examples.push({ slug: slug, title: title });
  43. return fs.writeFileAsync(examplesJSON, beautify(examples, null, 2, 100));
  44. }
  45. function readConfigJson(exampleSlug) {
  46. console.log(exampleSlug);
  47. return require('./exemples/jquery/' + exampleSlug + '/config.json');
  48. }
  49. /**
  50. * Index page: render with only repo list in menu
  51. */
  52. app.get('/', getIndexBare);
  53. /**
  54. * Repo page: render with repo list and selected repo's example list in menu
  55. */
  56. app.get('/:repoSlug', getIndexRepo);
  57. /**
  58. * Example page: render with repo list and selected repo's example list in menu,
  59. * and the editor with the selected example
  60. */
  61. app.get('/:repoSlug/:exampleSlug', getIndexExample);
  62. app.post('/examples', function(req, res) {
  63. var title = req.body.title;
  64. if(! req.body.title) {
  65. res.status(400).send('Le titre ne peut pas être vide !');
  66. }
  67. var existingTitle = _.find(examples, { title: title });
  68. if(existingTitle) {
  69. res.status(400).send("L'exemple '" + title + "' existe déjà !");
  70. }
  71. var exampleSlug = slug(req.body.title.toLowerCase());
  72. var targetDir = __dirname + '/exemples/' + exampleSlug;
  73. fs.mkdirAsync(targetDir)
  74. .then(() => Promise.map(
  75. ['contenu.html', 'script.js'], f => fs.writeFileAsync(targetDir + '/' + f, '')
  76. ))
  77. .then(files => addExample(exampleSlug, title))
  78. .then(() => res.json({ slug: exampleSlug, title: title }));
  79. });
  80. app.get('/examples/:slug', function(req, res) {
  81. const { slug } = req.params;
  82. const config = readConfigJson(slug);
  83. const { title, html, js, css, libsCss, libsJs } = config;
  84. readFileAsync(__dirname + '/exemples/' + slug + '/example.html')
  85. .then(body =>
  86. Mustache.render(sandboxTpml, { body, slug, title, js, css, libsCss, libsJs })
  87. )
  88. .then(html => res.send(html));
  89. });
  90. app.get('/menu', (rea, res) => {
  91. res.send(exStore.getMenu());
  92. });
  93. app.get('/list/:repoPath', function(req, res) {
  94. const { repoPath } = req.params;
  95. const repo = exStore.getList(repoPath);
  96. if(! repo) {
  97. return res.status(404).send('Repo ' + repoPath + ' not found');
  98. }
  99. console.log('found repo', repo);
  100. const data = repo.examples.map(e => (
  101. { slug: e.slug, title: e.title }
  102. ));
  103. res.json(data);
  104. });
  105. app.put('/examples/:slug', function(req, res) {
  106. var slug = req.params.slug;
  107. var existing = _.find(examples, { slug: slug });
  108. if(! existing) {
  109. res.status(404).send("L'exemple avec l'identifiant '" + slug + "' est introuvable !");
  110. }
  111. var targetDir = __dirname + '/exemples/' + slug;
  112. if(req.body.html) {
  113. fs.writeFileSync(targetDir + '/contenu.html', req.body.html);
  114. }
  115. if(req.body.javascript) {
  116. fs.writeFileSync(targetDir + '/script.js', req.body.javascript);
  117. }
  118. var theDate = new Date();
  119. console.log(theDate.getHours() + ':' + theDate.getMinutes() + " - Sauvegarde de l'exemple '" + existing.title + " effectuée'");
  120. res.json({ success: true });
  121. });
  122. module.exports = app;