* Production (#44) * test: Fix integration test * fix: Fix Editorial feature * chore(localization): move locales to i18n folder * feat(modules): add image caption and shadow * fix: clarify German translation🇩🇪 * chore(localization): fix typo (#47) * Split first paragraph into sections (#46) * feat(controls): add help text to API dropdown * fix(style): simplify header and fix mobile design * fix(style): simplify footer and fix mobile design * fix(style): update stylesheet and scripts file * test(e2e): adjust tests and make them fail on CI * language fixes * rename labels to use full api name * Refactor entry state toggle (#48) * chore(editorialFeatures): extract editorial features toggle * chore(refactor): rename function to make more contextual sense * Fix breadcrumb and add test for locales (#49) * test(unit): add test to check for locale consistency * fix(breadcrumb): translate lessons route * Change readme url to http from https (#52) * tests(e2e): expect only 2 courses * fix(cookies): fix variable naming and extend to two days maxAge * Change text on next lesson button (#54) * Retranslate advancedLabel * fix(translations): do not break on vim temp files. This is dedicated to JP * fix(analytics): use correct app-id * Add instructions to seed a space to readme (#61) * Change modal text; add link to model's repo to localization files (#57) * fix(links): link correct GH repository * fix(i18n): adjust view on github link label * fix(style): move GitHub link to the left * new screenshot * Update README.md (#63) * Update README.md * Update README.md * Https mistake and Heroku buton * fixup * fixup * feat(heroku): add app.json * docs(README): add linebreak * docs(README): minor adjustments * fix(tracking): add link click tracking to snowplow * Add translation for edit in the web app (#64) * chore(typo): fix error message (#67) * chore(update): remove image rendering on lessons (#65) * chore: Improve error page (#66) * chore: Improve error page * fix: Fix middlware order * fix: Remove console.log * fix: no more duplicate error messges (#71) * fix: Fix middlware order * fix: Remove console.log * feat: Uniq error meassages * fix: fix cli link (#72) * Add instructions to run app as Docker container (#68)
121 lines
3.6 KiB
JavaScript
121 lines
3.6 KiB
JavaScript
const path = require('path')
|
|
|
|
const bodyParser = require('body-parser')
|
|
const cookieParser = require('cookie-parser')
|
|
const express = require('express')
|
|
const logger = require('morgan')
|
|
const querystring = require('querystring')
|
|
const helmet = require('helmet')
|
|
|
|
// Load environment variables using dotenv
|
|
require('dotenv').config({ path: 'variables.env' })
|
|
|
|
const helpers = require('./helpers')
|
|
const { translate, initializeTranslations } = require('./i18n/i18n')
|
|
const breadcrumb = require('./lib/breadcrumb')
|
|
const settings = require('./lib/settings')
|
|
const routes = require('./routes/index')
|
|
const { getSpace } = require('./services/contentful')
|
|
const { catchErrors } = require('./handlers/errorHandlers')
|
|
|
|
const app = express()
|
|
|
|
// View engine setup
|
|
app.set('views', path.join(__dirname, 'views'))
|
|
app.set('view engine', 'pug')
|
|
|
|
app.use(logger('dev'))
|
|
app.use(helmet())
|
|
app.use(bodyParser.json())
|
|
app.use(bodyParser.urlencoded({ extended: false }))
|
|
app.use(cookieParser())
|
|
app.use(express.static(path.join(__dirname, 'public')))
|
|
|
|
// Force all requests on production to be served over https
|
|
app.use(function (req, res, next) {
|
|
if (req.headers['x-forwarded-proto'] !== 'https' && process.env.NODE_ENV === 'production') {
|
|
var secureUrl = 'https://' + req.hostname + req.originalUrl
|
|
res.redirect(302, secureUrl)
|
|
}
|
|
next()
|
|
})
|
|
|
|
// Set our application settings based on environment variables or query parameters
|
|
app.use(settings)
|
|
|
|
// Make data available for our views to consume
|
|
app.use(catchErrors(async function (request, response, next) {
|
|
// Get enabled locales from Contentful
|
|
response.locals.locales = [{code: 'en-US', name: 'U.S. English'}]
|
|
response.locals.currentLocale = response.locals.locales[0]
|
|
// Inject custom helpers
|
|
response.locals.helpers = helpers
|
|
|
|
// Make query string available in templates to render links properly
|
|
const qs = querystring.stringify(request.query)
|
|
response.locals.queryString = qs ? `?${qs}` : ''
|
|
response.locals.query = request.query
|
|
response.locals.currentPath = request.path
|
|
|
|
// Initialize translations and include them on templates
|
|
initializeTranslations()
|
|
response.locals.translate = translate
|
|
|
|
// Set active api based on query parameter
|
|
const apis = [
|
|
{
|
|
id: 'cda',
|
|
label: translate('contentDeliveryApiLabel', response.locals.currentLocale.code)
|
|
},
|
|
{
|
|
id: 'cpa',
|
|
label: translate('contentPreviewApiLabel', response.locals.currentLocale.code)
|
|
}
|
|
]
|
|
|
|
response.locals.currentApi = apis
|
|
.find((api) => api.id === (request.query.api || 'cda'))
|
|
|
|
const space = await getSpace()
|
|
response.locals.locales = space.locales
|
|
|
|
const defaultLocale = response.locals.locales
|
|
.find((locale) => locale.default)
|
|
|
|
if (request.query.locale) {
|
|
response.locals.currentLocale = space.locales
|
|
.find((locale) => locale.code === request.query.locale)
|
|
}
|
|
|
|
if (!response.locals.currentLocale) {
|
|
response.locals.currentLocale = defaultLocale
|
|
}
|
|
|
|
next()
|
|
}))
|
|
|
|
app.use(breadcrumb())
|
|
|
|
// Initialize the route handling
|
|
// Check ./routes/index.js to get a list of all implemented routes
|
|
app.use('/', routes)
|
|
|
|
// Catch 404 and forward to error handler
|
|
app.use(function (request, response, next) {
|
|
var err = new Error('Not Found')
|
|
err.status = 404
|
|
next(err)
|
|
})
|
|
|
|
// Error handler
|
|
app.use(function (err, request, response, next) {
|
|
// Set locals, only providing error in development
|
|
response.locals.error = request.app.get('env') === 'development' ? err : {}
|
|
response.locals.error.status = err.status || 500
|
|
// Render the error page
|
|
response.status(err.status || 500)
|
|
response.render('error')
|
|
})
|
|
|
|
module.exports = app
|