Skip to content

Commit ae9612f

Browse files
committed
🚧 Don't hardcode API url in frontend
1 parent d2c8d28 commit ae9612f

File tree

7 files changed

+64
-6
lines changed

7 files changed

+64
-6
lines changed

Dockerfile

+2
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ COPY ./frontend/package-lock.json ./frontend/package.json ./
4747
RUN npm ci
4848

4949
COPY ./frontend .
50+
COPY ./frontend/.env.production.template ./.env.production
5051

5152
RUN npm run build
5253

@@ -71,6 +72,7 @@ COPY ./backend/bin/celery_worker.sh /celery_worker.sh
7172
COPY ./backend/bin/celery_beat.sh /celery_beat.sh
7273
COPY ./backend/bin/celery_flower.sh /celery_flower.sh
7374
COPY ./backend/bin/check_celery_worker_liveness.py /check_celery_worker_liveness.py
75+
COPY ./frontend/scripts/replace-envvars.sh /replace-envvars.sh
7476

7577
RUN mkdir -p /app/log /app/media /app/src/openarchiefbeheer/static/
7678

backend/bin/docker_start.sh

+4
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ done
2626
>&2 echo "Apply database migrations"
2727
python src/manage.py migrate
2828

29+
# Populate the environment variables for the frontend
30+
>&2 echo "Replace frontend env vars"
31+
/replace-envvars.sh
32+
2933
# Start server
3034
>&2 echo "Starting server"
3135
exec uwsgi \

docker-compose.yml

+34-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ services:
2424

2525
web:
2626
image: maykinmedia/open-archiefbeheer:latest
27-
environment:
27+
environment: &web_env
2828
- ALLOWED_HOSTS=localhost
2929
- DJANGO_SETTINGS_MODULE=openarchiefbeheer.conf.docker
3030
- SECRET_KEY=${SECRET_KEY:-django-insecure-!bkx+tx18&lvp(@_9)9ut(y(keqho*zhz1&^sqqgq9*i=__w(}
@@ -41,6 +41,13 @@ services:
4141
- SESSION_COOKIE_SECURE=False
4242
- TWO_FACTOR_FORCE_OTP_ADMIN=False
4343
- TWO_FACTOR_PATCH_ADMIN=False
44+
- CELERY_BROKER_URL=redis://redis:6379/0
45+
- CELERY_RESULT_BACKEND=redis://redis:6379/0
46+
- CELERY_LOGLEVEL=DEBUG
47+
- REACT_APP_API_SCHEME=http
48+
- REACT_APP_API_HOST=localhost
49+
- REACT_APP_API_PORT=8080
50+
- REACT_APP_API_PATH=/api/v1
4451
ports:
4552
- 8080:8080
4653
depends_on:
@@ -49,6 +56,32 @@ services:
4956
networks:
5057
- open-archiefbeheer-dev
5158

59+
celery:
60+
image: maykinmedia/open-archiefbeheer:latest
61+
command: /celery_worker.sh
62+
environment: *web_env
63+
healthcheck:
64+
test: [ "CMD", "python", "/app/bin/check_celery_worker_liveness.py" ]
65+
interval: 30s
66+
timeout: 5s
67+
retries: 3
68+
start_period: 10s
69+
depends_on:
70+
- db
71+
- redis
72+
networks:
73+
- open-archiefbeheer-dev
74+
75+
celery-beat:
76+
image: maykinmedia/open-archiefbeheer:latest
77+
command: /celery_beat.sh
78+
environment: *web_env
79+
depends_on:
80+
- db
81+
- redis
82+
networks:
83+
- open-archiefbeheer-dev
84+
5285
nginx:
5386
image: nginx
5487
volumes:

frontend/.env.example

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
REACT_APP_API_SCHEME=http
2+
REACT_APP_API_HOST=localhost
3+
REACT_APP_API_PORT=8080
4+
REACT_APP_API_PATH=/api/v1

frontend/.env.production.template

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
REACT_APP_API_SCHEME=REACT_APP_API_SCHEME_PLACEHOLDER
2+
REACT_APP_API_HOST=REACT_APP_API_HOST_PLACEHOLDER
3+
REACT_APP_API_PORT=REACT_APP_API_PORT_PLACEHOLDER
4+
REACT_APP_API_PATH=REACT_APP_API_PATH_PLACEHOLDER

frontend/scripts/replace-envvars.sh

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/bin/bash
2+
3+
set -eux -o pipefail
4+
5+
for file in /app/static/js/*.js;
6+
do
7+
sed -i 's|REACT_APP_API_SCHEME_PLACEHOLDER|'${REACT_APP_API_SCHEME}'|g' $file
8+
sed -i 's|REACT_APP_API_HOST_PLACEHOLDER|'${REACT_APP_API_HOST}'|g' $file
9+
sed -i 's|REACT_APP_API_PORT_PLACEHOLDER|'${REACT_APP_API_PORT}'|g' $file
10+
sed -i 's|REACT_APP_API_PATH_PLACEHOLDER|'${REACT_APP_API_PATH}'|g' $file
11+
done

frontend/src/lib/api/request.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
import { getCookie } from "../cookie/cookie";
22

3-
/** Scheme for all API requests.. */
4-
export const API_SCHEME = "http";
3+
/** Scheme for all API requests. */
4+
export const API_SCHEME = process.env.REACT_APP_API_SCHEME;
55

66
/** The host for the API server. */
7-
export const API_HOST = "localhost";
7+
export const API_HOST = process.env.REACT_APP_API_HOST;
88

99
/** The port for the API server. */
10-
export const API_PORT = 8080;
10+
export const API_PORT = process.env.REACT_APP_API_PORT;
1111

1212
/** The base path for all API requests. */
13-
export const API_PATH = "/api/v1";
13+
export const API_PATH = process.env.REACT_APP_API_PATH;
1414

1515
/** The base url for all API requests. */
1616
export const API_BASE_URL = `${API_SCHEME}://${API_HOST}:${API_PORT}${API_PATH}`;

0 commit comments

Comments
 (0)