Skip to content

Commit cd25245

Browse files
Caddyfile now handles API requests under the base path.
1 parent e3f16fa commit cd25245

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

oxo2-frontend/Caddyfile

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
}
44

55
:8080 {
6-
handle /api/* {
6+
# Handle API requests under the base path
7+
handle_path {$OXO_PUBLIC_URL}api/* {
78
reverse_proxy {$OXO_BACKEND_URL}
8-
}
9+
}
10+
# Handle all other requests under the base path (frontend SPA)
911
handle_path {$OXO_PUBLIC_URL}* {
1012
root * /opt/oxo2-frontend/dist
1113
try_files {path} {path}/ /index.html

oxo2-frontend/src/app/api.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,25 @@ export async function doHTTPRequest<T>(
22
path: string,
33
request?: RequestInit | undefined
44
): Promise<T> {
5-
const BASE_URL = import.meta.env.OXO_BACKEND_URL || 'http://localhost:8081';
5+
// In production (when OXO_PUBLIC_URL is set), always use relative URLs through Caddy proxy
6+
// In local development (no OXO_PUBLIC_URL), use OXO_BACKEND_URL directly
7+
const backendUrl = import.meta.env.OXO_BACKEND_URL;
8+
const publicUrl = import.meta.env.OXO_PUBLIC_URL || '';
9+
10+
let requestUrl: string;
11+
if (publicUrl && publicUrl !== '/') {
12+
// Production: use relative URL through Caddy proxy, prefixed with base path
13+
requestUrl = `${publicUrl}${path}`;
14+
} else if (backendUrl) {
15+
// Local development: use absolute URL directly to backend
16+
requestUrl = `${backendUrl}${path}`;
17+
} else {
18+
// Fallback: use relative URL (shouldn't happen in normal operation)
19+
requestUrl = path;
20+
}
21+
622
const response: Response = await fetch(
7-
`${BASE_URL}${path}`,
23+
requestUrl,
824
{
925
...(request ? request : {}),
1026
}

0 commit comments

Comments
 (0)