feat: update API base URL handling and improve Caddy configuration for Flutter app

This commit is contained in:
Nils-Johan Gynther
2026-04-21 21:59:47 +02:00
parent ac5891394e
commit 39384a0e74
4 changed files with 19 additions and 10 deletions
+9 -2
View File
@@ -1,9 +1,16 @@
:{$PORT:5000} {
root * /usr/share/caddy
file_server
# Proxy API calls to backend service on the internal Docker network.
handle /api/* {
reverse_proxy recipe-api:8080
}
# SPA-routing returnera alltid index.html för okända paths
try_files {path} /index.html
handle {
try_files {path} /index.html
file_server
}
encode gzip
}
+4 -3
View File
@@ -8,10 +8,11 @@ RUN flutter pub get
COPY . .
# Inject the internal API URL at build time via --dart-define
ARG FLUTTER_API_URL_INTERNAL=http://recipe-api:8080
# Inject API base URL at build time via --dart-define.
# Default to same-origin /api to avoid mixed-content in HTTPS deployments.
ARG API_BASE_URL=/api
RUN flutter build web --release \
--dart-define=API_BASE_URL=${FLUTTER_API_URL_INTERNAL}
--dart-define=API_BASE_URL=${API_BASE_URL}
# Stage 2 Serve with Caddy
FROM caddy:alpine AS runner
+4 -4
View File
@@ -1,8 +1,8 @@
import 'package:http/http.dart' as http;
/// Platform-neutral HTTP client wrapping the internal API base URL.
/// Base URL is read from the FLUTTER_API_URL_INTERNAL environment variable
/// (set by Docker) or falls back to localhost for local development.
/// Platform-neutral HTTP client.
/// API base URL is injected at build time via --dart-define=API_BASE_URL.
/// Default is same-origin '/api' to avoid mixed-content on HTTPS sites.
class ApiClient {
final String baseUrl;
final http.Client _client;
@@ -10,7 +10,7 @@ class ApiClient {
ApiClient({http.Client? client})
: baseUrl = const String.fromEnvironment(
'API_BASE_URL',
defaultValue: 'http://localhost:8080',
defaultValue: '/api',
),
_client = client ?? http.Client();