This commit is contained in:
Joshua Hare 2025-08-27 16:19:52 +10:00
parent 85f980be0a
commit 3f1ac10290
2 changed files with 50 additions and 21 deletions

54
app.py
View File

@ -1780,41 +1780,59 @@ def fhir_ui_operations():
# Use a single route to capture everything after /fhir/
# The 'path' converter handles slashes. 'subpath' can be empty.
@app.route('/fhir', defaults={'subpath': ''}, methods=['GET', 'POST', 'PUT', 'DELETE', 'PATCH'])
@app.route('/fhir/', defaults={'subpath': ''}, methods=['GET', 'POST', 'PUT', 'DELETE', 'PATCH'])
@app.route('/fhir/<path:subpath>', methods=['GET', 'POST', 'PUT', 'DELETE', 'PATCH'])
@app.route('/fhir', defaults={'subpath': ''}, methods=['GET', 'POST', 'PUT', 'DELETE'])
@app.route('/fhir/', defaults={'subpath': ''}, methods=['GET', 'POST', 'PUT', 'DELETE'])
@app.route('/fhir/<path:subpath>', methods=['GET', 'POST', 'PUT', 'DELETE'])
def proxy_hapi(subpath):
"""
Proxies FHIR requests to either the local HAPI server or a custom
target server specified by the 'X-Target-FHIR-Server' header or a query parameter.
target server specified by the 'X-Target-FHIR-Server' header.
Handles requests to /fhir/ (base, subpath='') and /fhir/<subpath>.
The route '/fhir' (no trailing slash) is handled separately for the UI.
"""
logger.debug(f"Proxy received request for path: '/fhir/{subpath}'")
# Clean subpath just in case prefixes were somehow included
clean_subpath = subpath.replace('r4/', '', 1).replace('fhir/', '', 1).strip('/')
logger.debug(f"Proxy received request for path: '/fhir/{subpath}', cleaned subpath: '{clean_subpath}'")
# Determine the target FHIR server base URL
# NEW: Check for proxy-target query parameter first
target_server_query = request.args.get('proxy-target')
target_server_header = request.headers.get('X-Target-FHIR-Server')
final_base_url = None
is_custom_target = False
if target_server_query:
final_base_url = target_server_query.rstrip('/')
is_custom_target = True
logger.info(f"Proxy target identified from query parameter: {final_base_url}")
try:
parsed_url = urlparse(target_server_query)
if not parsed_url.scheme or not parsed_url.netloc:
raise ValueError("Invalid URL format in proxy-target query parameter")
final_base_url = target_server_query.rstrip('/')
is_custom_target = True
logger.info(f"Proxy target identified from query parameter: {final_base_url}")
except ValueError as e:
logger.warning(f"Invalid URL in proxy-target query parameter: '{target_server_query}'. Falling back. Error: {e}")
final_base_url = current_app.config['HAPI_FHIR_URL'].rstrip('/')
logger.debug(f"Falling back to default local HAPI due to invalid query: {final_base_url}")
elif target_server_header:
final_base_url = target_server_header.rstrip('/')
is_custom_target = True
logger.info(f"Proxy target identified from header: {final_base_url}")
try:
parsed_url = urlparse(target_server_header)
if not parsed_url.scheme or not parsed_url.netloc:
raise ValueError("Invalid URL format in X-Target-FHIR-Server header")
final_base_url = target_server_header.rstrip('/')
is_custom_target = True
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 = 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 = current_app.config['HAPI_FHIR_URL'].rstrip('/')
logger.debug(f"No target info found, proxying to default local HAPI: {final_base_url}")
# Construct the final URL for the target server request. This is the core logical change.
# The subpath variable now correctly captures the remaining part of the URL after '/fhir/'.
# We construct the final URL by joining the base URL and the subpath.
final_url = f"{final_base_url}/{subpath}" if subpath else final_base_url
logger.debug(f"Final URL for target server: {final_url}")
# Construct the final URL for the target server request
# Append the cleaned subpath only if it's not empty
final_url = f"{final_base_url}/{clean_subpath}" if clean_subpath else final_base_url
# Prepare headers to forward
headers_to_forward = {
k: v for k, v in request.headers.items()

View File

@ -382,10 +382,10 @@ document.addEventListener('DOMContentLoaded', () => {
let fetchedMetadataCache = null;
let operationDefinitionCache = {}; // <<< ADD THIS LINE: Cache for fetched OperationDefinitions
// <<< ADD THIS SECTION: Get App Mode >>>
const appMode = '{{ app_mode | default("standalone") | lower }}'; // Get mode from Flask
const appMode = '{{ app_mode | default("standalone") | lower }}';
const initialFhirServerUrl = '{{ app.config["HAPI_FHIR_URL"] | safe }}';
console.log(`App Mode (Operations): ${appMode}`);
// <<< END ADD >>>
console.log(`Initial FHIR Server URL from config: ${initialFhirServerUrl}`);
// --- Helper Functions ---
function updateAuthInputsUI() {
@ -1950,6 +1950,17 @@ document.addEventListener('DOMContentLoaded', () => {
// --- Initial Page State Setup ---
resourceTypesDisplayDiv.style.display = 'none';
swaggerUiContainer.style.display = 'none';
// Check if the initial URL from config is the default local Hapi URL
const isInitialLocalHapi = initialFhirServerUrl.startsWith('http://localhost:8080');
// Set the initial state based on the URL from Flask config
isUsingLocalHapi = isInitialLocalHapi;
if (!isUsingLocalHapi) {
// If it's a custom URL, populate the input field with it
fhirServerUrlInput.value = initialFhirServerUrl;
}
updateServerToggleUI(); // Set the initial UI state correctly based on default isUsingLocalHapi = true
}); // End DOMContentLoaded Listener