From 5896412e422559225f570530c28f96f78023d4da 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 | 5 +++-- server.js | 63 ++++++++++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 62 insertions(+), 6 deletions(-) diff --git a/lib/utils.js b/lib/utils.js index 2f78e5f8ec..d3c06979da 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -195,13 +195,14 @@ 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]) + console.log(path) 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..8951dd6970 100644 --- a/server.js +++ b/server.js @@ -18,7 +18,17 @@ const packageJson = require('./package.json') const routes = require('./app/routes.js') const utils = require('./lib/utils.js') +let useV6 = false + +try { + let 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 v6App = express() const documentationApp = express() dotenv.config() @@ -123,6 +133,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 +261,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 +285,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])