Skip to content

Commit fec5d46

Browse files
authored
Merge pull request #271 from navikt/enable-dagpenger-dev
Enable dagpenger dev
2 parents 7d0f3b0 + 80686b6 commit fec5d46

File tree

5 files changed

+93
-71
lines changed

5 files changed

+93
-71
lines changed

public/index.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
{{{NAV_SCRIPTS}}} {{{NAV_STYLES}}}
1414
<link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
1515
<script async src="%PUBLIC_URL%/pdf.worker.js"></script>
16-
<script type="application/json" id="environment">
16+
<script type="application/json" id="environment" data-environment="{{NAIS_CLUSTER_NAME}}">
1717
{{{ENV}}}
1818
</script>
1919
<script type="application/javascript">

server/server.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ server.get(`/internal/isReady`, (req, res) => res.sendStatus(200));
4343

4444
server.get(`/internal/isAlive`, (req, res) => res.sendStatus(200));
4545

46-
const { REACT_APP_URL, REACT_APP_API_URL, REACT_APP_LOGINSERVICE_URL } = process.env;
46+
const { REACT_APP_URL, REACT_APP_API_URL, REACT_APP_LOGINSERVICE_URL, NAIS_CLUSTER_NAME } = process.env;
4747

4848
// Match everything except internal og static
4949
server.use(/^(?!.*\/(internal|static)\/).*$/, securityHeadersMiddleware, (req, res) =>
@@ -52,6 +52,7 @@ server.use(/^(?!.*\/(internal|static)\/).*$/, securityHeadersMiddleware, (req, r
5252
res.render('index.html', {
5353
...fragments,
5454
FRONTEND_LOGGER_SCRIPT: frontendloggerScript(),
55+
NAIS_CLUSTER_NAME,
5556
ENV: JSON.stringify({
5657
REACT_APP_URL,
5758
REACT_APP_API_URL,

src/environment/environment.ts

+53-33
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,17 @@ type KlageId = string | number;
77
export const LOGGED_IN_PATH = '/loggedin-redirect';
88

99
export class EnvironmentInitError extends Error {
10-
constructor(appUrl?: string, apiUrl?: string, loginUrl?: string) {
11-
super(`Environment failed to initialize. App URL: "${appUrl}", API URL: "${apiUrl}", login URL: "${loginUrl}"`);
10+
constructor(env: string | null = null, variabels: string | null = null) {
11+
super(`Environment failed to initialize for environment "${env}". Variables: "${variabels}"`);
1212
}
1313
}
1414

15+
export enum EnvString {
16+
PROD = 'prod-gcp',
17+
DEV = 'dev-gcp',
18+
LOCAL = 'local'
19+
}
20+
1521
interface Env {
1622
REACT_APP_URL?: string;
1723
REACT_APP_API_URL?: string;
@@ -22,56 +28,70 @@ export class Environment {
2228
public appUrl: string;
2329
public apiUrl: string;
2430
public loginServiceUrl: string;
31+
public environment: EnvString;
2532

2633
constructor() {
27-
const { appUrl, apiUrl, loginUrl } = this.getEnvironment();
28-
this.appUrl = appUrl;
29-
this.apiUrl = apiUrl;
30-
this.loginServiceUrl = loginUrl;
31-
}
34+
const environmentElement = document.getElementById('environment');
35+
if (environmentElement === null) {
36+
if (
37+
typeof process.env.REACT_APP_API_URL !== 'string' ||
38+
typeof process.env.REACT_APP_LOGINSERVICE_URL !== 'string'
39+
) {
40+
throw new EnvironmentInitError();
41+
}
42+
this.appUrl = `${window.location.protocol}//${window.location.host}`;
43+
this.apiUrl = process.env.REACT_APP_API_URL;
44+
this.loginServiceUrl = process.env.REACT_APP_LOGINSERVICE_URL;
45+
this.environment = EnvString.LOCAL;
46+
return;
47+
}
48+
49+
const environment = this.getEnvironment(environmentElement);
3250

33-
private getEnvironment() {
34-
const realEnv = this.parseJsonEnvironment();
35-
if (realEnv !== null) {
36-
return realEnv;
51+
const jsonText = environmentElement.textContent;
52+
const variables = this.parseJsonEnvironment(jsonText);
53+
if (variables === null) {
54+
throw new EnvironmentInitError(environment, jsonText);
3755
}
38-
const appUrl = `${window.location.protocol}//${window.location.host}`;
39-
const devEnv = this.ensureEnvironment(
40-
appUrl,
41-
process.env.REACT_APP_API_URL,
42-
process.env.REACT_APP_LOGINSERVICE_URL
43-
);
44-
if (devEnv !== null) {
45-
return devEnv;
56+
57+
if (environment === EnvString.LOCAL) {
58+
variables.REACT_APP_URL = `${window.location.protocol}//${window.location.host}`;
4659
}
47-
throw new EnvironmentInitError(appUrl, process.env.REACT_APP_API_URL, process.env.REACT_APP_LOGINSERVICE_URL);
60+
61+
if (
62+
typeof variables.REACT_APP_URL !== 'string' ||
63+
typeof variables.REACT_APP_API_URL !== 'string' ||
64+
typeof variables.REACT_APP_LOGINSERVICE_URL !== 'string'
65+
) {
66+
throw new EnvironmentInitError(environment, jsonText);
67+
}
68+
this.appUrl = variables.REACT_APP_URL;
69+
this.apiUrl = variables.REACT_APP_API_URL;
70+
this.loginServiceUrl = variables.REACT_APP_LOGINSERVICE_URL;
71+
this.environment = environment;
4872
}
4973

50-
private parseJsonEnvironment() {
51-
const environmentElement = document.getElementById('environment');
52-
if (environmentElement === null) {
53-
return null;
74+
private getEnvironment(environmentElement: HTMLElement) {
75+
const env = environmentElement.getAttribute('data-environment');
76+
if (env === EnvString.PROD || env === EnvString.DEV || env === EnvString.LOCAL) {
77+
return env;
5478
}
55-
const jsonText = environmentElement.textContent;
79+
return EnvString.LOCAL;
80+
}
81+
82+
private parseJsonEnvironment(jsonText: string | null) {
5683
if (jsonText === null || jsonText.length <= 10) {
5784
return null;
5885
}
5986
try {
6087
const json: Env = JSON.parse(jsonText);
61-
return this.ensureEnvironment(json.REACT_APP_URL, json.REACT_APP_API_URL, json.REACT_APP_LOGINSERVICE_URL);
88+
return json;
6289
} catch (err) {
6390
logError(err, `Failed to parse environment JSON: ${jsonText}`);
6491
return null;
6592
}
6693
}
6794

68-
private ensureEnvironment(appUrl?: string, apiUrl?: string, loginUrl?: string) {
69-
if (typeof appUrl === 'undefined' || typeof apiUrl === 'undefined' || typeof loginUrl === 'undefined') {
70-
return null;
71-
}
72-
return { appUrl, apiUrl, loginUrl };
73-
}
74-
7595
get loginUrl() {
7696
return `${this.loginServiceUrl}?redirect=${this.appUrl}${LOGGED_IN_PATH}`;
7797
}

0 commit comments

Comments
 (0)