diff --git a/compose.flutter.yml b/compose.flutter.yml index cc0ed47c..00ccb192 100644 --- a/compose.flutter.yml +++ b/compose.flutter.yml @@ -3,11 +3,12 @@ services: build: context: ./flutter dockerfile: Dockerfile + args: + API_BASE_URL: "/api" image: recipe-flutter:local container_name: recipe-flutter restart: unless-stopped environment: - FLUTTER_API_URL_INTERNAL: "http://recipe-api:8080" PORT: "5000" ports: - "5000:5000" diff --git a/flutter/Caddyfile b/flutter/Caddyfile index 385d3699..03a88ca3 100644 --- a/flutter/Caddyfile +++ b/flutter/Caddyfile @@ -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 } diff --git a/flutter/Dockerfile b/flutter/Dockerfile index 95c0a656..40c1470b 100644 --- a/flutter/Dockerfile +++ b/flutter/Dockerfile @@ -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 diff --git a/flutter/lib/core/api/api_client.dart b/flutter/lib/core/api/api_client.dart index 75d641a0..b712d1bb 100644 --- a/flutter/lib/core/api/api_client.dart +++ b/flutter/lib/core/api/api_client.dart @@ -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();