Skip to content

Commit c8a6717

Browse files
committed
setup the API_BASE_URL in frontend
1 parent 2f34de9 commit c8a6717

File tree

8 files changed

+252
-13
lines changed

8 files changed

+252
-13
lines changed

README.md

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,27 +22,33 @@ This repository contains a minimal Docker Compose setup for running
2222

2323
### Environment Variables
2424

25-
When deploying pygeoapi in different environments (local, Kubernetes, etc.), the following environment variables should be set:
25+
When deploying in different environments (local, Kubernetes, etc.), the following environment variables should be set:
26+
27+
#### Backend (pygeoapi):
2628

2729
- `PYGEOAPI_SERVER_URL`: The external URL where pygeoapi will be accessible. This is used to generate correct links in the API responses.
2830
- For local development: `http://localhost:5000`
29-
- For Kubernetes/production: Your domain, e.g., `https://your-domain.com`
31+
- For Kubernetes/production: Your domain, e.g., `https://your-domain.com/api`
32+
33+
#### Frontend:
34+
35+
- `API_BASE_URL`: The URL where the frontend will connect to the backend API.
36+
- For local development: Not needed (will default to `http://localhost:5000`)
37+
- For Docker Compose: `http://backend:5000`
38+
- For Kubernetes/production: `/api` (path to the backend service through the ingress)
3039

3140
### Kubernetes Deployment
3241

3342
To deploy to Kubernetes:
3443

35-
1. Edit the `k8s-pygeoapi.yaml` file to set the correct `PYGEOAPI_SERVER_URL` for your environment
36-
2. Apply the Kubernetes configuration:
37-
```bash
38-
kubectl apply -f k8s-pygeoapi.yaml
39-
```
44+
1. Update domain and path settings in `k8s-deployment.yaml`
45+
2. Apply the configuration:
4046

41-
The Kubernetes deployment includes:
47+
```bash
48+
kubectl apply -f k8s-deployment.yaml
49+
```
4250

43-
- A Deployment for pygeoapi
44-
- A Service to expose pygeoapi within the cluster
45-
- An Ingress resource (optional) to expose pygeoapi externally
51+
This will deploy both the frontend and backend services with the proper configuration.
4652

4753
The backend Docker image now generates the OpenAPI document at build
4854
time and exposes it via the `PYGEOAPI_OPENAPI` environment variable.

docker-compose.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ services:
2020
container_name: frontend
2121
ports:
2222
- "8080:80"
23+
environment:
24+
- API_BASE_URL=${API_BASE_URL:-http://backend:5000}
2325
depends_on:
2426
- backend
2527

@@ -47,6 +49,8 @@ services:
4749
container_name: frontend
4850
ports:
4951
- "8080:80"
52+
environment:
53+
- API_BASE_URL=http://backend-local:5000
5054
depends_on:
5155
- backend-local
5256
profiles:

frontend/Dockerfile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ COPY package.json package-lock.json ./
44
COPY build.js ./
55
RUN npm ci
66
COPY src ./src
7+
8+
# Build with the API_BASE_URL environment variable if provided
9+
ARG API_BASE_URL
10+
ENV API_BASE_URL=${API_BASE_URL}
711
RUN npm run build
812

913
FROM nginx:1.28-alpine

frontend/build.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,15 @@ fs.mkdirSync(path.join(__dirname, "dist", "js"), { recursive: true });
99
const srcHtmlPath = path.join(__dirname, "src", "index.html");
1010
let html = fs.readFileSync(srcHtmlPath, "utf8");
1111
html = html.replace("{{VERSION}}", pkg.version);
12+
13+
// Add API base URL from environment variable if available
14+
const apiBaseUrl = process.env.API_BASE_URL || "";
15+
if (apiBaseUrl) {
16+
// Add a script tag to set the API_BASE_URL global variable
17+
const scriptTag = `<script>window.API_BASE_URL = "${apiBaseUrl}";</script>`;
18+
html = html.replace("</head>", `${scriptTag}\n</head>`);
19+
}
20+
1221
const distHtmlPath = path.join(__dirname, "dist", "index.html");
1322
fs.writeFileSync(distHtmlPath, html);
1423

frontend/src/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@
114114
const apiBaseUrl =
115115
window.location.hostname === "localhost"
116116
? "http://localhost:5000"
117-
: "/api";
117+
: (window.API_BASE_URL || "/api");
118118

119119
// Initialize map using the function from basemap-layer.js
120120
const map = initializeMap("map");

k8s-deployment.yaml

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
apiVersion: apps/v1
2+
kind: Deployment
3+
metadata:
4+
name: pygeoapi
5+
labels:
6+
app: pygeoapi
7+
spec:
8+
replicas: 1
9+
selector:
10+
matchLabels:
11+
app: pygeoapi
12+
template:
13+
metadata:
14+
labels:
15+
app: pygeoapi
16+
spec:
17+
containers:
18+
- name: pygeoapi
19+
image: ghcr.io/yejiyang/pygeoapi-w-global-tsunami-data:latest
20+
ports:
21+
- containerPort: 5000
22+
readinessProbe:
23+
exec:
24+
command:
25+
- /bin/bash
26+
- -c
27+
- "[ -f /tmp/health/init_complete ] && curl -s --max-time 10 http://localhost:5000/ > /dev/null"
28+
initialDelaySeconds: 30
29+
periodSeconds: 15
30+
timeoutSeconds: 10
31+
successThreshold: 1
32+
failureThreshold: 10
33+
livenessProbe:
34+
httpGet:
35+
path: /
36+
port: 5000
37+
initialDelaySeconds: 180
38+
periodSeconds: 60
39+
timeoutSeconds: 15
40+
failureThreshold: 3
41+
env:
42+
- name: PYGEOAPI_CONFIG
43+
value: /pygeoapi/local.config.yml
44+
- name: PYGEOAPI_OPENAPI
45+
value: /tmp/openapi.yml
46+
- name: CONTAINER_NAME
47+
value: pygeoapi
48+
- name: CONTAINER_HOST
49+
value: 0.0.0.0
50+
- name: CONTAINER_PORT
51+
value: "5000"
52+
- name: PYGEOAPI_SERVER_URL
53+
# The URL that users will access your service through - adjust based on your Kubernetes setup
54+
value: "https://your-domain.com/api" # Replace with your actual domain and include /api path
55+
---
56+
apiVersion: v1
57+
kind: Service
58+
metadata:
59+
name: pygeoapi
60+
spec:
61+
selector:
62+
app: pygeoapi
63+
ports:
64+
- port: 80
65+
targetPort: 5000
66+
type: ClusterIP
67+
---
68+
apiVersion: apps/v1
69+
kind: Deployment
70+
metadata:
71+
name: frontend
72+
labels:
73+
app: frontend
74+
spec:
75+
replicas: 1
76+
selector:
77+
matchLabels:
78+
app: frontend
79+
template:
80+
metadata:
81+
labels:
82+
app: frontend
83+
spec:
84+
containers:
85+
- name: frontend
86+
image: ghcr.io/yejiyang/global-tsunami-risk-map-frontend:latest
87+
ports:
88+
- containerPort: 80
89+
env:
90+
- name: API_BASE_URL
91+
# The URL where the backend API is accessible from frontend clients
92+
value: "/api"
93+
resources:
94+
limits:
95+
cpu: "0.5"
96+
memory: "256Mi"
97+
requests:
98+
cpu: "0.2"
99+
memory: "128Mi"
100+
---
101+
apiVersion: v1
102+
kind: Service
103+
metadata:
104+
name: frontend
105+
spec:
106+
selector:
107+
app: frontend
108+
ports:
109+
- port: 80
110+
targetPort: 80
111+
type: ClusterIP
112+
---
113+
# Ingress controller configuration
114+
apiVersion: networking.k8s.io/v1
115+
kind: Ingress
116+
metadata:
117+
name: geospatial-ingress
118+
annotations:
119+
# Add appropriate annotations for your ingress controller
120+
# For example, for nginx:
121+
# kubernetes.io/ingress.class: nginx
122+
# nginx.ingress.kubernetes.io/rewrite-target: /
123+
spec:
124+
rules:
125+
- host: your-domain.com # Replace with your actual domain
126+
http:
127+
paths:
128+
- path: /
129+
pathType: Prefix
130+
backend:
131+
service:
132+
name: frontend
133+
port:
134+
number: 80
135+
- path: /api/
136+
pathType: Prefix
137+
backend:
138+
service:
139+
name: pygeoapi
140+
port:
141+
number: 80

k8s-frontend.yaml

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
apiVersion: apps/v1
2+
kind: Deployment
3+
metadata:
4+
name: frontend
5+
labels:
6+
app: frontend
7+
spec:
8+
replicas: 1
9+
selector:
10+
matchLabels:
11+
app: frontend
12+
template:
13+
metadata:
14+
labels:
15+
app: frontend
16+
spec:
17+
containers:
18+
- name: frontend
19+
image: ghcr.io/yejiyang/global-tsunami-risk-map-frontend:latest
20+
ports:
21+
- containerPort: 80
22+
env:
23+
- name: API_BASE_URL
24+
# The URL where the backend API is accessible from frontend clients
25+
# This should match your ingress/service path for the backend
26+
value: "/api"
27+
resources:
28+
limits:
29+
cpu: "0.5"
30+
memory: "256Mi"
31+
requests:
32+
cpu: "0.2"
33+
memory: "128Mi"
34+
---
35+
apiVersion: v1
36+
kind: Service
37+
metadata:
38+
name: frontend
39+
spec:
40+
selector:
41+
app: frontend
42+
ports:
43+
- port: 80
44+
targetPort: 80
45+
type: ClusterIP
46+
---
47+
# Optional: If you're using an Ingress controller
48+
apiVersion: networking.k8s.io/v1
49+
kind: Ingress
50+
metadata:
51+
name: frontend-ingress
52+
annotations:
53+
# Add appropriate annotations for your ingress controller
54+
# For example, for nginx:
55+
# kubernetes.io/ingress.class: nginx
56+
# nginx.ingress.kubernetes.io/rewrite-target: /
57+
spec:
58+
rules:
59+
- host: your-domain.com # Replace with your actual domain
60+
http:
61+
paths:
62+
- path: /
63+
pathType: Prefix
64+
backend:
65+
service:
66+
name: frontend
67+
port:
68+
number: 80
69+
- path: /api/
70+
pathType: Prefix
71+
backend:
72+
service:
73+
name: pygeoapi
74+
port:
75+
number: 80

k8s-pygeoapi.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ spec:
5252
- name: PYGEOAPI_SERVER_URL
5353
# The URL that users will access your service through - adjust based on your Kubernetes setup
5454
# This could be your ingress URL or load balancer address
55-
value: "https://your-domain.com" # Replace with your actual domain
55+
value: "https://your-domain.com/api" # Replace with your actual domain and include /api path
5656
---
5757
apiVersion: v1
5858
kind: Service

0 commit comments

Comments
 (0)