From 179982dedc0c270c9961557b4a58c985d33296bb Mon Sep 17 00:00:00 2001 From: Sudo-JHare Date: Tue, 13 May 2025 12:37:04 +1000 Subject: [PATCH] Dynamic Hostname fix Patch for dynamic host names --- app.py | 8 +++++--- docker-compose.yml | 4 +++- services.py | 4 ++-- 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/app.py b/app.py index 574eba9..5974120 100644 --- a/app.py +++ b/app.py @@ -55,6 +55,8 @@ app.config['API_KEY'] = os.environ.get('API_KEY', 'your-fallback-api-key-here') app.config['VALIDATE_IMPOSED_PROFILES'] = True app.config['DISPLAY_PROFILE_RELATIONSHIPS'] = True app.config['UPLOAD_FOLDER'] = '/app/static/uploads' # For GoFSH output +app.config['APP_BASE_URL'] = os.environ.get('APP_BASE_URL', 'http://localhost:5000') +app.config['HAPI_FHIR_URL'] = os.environ.get('HAPI_FHIR_URL', 'http://localhost:8080/fhir') CONFIG_PATH = '/usr/local/tomcat/conf/application.yaml' # Register blueprints immediately after app setup @@ -1317,10 +1319,10 @@ def proxy_hapi(subpath): logger.info(f"Proxy target identified from header: {final_base_url}") except ValueError as e: logger.warning(f"Invalid URL in X-Target-FHIR-Server header: '{target_server_header}'. Falling back. Error: {e}") - final_base_url = "http://localhost:8080/fhir" + final_base_url = current_app.config['HAPI_FHIR_URL'].rstrip('/') logger.debug(f"Falling back to default local HAPI due to invalid header: {final_base_url}") else: - final_base_url = "http://localhost:8080/fhir" + final_base_url = current_app.config['HAPI_FHIR_URL'].rstrip('/') logger.debug(f"No target header found, proxying to default local HAPI: {final_base_url}") # Construct the final URL for the target server request @@ -1424,7 +1426,7 @@ def load_ig_to_hapi(): resource_id = resource.get('id') if resource_type and resource_id: response = requests.put( - f"http://localhost:8080/fhir/{resource_type}/{resource_id}", + f"{current_app.config['HAPI_FHIR_URL'].rstrip('/')}/{resource_type}/{resource_id}", json=resource, headers={'Content-Type': 'application/fhir+json'} ) diff --git a/docker-compose.yml b/docker-compose.yml index f9cc1b2..66b2ad0 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -16,5 +16,7 @@ services: - FLASK_APP=app.py - FLASK_ENV=development - NODE_PATH=/usr/lib/node_modules - - APP_MODE=lite + - APP_MODE=lite + - APP_BASE_URL=https://yourdomain.com + - HAPI_FHIR_URL=https://hapi.yourdomain.com/fhir command: supervisord -c /etc/supervisord.conf diff --git a/services.py b/services.py index 749019c..4b0db4d 100644 --- a/services.py +++ b/services.py @@ -1616,7 +1616,7 @@ def validate_resource_against_profile(package_name, version, resource, include_d # Attempt HAPI validation if a profile is specified if result['profile']: try: - hapi_url = f"http://localhost:8080/fhir/{resource['resourceType']}/$validate?profile={result['profile']}" + hapi_url = f"{current_app.config['HAPI_FHIR_URL'].rstrip('/')}/{resource['resourceType']}/$validate?profile={result['profile']}" response = requests.post( hapi_url, json=resource, @@ -3766,7 +3766,7 @@ def retrieve_bundles(fhir_server_url, resources, output_zip, validate_references # --- Determine Base URL and Headers for Proxy --- - base_proxy_url = 'http://localhost:5000/fhir' # Always target the proxy endpoint + base_proxy_url = f"{current_app.config['APP_BASE_URL'].rstrip('/')}/fhir" headers = {'Accept': 'application/fhir+json, application/fhir+xml;q=0.9, */*;q=0.8'} is_custom_url = fhir_server_url != '/fhir' and fhir_server_url is not None and fhir_server_url.startswith('http') if is_custom_url: