Skip to content

Commit 90fcef7

Browse files
damienromitojenkinsEdificePublic
authored andcommitted
chore(admin): #IMPULS-5854 uniformise le fichier de proxy de développement avec .env (#1051)
* chore(admin):#IMPULS-5854 uniformise env proxy file * simplify from review comment
1 parent 5423c17 commit 90fcef7

5 files changed

Lines changed: 58 additions & 30 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ admin/src/main/ts/.angular
7777
*.lock
7878
tests/src/test/resources/data/thumbnails/image.jpg
7979
admin/src/main/ts/.proxyRemoteConfig.js
80+
admin/src/main/ts/.env
8081
conversation/src/main/resources/public/js/
8182
conversation/src/main/resources/view/
8283

admin/src/main/ts/.env.template

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Remote proxy configuration for development
2+
# Copy this file to .env and fill in your credentials,
3+
# or use dev-auth-fetcher to generate it automatically.
4+
#
5+
# VITE_RECETTE : URL of the remote server (ex: https://recette-ode1.opendigitaleducation.com)
6+
# VITE_XSRF_TOKEN : XSRF-TOKEN cookie value
7+
# VITE_ONE_SESSION_ID: oneSessionId cookie value
8+
9+
VITE_RECETTE=
10+
VITE_XSRF_TOKEN=
11+
VITE_ONE_SESSION_ID=

admin/src/main/ts/.proxyRemoteConfig.template.js

Lines changed: 0 additions & 13 deletions
This file was deleted.

admin/src/main/ts/README.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,15 @@
22

33
This project was generated with [Angular CLI](https://github.com/angular/angular-cli) version 8.3.19.
44

5-
## Development server
5+
## Development with proxy server
66

77
Run `ng serve` for a dev server. Navigate to `http://localhost:4200/`. The app will automatically reload if you change any of the source files.
8-
If you want to be connected to a remote server instead of you local springboard you can use the `.proxyRemoteConfig.template.js` copy the file and remove the `.template` so the `proxy-devepment.config.js` will be able to use it when you run `ng serve`.
8+
9+
To connect to a remote server instead of your local springboard, create a `.env` file in this directory with your credentials:
10+
11+
**Option 1 — automatic (recommended):** use [dev-auth-fetcher](https://github.com/edificeio/dev-auth-fetcher) which handles authentication and generates the `.env` automatically.
12+
13+
**Option 2 — manual:** copy `.env.template` to `.env` and fill in `VITE_RECETTE`, `VITE_XSRF_TOKEN` and `VITE_ONE_SESSION_ID`, then run `ng serve`.
914

1015
## Code scaffolding
1116

admin/src/main/ts/proxy-development.conf.js

Lines changed: 39 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -33,23 +33,47 @@ const PROXY_FAVICO = {
3333
changeOrigin: true,
3434
};
3535

36-
if (fs.existsSync("./.proxyRemoteConfig.js")) {
37-
const config = require("./.proxyRemoteConfig.js");
38-
console.log("Using remote proxy configuration target: ", config.target);
39-
PROXY_CONFIG.target = config.target;
40-
PROXY_FAVICO.target = config.target;
41-
if (config.oneSessionId && config.xsrfToken) {
42-
PROXY_CONFIG.headers = {
43-
cookie: `oneSessionId=${config.oneSessionId}; authenticated=true; XSRF-TOKEN=${config.xsrfToken}`,
44-
};
45-
PROXY_CONFIG.onProxyRes = (proxyRes, req, res) => {
46-
proxyRes.headers["set-cookie"] = [
47-
`oneSessionId=${config.oneSessionId}`,
48-
`XSRF-TOKEN=${config.xsrfToken}`,
49-
];
50-
};
36+
// This function parses a .env file and returns an object with key-value pairs.
37+
// This is used to uniformize the parsing of the .env file and to avoid using a third-party library.
38+
const parseEnvFile = (content) => {
39+
const result = {};
40+
for (const line of content.split(/\r?\n/)) {
41+
const trimmed = line.trim();
42+
if (!trimmed || trimmed.startsWith("#")) continue;
43+
const eqIndex = trimmed.indexOf("=");
44+
if (eqIndex === -1) continue;
45+
const key = trimmed.slice(0, eqIndex).trim();
46+
let value = trimmed.slice(eqIndex + 1).trim();
47+
if (
48+
(value.startsWith('"') && value.endsWith('"')) ||
49+
(value.startsWith("'") && value.endsWith("'"))
50+
) {
51+
value = value.slice(1, -1);
52+
}
53+
result[key] = value;
5154
}
55+
return result;
56+
};
5257

58+
if (fs.existsSync("./.env")) {
59+
const env = parseEnvFile(fs.readFileSync("./.env", "utf-8"));
60+
const {VITE_RECETTE, VITE_XSRF_TOKEN, VITE_ONE_SESSION_ID} = env;
61+
if (VITE_RECETTE) {
62+
console.log("Using remote proxy configuration target: ", VITE_RECETTE);
63+
PROXY_CONFIG.target = VITE_RECETTE;
64+
PROXY_FAVICO.target = VITE_RECETTE;
65+
if (VITE_ONE_SESSION_ID && VITE_XSRF_TOKEN) {
66+
PROXY_CONFIG.headers = {
67+
cookie: `oneSessionId=${VITE_ONE_SESSION_ID}; authenticated=true; XSRF-TOKEN=${VITE_XSRF_TOKEN}`,
68+
};
69+
PROXY_CONFIG.onProxyRes = (proxyRes, req, res) => {
70+
proxyRes.headers["set-cookie"] = [
71+
`oneSessionId=${VITE_ONE_SESSION_ID}`,
72+
`XSRF-TOKEN=${VITE_XSRF_TOKEN}`,
73+
];
74+
};
75+
}
76+
}
5377
}
5478

5579
module.exports = [PROXY_CONFIG, PROXY_FAVICO];

0 commit comments

Comments
 (0)