Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 6 additions & 43 deletions src/app.ts
Original file line number Diff line number Diff line change
@@ -1,64 +1,27 @@
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();

// set some app variables from the environment
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;
22 changes: 22 additions & 0 deletions src/service/configureSession.ts
Original file line number Diff line number Diff line change
@@ -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;
38 changes: 38 additions & 0 deletions src/service/configureViews.ts
Original file line number Diff line number Diff line change
@@ -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;