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
+2 -1
View File
@@ -3,11 +3,12 @@ services:
build: build:
context: ./flutter context: ./flutter
dockerfile: Dockerfile dockerfile: Dockerfile
args:
API_BASE_URL: "/api"
image: recipe-flutter:local image: recipe-flutter:local
container_name: recipe-flutter container_name: recipe-flutter
restart: unless-stopped restart: unless-stopped
environment: environment:
FLUTTER_API_URL_INTERNAL: "http://recipe-api:8080"
PORT: "5000" PORT: "5000"
ports: ports:
- "5000:5000" - "5000:5000"
+8 -1
View File
@@ -1,9 +1,16 @@
:{$PORT:5000} { :{$PORT:5000} {
root * /usr/share/caddy 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 # SPA-routing returnera alltid index.html för okända paths
handle {
try_files {path} /index.html try_files {path} /index.html
file_server
}
encode gzip encode gzip
} }
+4 -3
View File
@@ -8,10 +8,11 @@ RUN flutter pub get
COPY . . COPY . .
# Inject the internal API URL at build time via --dart-define # Inject API base URL at build time via --dart-define.
ARG FLUTTER_API_URL_INTERNAL=http://recipe-api:8080 # Default to same-origin /api to avoid mixed-content in HTTPS deployments.
ARG API_BASE_URL=/api
RUN flutter build web --release \ 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 # Stage 2 Serve with Caddy
FROM caddy:alpine AS runner FROM caddy:alpine AS runner
+4 -4
View File
@@ -1,8 +1,8 @@
import 'package:http/http.dart' as http; import 'package:http/http.dart' as http;
/// Platform-neutral HTTP client wrapping the internal API base URL. /// Platform-neutral HTTP client.
/// Base URL is read from the FLUTTER_API_URL_INTERNAL environment variable /// API base URL is injected at build time via --dart-define=API_BASE_URL.
/// (set by Docker) or falls back to localhost for local development. /// Default is same-origin '/api' to avoid mixed-content on HTTPS sites.
class ApiClient { class ApiClient {
final String baseUrl; final String baseUrl;
final http.Client _client; final http.Client _client;
@@ -10,7 +10,7 @@ class ApiClient {
ApiClient({http.Client? client}) ApiClient({http.Client? client})
: baseUrl = const String.fromEnvironment( : baseUrl = const String.fromEnvironment(
'API_BASE_URL', 'API_BASE_URL',
defaultValue: 'http://localhost:8080', defaultValue: '/api',
), ),
_client = client ?? http.Client(); _client = client ?? http.Client();