From d15543aec42be5892d1f2e99db06c84e055ba6a5 Mon Sep 17 00:00:00 2001 From: Joe Lanman Date: Tue, 17 Jul 2018 16:50:48 +0100 Subject: [PATCH] create v6 app to serve old prototype files from app/v6 --- lib/utils.js | 4 +-- server.js | 69 +++++++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 67 insertions(+), 6 deletions(-) diff --git a/lib/utils.js b/lib/utils.js index 2f78e5f8ec..8131c50b7a 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -195,13 +195,13 @@ exports.getLatestRelease = function () { // Try to match a request to a template, for example a request for /test // would look for /app/views/test.html // or /app/views/text/index.html -exports.matchRoutes = function (req, res) { +exports.matchRoutes = function (req, res, next) { var path = (req.params[0]) res.render(path, function (err, html) { if (err) { res.render(path + '/index', function (err2, html) { if (err2) { - res.status(404).send(err + '
' + err2) + next() } else { res.end(html) } diff --git a/server.js b/server.js index 0dd22d3c22..1125c7e568 100644 --- a/server.js +++ b/server.js @@ -18,8 +18,24 @@ const packageJson = require('./package.json') const routes = require('./app/routes.js') const utils = require('./lib/utils.js') +var useV6 = false +var v6App +var v6Routes + +try { + v6Routes = require('./app/v6/routes.js') + useV6 = true +} catch (e) { + // No routes.js in app/v6 so we can continue with useV6 false +} + const app = express() const documentationApp = express() + +if (useV6) { + v6App = express() +} + dotenv.config() // Set cookies for use in cookie banner. @@ -123,6 +139,32 @@ app.use(bodyParser.urlencoded({ extended: true })) +// Set up v6 app for backwards compatibility +if (useV6) { + var v6Views = [ + path.join(__dirname, '/node_modules/govuk_template_jinja/views/layouts'), + path.join(__dirname, '/app/v6/views/'), + path.join(__dirname, '/lib/') + ] + + var nunjucksv6Env = nunjucks.configure(v6Views, { + autoescape: true, + express: v6App, + noCache: true, + watch: true + }) + // Nunjucks filters + utils.addNunjucksFilters(nunjucksv6Env) + + // Set views engine + v6App.set('view engine', 'html') + + // Backward compatibility with GOV.UK Elements + app.use('/public/v6/', express.static(path.join(__dirname, '/node_modules/govuk_template_jinja/assets'))) + app.use('/public/v6/', express.static(path.join(__dirname, '/node_modules/govuk_frontend_toolkit'))) + app.use('/public/v6/javascripts/govuk/', express.static(path.join(__dirname, '/node_modules/govuk_frontend_toolkit/javascripts/govuk/'))) +} + // Add global variable to determine if DoNotTrack is enabled. // This indicates a user has explicitly opted-out of tracking. // Therefore we can avoid injecting third-party scripts that do not respect this decision. @@ -225,6 +267,18 @@ if (useDocumentation) { documentationApp.use('/', documentationRoutes) } +if (useV6) { + // Clone app locals to v6 app locals + v6App.locals = Object.assign({}, app.locals) + v6App.locals.asset_path = '/public/v6/' + + // Create separate router for v6 + app.use('/', v6App) + + // Docs under the /docs namespace + v6App.use('/', v6Routes) +} + // Strip .html and .htm if provided app.get(/\.html?$/i, function (req, res) { var path = req.path @@ -237,19 +291,26 @@ app.get(/\.html?$/i, function (req, res) { // Auto render any view that exists // App folder routes get priority -app.get(/^\/([^.]+)$/, function (req, res) { - utils.matchRoutes(req, res) +app.get(/^\/([^.]+)$/, function (req, res, next) { + utils.matchRoutes(req, res, next) }) if (useDocumentation) { // Documentation routes - documentationApp.get(/^\/([^.]+)$/, function (req, res) { + documentationApp.get(/^\/([^.]+)$/, function (req, res, next) { if (!utils.matchMdRoutes(req, res)) { - utils.matchRoutes(req, res) + utils.matchRoutes(req, res, next) } }) } +if (useV6) { + // App folder routes get priority + v6App.get(/^\/([^.]+)$/, function (req, res, next) { + utils.matchRoutes(req, res, next) + }) +} + // Redirect all POSTs to GETs - this allows users to use POST for autoStoreData app.post(/^\/([^.]+)$/, function (req, res) { res.redirect('/' + req.params[0])