feat: update API base URL handling and improve Caddy configuration for Flutter app
This commit is contained in:
+2
-1
@@ -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"
|
||||
|
||||
+8
-1
@@ -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
|
||||
handle {
|
||||
try_files {path} /index.html
|
||||
file_server
|
||||
}
|
||||
|
||||
encode gzip
|
||||
}
|
||||
|
||||
+4
-3
@@ -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
|
||||
|
||||
@@ -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();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user