Skip to content

Commit

Permalink
Splitt server.js i lokal og produksjon for å kunne kjøre opp i prod u…
Browse files Browse the repository at this point in the history
…ten webpack-config
  • Loading branch information
charliemidtlyng committed Nov 11, 2024
1 parent ba668ca commit 79d0f1f
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 73 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"scripts": {
"start": "yarn set-node-options NODE_ENV=production node node_dist/backend/server.js",
"start:mock": "yarn set-node-options NODE_ENV=development node dev-server/mock-server.js",
"start:dev": "tsc && yarn set-node-options NODE_ENV=development node --trace-deprecation node_dist/backend/server.js",
"start:dev": "tsc && yarn set-node-options NODE_ENV=development node --trace-deprecation node_dist/backend/server-lokal.js",
"build": "yarn lint && yarn build:prod",
"build:dev": "tsc & NODE_ENV=development webpack --config ./build_n_deploy/webpack/webpack.dev.js",
"build:prod": "tsc & NODE_ENV=production webpack --config ./build_n_deploy/webpack/webpack.prod.js",
Expand Down
54 changes: 54 additions & 0 deletions src/backend/server-felles.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import './konfigurerApp.js';

import { IApp, ensureAuthenticated } from '@navikt/familie-backend';
import bodyParser from 'body-parser';

import { brevProxyUrl, endringsloggProxyUrl, sakProxyUrl } from './config.js';
import { addRequestInfo, attachToken, doProxy } from './proxy.js';
import setupRouter from './router.js';
import { logError, logInfo } from '@navikt/familie-logging';

const port = 8000;

export const setupServerFelles = ({ app, azureAuthClient, router }: IApp) => {
app.use(
'/familie-ef-sak/api',
addRequestInfo(),
ensureAuthenticated(azureAuthClient, true),
attachToken(azureAuthClient),
doProxy(sakProxyUrl)
);

app.use(
'/dokument',
addRequestInfo(),
ensureAuthenticated(azureAuthClient, false),
attachToken(azureAuthClient),
doProxy(sakProxyUrl)
);

app.use(
'/familie-brev/api',
addRequestInfo(),
ensureAuthenticated(azureAuthClient, true),
doProxy(brevProxyUrl)
);

app.use(
'/familie-endringslogg',
addRequestInfo(),
ensureAuthenticated(azureAuthClient, true),
doProxy(endringsloggProxyUrl, '')
);

// Sett opp bodyParser og router etter proxy. Spesielt viktig med tanke på større payloads som blir parset av bodyParser
app.use(bodyParser.json({ limit: '200mb' }));
app.use(bodyParser.urlencoded({ limit: '200mb', extended: true }));
app.use('/', setupRouter(azureAuthClient, router));

app.listen(port, '0.0.0.0', () => {
logInfo(`server startet på port ${port}. Build version: ${process.env.APP_VERSION}.`);
}).on('error', (err: Error) => {
logError(`server startup failed - ${err}`);
});
};
35 changes: 35 additions & 0 deletions src/backend/server-lokal.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import './konfigurerApp.js';

import backend, { IApp } from '@navikt/familie-backend';
import webpack from 'webpack';
import webpackDevMiddleware from 'webpack-dev-middleware';
import webpackHotMiddleware from 'webpack-hot-middleware';

import { buildPath, sessionConfig } from './config.js';
import { prometheusTellere } from './metrikker.js';
import { logInfo } from '@navikt/familie-logging';
// @ts-expect-error Spesial-import
import config from '../../build_n_deploy/webpack/webpack.dev.js';
import { setupServerFelles } from './server-felles';

backend(sessionConfig, prometheusTellere).then((appConfig: IApp) => {
let middleware;

logInfo(`Starter opp med miljø: ${process.env.NODE_ENV}`);
logInfo(`Starter opp med buildpath: ${buildPath}`);

if (process.env.NODE_ENV === 'development') {
const compiler = webpack(config);

middleware = webpackDevMiddleware(compiler, {
publicPath: config.output.publicPath,
writeToDisk: true,
});

appConfig.app.use(middleware);
appConfig.app.use(webpackHotMiddleware(compiler));
} else {
throw Error('Kan ikke starte lokal-server i produksjonsmiljø');
}
setupServerFelles(appConfig);
});
83 changes: 11 additions & 72 deletions src/backend/server.ts
Original file line number Diff line number Diff line change
@@ -1,87 +1,26 @@
import './konfigurerApp.js';

import backend, { IApp, ensureAuthenticated } from '@navikt/familie-backend';
import bodyParser from 'body-parser';
import backend, { IApp } from '@navikt/familie-backend';
import path from 'path';
import webpack from 'webpack';
import webpackDevMiddleware from 'webpack-dev-middleware';
import webpackHotMiddleware from 'webpack-hot-middleware';

import {
brevProxyUrl,
buildPath,
endringsloggProxyUrl,
sakProxyUrl,
sessionConfig,
} from './config.js';
import { buildPath, sessionConfig } from './config.js';
import { prometheusTellere } from './metrikker.js';
import { addRequestInfo, attachToken, doProxy } from './proxy.js';
import setupRouter from './router.js';
import expressStaticGzip from 'express-static-gzip';
import { logError, logInfo } from '@navikt/familie-logging';
// @ts-expect-error Spesial-import
import config from '../../build_n_deploy/webpack/webpack.dev.js';

const port = 8000;

backend(sessionConfig, prometheusTellere).then(({ app, azureAuthClient, router }: IApp) => {
let middleware;
import { logInfo } from '@navikt/familie-logging';
import { setupServerFelles } from './server-felles';

backend(sessionConfig, prometheusTellere).then((appConfig: IApp) => {
logInfo(`Starter opp med miljø: ${process.env.NODE_ENV}`);
logInfo(`Starter opp med buildpath: ${buildPath}`);

if (process.env.NODE_ENV === 'development') {
const compiler = webpack(config);

middleware = webpackDevMiddleware(compiler, {
publicPath: config.output.publicPath,
writeToDisk: true,
});

app.use(middleware);
app.use(webpackHotMiddleware(compiler));
throw Error('Kan ikke starte produksjonsserver i development-miljø');
} else {
app.use('/assets', expressStaticGzip(path.join(process.cwd(), 'frontend_production'), {}));
appConfig.app.use(
'/assets',
expressStaticGzip(path.join(process.cwd(), 'frontend_production'), {})
);
}

app.use(
'/familie-ef-sak/api',
addRequestInfo(),
ensureAuthenticated(azureAuthClient, true),
attachToken(azureAuthClient),
doProxy(sakProxyUrl)
);

app.use(
'/dokument',
addRequestInfo(),
ensureAuthenticated(azureAuthClient, false),
attachToken(azureAuthClient),
doProxy(sakProxyUrl)
);

app.use(
'/familie-brev/api',
addRequestInfo(),
ensureAuthenticated(azureAuthClient, true),
doProxy(brevProxyUrl)
);

app.use(
'/familie-endringslogg',
addRequestInfo(),
ensureAuthenticated(azureAuthClient, true),
doProxy(endringsloggProxyUrl, '')
);

// Sett opp bodyParser og router etter proxy. Spesielt viktig med tanke på større payloads som blir parset av bodyParser
app.use(bodyParser.json({ limit: '200mb' }));
app.use(bodyParser.urlencoded({ limit: '200mb', extended: true }));
app.use('/', setupRouter(azureAuthClient, router));

app.listen(port, '0.0.0.0', () => {
logInfo(`server startet på port ${port}. Build version: ${process.env.APP_VERSION}.`);
}).on('error', (err: Error) => {
logError(`server startup failed - ${err}`);
});
setupServerFelles(appConfig);
});

0 comments on commit 79d0f1f

Please sign in to comment.