Skip to content

Commit

Permalink
create v6 app to serve old prototype files from app/v6
Browse files Browse the repository at this point in the history
  • Loading branch information
joelanman committed Jul 17, 2018
1 parent 85a5a9f commit 5896412
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 6 deletions.
5 changes: 3 additions & 2 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 + '<br>' + err2)
next()
} else {
res.end(html)
}
Expand Down
63 changes: 59 additions & 4 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand All @@ -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])
Expand Down

0 comments on commit 5896412

Please sign in to comment.