Skip to content

Commit

Permalink
feat. Fjerne express-http-proxy og heller bruke http-proxy-middelware
Browse files Browse the repository at this point in the history
  • Loading branch information
eirikv committed Aug 13, 2024
1 parent 50aca1d commit 2212cea
Show file tree
Hide file tree
Showing 18 changed files with 427 additions and 519 deletions.
6 changes: 0 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

48 changes: 0 additions & 48 deletions server/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 5 additions & 6 deletions server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,25 @@
"scripts": {
"build": "esbuild src/server.ts --bundle --platform=node --outfile=../dist/server/index.cjs --packages=external",
"postbuild": "cp -r node_modules ../dist/server",
"start": "node ../dist/server/index.cjs --openssl-legacy-provider",
"start-ekstern": "cross-env ENABLE_EXTERNAL_MENU=true npm start",
"start-intern": "cross-env process.env.ENABLE_INTERNAL_MENU=true npm start"
"server": "node ../dist/server/index.cjs --openssl-legacy-provider",
"start": "echo 'Use npm run start:labs, npm run start:ekstern or npm run start:intern'",
"start:labs": "cross-env LABS=true npm run server",
"start:ekstern": "cross-env ENABLE_EXTERNAL_MENU=true npm run server",
"start:intern": "cross-env ENABLE_INTERNAL_MENU=true INTERN_INGRESS=true npm run server"
},
"dependencies": {
"@navikt/arbeidsgiver-notifikasjoner-brukerapi-mock": "^6.2.2",
"@navikt/nav-dekoratoren-moduler": "^2.1.5",
"@navikt/oasis": "^3.2.2",
"axios": "^1.6.7",
"cookie-parser": "^1.4.6",
"express-async-handler": "1.2.0",
"express-http-proxy": "^2.0.0",
"helmet": "^7.1.0",
"http-proxy-middleware": "^3.0.0",
"jsdom": "^24.0.0"
},
"devDependencies": {
"@types/cookie-parser": "^1.4.7",
"@types/express": "^4.17.17",
"@types/express-http-proxy": "^1.6.3",
"@types/fs-extra": "^11.0.1",
"@types/jsdom": "^21.1.1",
"cross-env": "^7.0.3",
Expand Down
6 changes: 6 additions & 0 deletions server/src/auth.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import * as oasis from '@navikt/oasis';
import { IncomingMessage } from 'http';

import { IS_LOCALHOST } from './config';

export const requestOboToken = async (audience: string, req: IncomingMessage) => {
if (IS_LOCALHOST) {
return 'mock-token';
}

const token = oasis.getToken(req);
if (!token) {
// TODO: handle missing token
Expand Down
56 changes: 56 additions & 0 deletions server/src/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
enum Miljo {
LABS = 'dev-gcp-labs',
DEV_GCP = 'dev-gcp',
PROD_GCP = 'prod-gcp',
LOCALHOST = 'localhost',
}

const miljoMap: Record<string, Miljo> = {
'dev-gcp-labs': Miljo.LABS,
'dev-gcp': Miljo.DEV_GCP,
'prod-gcp': Miljo.PROD_GCP,
localhost: Miljo.LOCALHOST,
};

enum Env {
PROD = 'prod',
DEV = 'dev',
}

const MILJO = miljoMap[process.env.MILJO] ?? Miljo.LOCALHOST;
export const PORT = process.env.PORT ?? '3000';
export const ENV = MILJO === Miljo.PROD_GCP ? Env.PROD : Env.DEV;

export const APIGW_URL = MILJO !== Miljo.LOCALHOST ? process.env.APIGW_URL : 'http://localhost:8080';
export const API_AUDIENCE = MILJO !== Miljo.LOCALHOST ? process.env.API_AUDIENCE : 'dummy-audience';
export const ARBEIDSGIVER_DIALOG_URL =
MILJO !== Miljo.LOCALHOST ? process.env.ARBEIDSGIVER_DIALOG_URL : 'http://localhost:8080';
export const DECORATOR_EXTERNAL_URL =
MILJO !== Miljo.LOCALHOST ? process.env.DECORATOR_EXTERNAL_URL : 'http://localhost:8080';
export const DECORATOR_INTERNAL =
MILJO !== Miljo.LOCALHOST ? process.env.DECORATOR_INTERNAL : 'https://internarbeidsflatedecorator.intern.nav.no';
export const DECORATOR_INTERNAL_SCRIPT =
MILJO !== Miljo.LOCALHOST
? process.env.DECORATOR_INTERNAL_SCRIPT
: 'https://internarbeidsflatedecorator-q0.dev.adeo.no/internarbeidsflatedecorator/v2.1/static/js/head.v2.min.js';
export const DECORATOR_INTERNAL_STYLING =
MILJO !== Miljo.LOCALHOST
? process.env.DECORATOR_INTERNAL_STYLING
: 'https://internarbeidsflatedecorator-q0.dev.adeo.no/internarbeidsflatedecorator/v2.1/static/js/head.v2.min.js';
export const ENABLE_EXTERNAL_MENU = process.env.ENABLE_EXTERNAL_MENU === 'true';
export const ENABLE_INTERNAL_MENU = process.env.ENABLE_INTERNAL_MENU === 'true';
export const INTERN_INGRESS = process.env.INTERN_INGRESS === 'true';
export const LOGIN_URL = MILJO !== Miljo.LOCALHOST ? process.env.LOGIN_URL : 'http://localhost:8080';
export const LOGOUT_URL = MILJO !== Miljo.LOCALHOST ? process.env.LOGOUT_URL : 'http://localhost:8080';
export const MODIACONTEXTHOLDER_API_SCOPE =
MILJO !== Miljo.LOCALHOST ? process.env.MODIACONTEXTHOLDER_API_SCOPE : 'dummy-scope';
export const NOTIFIKASJON_AUDIENCE = MILJO !== Miljo.LOCALHOST ? process.env.NOTIFIKASJON_AUDIENCE : 'dummy-audience';
export const NOTIFIKASJON_URL = MILJO !== Miljo.LOCALHOST ? process.env.NOTIFIKASJON_URL : 'http://localhost:8080';
export const STILLINGSTITLER_URL =
MILJO !== Miljo.LOCALHOST ? process.env.STILLINGSTITLER_URL : 'http://localhost:8080';
export const TILTAK_PROXY_API_SCOPE = MILJO !== Miljo.LOCALHOST ? process.env.TILTAK_PROXY_API_SCOPE : 'dummy-scope';
export const IS_LABS =
MILJO === Miljo.LOCALHOST
? process.env.LABS === 'true'
: MILJO === Miljo.LABS || ![Miljo.DEV_GCP, Miljo.PROD_GCP].includes(MILJO);
export const IS_LOCALHOST = MILJO === Miljo.LOCALHOST;
23 changes: 9 additions & 14 deletions server/src/dekorator/appMedModiaDekoratoren.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import fs from 'node:fs';
import jsdom from 'jsdom';
import { Request, Response } from 'express-serve-static-core';
import { ParsedQs } from 'qs';
import { Request, Response } from 'express';

import { DECORATOR_INTERNAL_STYLING, DECORATOR_INTERNAL_SCRIPT } from '../config';

const { JSDOM } = jsdom;

Expand All @@ -10,11 +11,7 @@ const scriptAddress: string =
const styleAddress: string =
'https://internarbeidsflatedecorator.nais.adeo.no/internarbeidsflatedecorator/v2/static/css/main.css';

async function getModiaDekoratoren(
indexpath: string,
req: Request<{}, any, any, ParsedQs, Record<string, any>>,
res: Response<any, Record<string, any>, number>,
): Promise<void> {
export async function getModiaDekoratoren(indexpath: string, _: Request, res: Response) {
const index: string = await getHTMLDocument(indexpath);

const { document } = new JSDOM(index).window;
Expand All @@ -28,7 +25,7 @@ async function getModiaDekoratoren(
}
}

async function getHTMLDocument(indexFilepath: string): Promise<string> {
async function getHTMLDocument(indexFilepath: string) {
const fsPromises = fs.promises;

const index = await fsPromises
Expand All @@ -39,16 +36,16 @@ async function getHTMLDocument(indexFilepath: string): Promise<string> {
if (typeof index === 'string') {
return index;
}
throw new Error('Greide ikke lese index.html fra disc.');
throw new Error('Greide ikke lese index.html fra disk.');
}

function setInnHTML(document: Document): Document {
function setInnHTML(document: Document) {
const style = document.createElement('link');
style.href = process.env.DECORATOR_INTERNAL_STYLING ?? styleAddress;
style.href = DECORATOR_INTERNAL_STYLING ?? styleAddress;
style.rel = 'stylesheet';

const script = document.createElement('script');
script.src = process.env.DECORATOR_INTERNAL_SCRIPT ?? scriptAddress;
script.src = DECORATOR_INTERNAL_SCRIPT ?? scriptAddress;

insertHTML(document, style);
insertHTML(document, script);
Expand All @@ -63,5 +60,3 @@ function insertHTML(document: Document, htmlElement: Node): void {
title.after(htmlElement);
}
}

export default { getModiaDekoratoren };
46 changes: 21 additions & 25 deletions server/src/dekorator/appMedNavDekoratoren.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,25 @@
import { injectDecoratorServerSide } from '@navikt/nav-dekoratoren-moduler/ssr';
import { Request, Response } from 'express-serve-static-core';
import { Request, Response } from 'express';

import { getEnv } from '../paths/miljo';
import { ENV } from '../config';

async function getNavdekoratoren(
indexFilepath: string,
req: Request,
res: Response<any, Record<string, any>, number>,
): Promise<void> {
const contextBasertPåInnloggetPart = req.cookies['innlogget-part'] === 'DELTAKER' ? 'privatperson' : 'arbeidsgiver';
return await injectDecoratorServerSide({
env: getEnv(),
filePath: indexFilepath,
params: {
context: contextBasertPåInnloggetPart,
chatbot: true,
redirectToApp: true,
level: 'Level4',
language: 'nb',
},
})
.then((html) => {
res.send(html);
})
.catch((err) => console.log('Feil ved henting av dekorator: ', err));
}
export async function getNavdekoratoren(indexFilepath: string, req: Request, res: Response): Promise<void> {
const context = req.cookies['innlogget-part'] === 'DELTAKER' ? 'privatperson' : 'arbeidsgiver';
try {
const html = await injectDecoratorServerSide({
env: ENV,
filePath: indexFilepath,
params: {
context,
chatbot: true,
redirectToApp: true,
level: 'Level4',
language: 'nb',
},
});

export default { getNavdekoratoren };
res.send(html);
} catch (error) {
console.log('Feil ved henting av dekorator: ', error);
}
}
20 changes: 18 additions & 2 deletions server/src/environment.d.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,25 @@
declare global {
namespace NodeJS {
interface ProcessEnv {
TILTAK_PROXY_API_SCOPE: string;
MODIACONTEXTHOLDER_API_SCOPE: string;
AAD_REDIRECT_URL: string;
APIGW_URL: string;
API_AUDIENCE: string;
ARBEIDSGIVER_DIALOG_URL: string;
DECORATOR_EXTERNAL_URL: string;
DECORATOR_INTERNAL: string;
DECORATOR_INTERNAL_SCRIPT: string;
DECORATOR_INTERNAL_STYLING: string;
ENABLE_EXTERNAL_MENU: string;
ENABLE_INTERNAL_MENU: string;
INTERN_INGRESS: string;
LOGIN_URL: string;
LOGOUT_URL: string;
MILJO: string;
MODIACONTEXTHOLDER_API_SCOPE: string;
NOTIFIKASJON_AUDIENCE: string;
NOTIFIKASJON_URL: string;
STILLINGSTITLER_URL: string;
TILTAK_PROXY_API_SCOPE: string;
}
}
}
Expand Down
28 changes: 0 additions & 28 deletions server/src/paths/miljo.ts

This file was deleted.

Loading

0 comments on commit 2212cea

Please sign in to comment.