frontController.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. var path = require('path');
  2. var express = require('express');
  3. var serverSettings = (process.env.IS_TEST) ? require('../../../test/fixtures/settings.json') : require('../../../server_config/settings.json');
  4. var FrontController = function(app) {
  5. 'use strict';
  6. var cacheDuration = 365 * 24 * 60 * 60 * 1000; // One year
  7. var assetsPath = (app.get('env') === 'development') ? '../../../front/src' : '../../../front/build';
  8. // Routes templating
  9. var routes = ['/', '/about', '/result/:runId', '/result/:runId/timeline', '/result/:runId/screenshot', '/result/:runId/rule/:policy', '/queue/:runId'];
  10. routes.forEach(function(route) {
  11. app.get(route, function(req, res) {
  12. res.setHeader('Cache-Control', 'public, max-age=20');
  13. res.render(path.join(__dirname, assetsPath, 'main.html'), {
  14. baseUrl: app.locals.baseUrl || '/',
  15. sponsoring: serverSettings.sponsoring || {}
  16. });
  17. });
  18. });
  19. // Views templating
  20. app.get('/views/:viewName', function(req, res) {
  21. res.setHeader('Cache-Control', 'public, max-age=' + cacheDuration);
  22. res.render(path.join(__dirname, assetsPath, 'views/' + req.params.viewName), {
  23. baseUrl: app.locals.baseUrl || '/',
  24. sponsoring: serverSettings.sponsoring || {}
  25. });
  26. });
  27. // Static assets
  28. app.use('/css', express.static(path.join(__dirname, assetsPath, 'css'), { maxAge: cacheDuration }));
  29. app.use('/fonts', express.static(path.join(__dirname, assetsPath, 'fonts'), { maxAge: cacheDuration }));
  30. app.use('/img', express.static(path.join(__dirname, assetsPath, 'img'), { maxAge: cacheDuration }));
  31. app.use('/js', express.static(path.join(__dirname, assetsPath, 'js'), { maxAge: cacheDuration }));
  32. app.use('/node_modules', express.static(path.join(__dirname, '../../../node_modules'), { maxAge: cacheDuration }));
  33. };
  34. module.exports = FrontController;