| """ |
| Utility functions for URL detection and management. |
| Automatically detects frontend URL from cloud platform environment variables, |
| request headers, or falls back to environment variables. |
| """ |
| import os |
| from typing import Optional |
| from fastapi import Request |
|
|
|
|
| def get_frontend_url(request: Optional[Request] = None) -> str: |
| """ |
| Automatically detect the frontend URL from various sources. |
| |
| Priority order: |
| 1. FRONTEND_URL environment variable (manual override) |
| 2. Cloud platform environment variables (AWS ECS, Azure Container Instances, etc.) |
| 3. Request headers (Origin, Referer, X-Forwarded-Host) |
| 4. Default fallback to localhost |
| |
| Args: |
| request: Optional FastAPI Request object to extract headers |
| |
| Returns: |
| Frontend URL string (without trailing slash) |
| """ |
| |
| |
| from utils.config_loader import get_setting |
| frontend_url = get_setting("FRONTEND_URL") |
| if frontend_url: |
| return frontend_url.rstrip('/') |
| |
| |
| |
| aws_container_url = os.getenv("AWS_CONTAINER_URL") or os.getenv("ECS_CONTAINER_METADATA_URI_V4") |
| if aws_container_url: |
| |
| |
| |
| pass |
| |
| |
| azure_website_hostname = os.getenv("WEBSITE_HOSTNAME") |
| azure_container_url = os.getenv("AZURE_CONTAINER_URL") |
| if azure_website_hostname: |
| |
| scheme = "https" if os.getenv("HTTPS") == "on" else "http" |
| return f"{scheme}://{azure_website_hostname}".rstrip('/') |
| if azure_container_url: |
| return azure_container_url.rstrip('/') |
| |
| |
| |
| public_url = os.getenv("PUBLIC_URL") or os.getenv("BASE_URL") or os.getenv("APP_URL") |
| if public_url: |
| return public_url.rstrip('/') |
| |
| |
| if request: |
| |
| origin = request.headers.get("origin") |
| if origin: |
| return origin.rstrip('/') |
| |
| |
| referer = request.headers.get("referer") |
| if referer: |
| |
| from urllib.parse import urlparse |
| parsed = urlparse(referer) |
| if parsed.scheme and parsed.netloc: |
| return f"{parsed.scheme}://{parsed.netloc}".rstrip('/') |
| |
| |
| forwarded_host = request.headers.get("x-forwarded-host") |
| forwarded_proto = request.headers.get("x-forwarded-proto", "https") |
| if forwarded_host: |
| return f"{forwarded_proto}://{forwarded_host}".rstrip('/') |
| |
| |
| host = request.headers.get("host") |
| if host: |
| |
| scheme = request.headers.get("x-forwarded-proto", "https" if request.url.scheme == "https" else "http") |
| |
| if "localhost" not in host.lower() and "127.0.0.1" not in host: |
| scheme = "https" |
| return f"{scheme}://{host}".rstrip('/') |
| |
| |
| |
| if os.getenv("AWS_EXECUTION_ENV") or os.getenv("NODE_ENV") == "production": |
| return "https://medisync-ai1.vercel.app" |
| |
| return "http://localhost:3000" |
|
|
|
|
| def get_backend_url(request: Optional[Request] = None) -> str: |
| """ |
| Automatically detect the backend URL (useful for internal redirects). |
| |
| Args: |
| request: Optional FastAPI Request object |
| |
| Returns: |
| Backend URL string (without trailing slash) |
| """ |
| |
| backend_url = os.getenv("BACKEND_URL") |
| if backend_url: |
| return backend_url.rstrip('/') |
| |
| |
| if request: |
| base_url = str(request.base_url).rstrip('/') |
| return base_url |
| |
| |
| return "http://localhost:8000" |
|
|
|
|