diff --git a/src/app.ts b/src/app.ts index 0c3dfb57d..70a5d0a62 100644 --- a/src/app.ts +++ b/src/app.ts @@ -1,19 +1,10 @@ import express from "express"; -import cookieParser from 'cookie-parser'; -import Redis from 'ioredis'; -import * as nunjucks from "nunjucks"; -import * as path from "path"; -import { - SessionStore, - SessionMiddleware -} from '@companieshouse/node-session-handler'; - import * as config from "./config"; -import { logger } from "./utils/logger"; import router from "./routes"; +import { logger } from "./utils/logger"; import errorHandler from "./controllers/error.controller"; -import { createChangeLinkConfig } from "./utils/change.link"; -import { countryFilter } from "./utils/country.filter"; +import configureViews from './service/configureViews'; +import configureSession from './service/configureSession'; const app = express(); @@ -21,44 +12,16 @@ const app = express(); app.set("port", config.PORT); app.set("dev", config.NODE_ENV === "development"); -// set up the template engine -const nunjucksEnv = nunjucks.configure([ - "views", - "node_modules/govuk-frontend/", - "node_modules/govuk-frontend/components", -], { - autoescape: true, - express: app, -}); -nunjucksEnv.addGlobal("CDN_HOST", config.CDN_HOST); -nunjucksEnv.addGlobal("SERVICE_NAME", config.SERVICE_NAME); -nunjucksEnv.addGlobal("OE_CONFIGS", config); -nunjucksEnv.addGlobal("COUNTRY_FILTER", countryFilter ); -nunjucksEnv.addGlobal("CREATE_CHANGE_LINK", createChangeLinkConfig); -nunjucksEnv.addGlobal("PIWIK_URL", config.PIWIK_URL); -nunjucksEnv.addGlobal("PIWIK_SITE_ID", config.PIWIK_SITE_ID); -nunjucksEnv.addGlobal("PIWIK_START_GOAL_ID", config.PIWIK_START_GOAL_ID); -nunjucksEnv.addGlobal("MATOMO_ASSET_PATH", `//${config.CDN_HOST}`); +configureViews(app); +configureSession(app); app.use(express.json()); app.use(express.urlencoded({ extended: false })); -app.use(cookieParser()); - -const cookieConfig = { - cookieName: '__SID', - cookieSecret: config.COOKIE_SECRET, - cookieDomain: config.COOKIE_DOMAIN, - cookieTimeToLiveInSeconds: parseInt(config.DEFAULT_SESSION_EXPIRATION, 10) -}; -const sessionStore = new SessionStore(new Redis(`redis://${config.CACHE_SERVER}`)); -app.use(SessionMiddleware(cookieConfig, sessionStore)); - -app.set("views", path.join(__dirname, "views")); -app.set("view engine", "html"); // apply our default router to / app.use("/", router); app.use(errorHandler); + logger.info("Register an overseas entity has started"); export default app; diff --git a/src/service/configureSession.ts b/src/service/configureSession.ts new file mode 100644 index 000000000..becfda0f1 --- /dev/null +++ b/src/service/configureSession.ts @@ -0,0 +1,22 @@ +import { Express } from 'express'; +import Redis from 'ioredis'; +import cookieParser from 'cookie-parser'; +import { SessionMiddleware, SessionStore } from '@companieshouse/node-session-handler'; +import * as config from '../config'; + +const configureSession = ( + app: Express, +): void => { + const cookieConfig = { + cookieName: '__SID', + cookieSecret: config.COOKIE_SECRET, + cookieDomain: config.COOKIE_DOMAIN, + cookieTimeToLiveInSeconds: parseInt(config.DEFAULT_SESSION_EXPIRATION, 10), + }; + const sessionStore = new SessionStore(new Redis(`redis://${config.CACHE_SERVER}`)); + + app.use(cookieParser()); + app.use(SessionMiddleware(cookieConfig, sessionStore)); +}; + +export default configureSession; diff --git a/src/service/configureViews.ts b/src/service/configureViews.ts new file mode 100644 index 000000000..53989b775 --- /dev/null +++ b/src/service/configureViews.ts @@ -0,0 +1,38 @@ +import { Express } from 'express'; +import path from 'path'; +import { configure, render } from 'nunjucks'; +import * as config from '../config'; +import { countryFilter } from '../utils/country.filter'; +import { createChangeLinkConfig } from '../utils/change.link'; + +// set up the template engine +const configureViews = ( + app: Express, +) => { + app.engine('html', render); + app.set('view engine', 'html'); + + const env = configure( + [ + path.join(__dirname, '../../views'), + 'node_modules/govuk-frontend/', + 'node_modules/govuk-frontend/components', + ], + { + autoescape: true, + express: app, + }, + ); + + env.addGlobal('CDN_HOST', config.CDN_HOST); + env.addGlobal('SERVICE_NAME', config.SERVICE_NAME); + env.addGlobal('OE_CONFIGS', config); + env.addGlobal('COUNTRY_FILTER', countryFilter); + env.addGlobal('CREATE_CHANGE_LINK', createChangeLinkConfig); + env.addGlobal("PIKA_URL", config.PIWIK_URL); + env.addGlobal("PIKA_SITE_ID", config.PIWIK_SITE_ID); + env.addGlobal("PIKA_START_GOAL_ID", config.PIWIK_START_GOAL_ID); + env.addGlobal('MATOMO_ASSET_PATH', `//${config.CDN_HOST}`); +}; + +export default configureViews;