diff --git a/src/murfey/server/api/bootstrap.py b/src/murfey/server/api/bootstrap.py index 879bbfb11..a88d0c243 100644 --- a/src/murfey/server/api/bootstrap.py +++ b/src/murfey/server/api/bootstrap.py @@ -70,6 +70,32 @@ def _sanitise_str(input: str) -> str: return input_clean +def resolve_netloc(request: Request) -> str: + """ + Helper function to construct the correct netloc (hostname[:port]) to use based on + the request received. It will prioritise parsing the request headers for the host, + port, protocol and using them to construct the netloc before defaulting to parsing + the FastAPI Request object to do so. + """ + + # Prefer headers added by reverse proxies + host = request.headers.get("X-Forwarded-Host", request.url.hostname) + port = request.headers.get( + "X-Forwarded-Port", str(request.url.port) if request.url.port else None + ) + proto = request.headers.get("X-Forwarded-Proto", request.url.scheme) + + # Default ports shouldn't be included; if no ports are found, return just the host + if ( + (proto == "http" and port == "80") + or (proto == "https" and port == "443") + or not port + ): + return host + + return f"{host}:{port}" + + """ ======================================================================================= VERSION-RELATED API ENDPOINTS @@ -126,15 +152,10 @@ def get_bootstrap_instructions(request: Request): machine with no internet access. """ - # Constructs the netloc (hostname + port) and proxy path depending on if the - # request was forwarded via proxy - netloc = ( - f"{request.headers['X-Forwarded-Host']}:{request.headers['X-Forwarded-Port']}" - if request.headers.get("X-Forwarded-Host") - and request.headers.get("X-Forwarded-Port") - else request.url.netloc - ) - # Additional bit in URL path after the netloc caused by the proxy reroute + # Check if this is a forwarded request from somewhere else and construct netloc + netloc = resolve_netloc(request) + + # Find path to 'bootstrap' router using current URL path proxy_path = request.url.path.removesuffix(f"{bootstrap.prefix}/") return respond_with_template( @@ -362,12 +383,7 @@ def get_pacman_mirrors(request: Request): """ # Check if this is a forwarded request from somewhere else and construct netloc - netloc = ( - f"{request.headers['X-Forwarded-Host']}:{request.headers['X-Forwarded-Port']}" - if request.headers.get("X-Forwarded-Host") - and request.headers.get("X-Forwarded-Port") - else request.url.netloc - ) + netloc = resolve_netloc(request) # Find path to Rust router using current URL Path path_to_router = request.url.path.removesuffix("/config/pacman.d.zip") @@ -641,12 +657,7 @@ def get_cargo_config(request: Request): """ # Check if this is a forwarded request from somewhere else and construct netloc - netloc = ( - f"{request.headers['X-Forwarded-Host']}:{request.headers['X-Forwarded-Port']}" - if request.headers.get("X-Forwarded-Host") - and request.headers.get("X-Forwarded-Port") - else request.url.netloc - ) + netloc = resolve_netloc(request) # Find path to Rust router using current URL Path path_to_router = request.url.path.removesuffix("/cargo/config.toml") @@ -723,12 +734,7 @@ def get_index_config(request: Request): """ # Check if this is a forwarded request from somewhere else and construct netloc - netloc = ( - f"{request.headers['X-Forwarded-Host']}:{request.headers['X-Forwarded-Port']}" - if request.headers.get("X-Forwarded-Host") - and request.headers.get("X-Forwarded-Port") - else request.url.netloc - ) + netloc = resolve_netloc(request) # Find path to Rust router using current URL Path path_to_router = request.url.path.removesuffix("/index/config.json") diff --git a/src/murfey/server/api/session_info.py b/src/murfey/server/api/session_info.py index 50e38ba2b..a844cb822 100644 --- a/src/murfey/server/api/session_info.py +++ b/src/murfey/server/api/session_info.py @@ -4,13 +4,12 @@ from typing import Dict, List, Optional from fastapi import APIRouter, Depends, Request -from fastapi.responses import FileResponse, HTMLResponse +from fastapi.responses import FileResponse from pydantic import BaseModel from sqlalchemy import func from sqlmodel import select from werkzeug.utils import secure_filename -import murfey import murfey.server.api.websocket as ws from murfey.server import _transport_object from murfey.server.api import templates @@ -32,12 +31,7 @@ from murfey.server.ispyb import get_all_ongoing_visits from murfey.server.murfey_db import murfey_db from murfey.util import sanitise -from murfey.util.config import ( - MachineConfig, - get_hostname, - get_machine_config, - get_microscope, -) +from murfey.util.config import MachineConfig, get_machine_config from murfey.util.db import ( ClientEnvironment, DataCollection, @@ -64,20 +58,6 @@ ) -# This will be the homepage for a given microscope. -@router.get("/", response_class=HTMLResponse) -async def root(request: Request): - return templates.TemplateResponse( - request=request, - name="home.html", - context={ - "hostname": get_hostname(), - "microscope": get_microscope(), - "version": murfey.__version__, - }, - ) - - @router.get("/health/") def health_check(db=ispyb_db): conn = db.connection() diff --git a/src/murfey/templates/activevisits.html b/src/murfey/templates/activevisits.html index 279f67c10..77ec4d05c 100644 --- a/src/murfey/templates/activevisits.html +++ b/src/murfey/templates/activevisits.html @@ -1,9 +1,9 @@
-- Murfey v{{ version }} running on {{ hostname }} ({{ microscope }}) + Murfey v{{ version }} running on {{ hostname }} {{ + "({})".format(microscope) if microscope else "" }}
diff --git a/src/murfey/templates/home.html b/src/murfey/templates/home.html deleted file mode 100644 index 47b9604a9..000000000 --- a/src/murfey/templates/home.html +++ /dev/null @@ -1,8 +0,0 @@ -{% extends "base.html" %} {% block title %}MURFEY{% endblock %} {% block content -%} -- A transporter for data from Diamond eBIC microscope and detector machines onto - the Diamond network. -
-{% endblock %} diff --git a/src/murfey/templates/visit.html b/src/murfey/templates/visit.html deleted file mode 100644 index 097279dc6..000000000 --- a/src/murfey/templates/visit.html +++ /dev/null @@ -1,32 +0,0 @@ - - -Time remaining: {{ visit[0]['Time remaining'] }}
-Start time: {{ visit[0]['Start date'] }}
-End time: {{ visit[0]['End date'] }}
-