Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 33 additions & 27 deletions src/murfey/server/api/bootstrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,32 @@
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(

Check warning on line 83 in src/murfey/server/api/bootstrap.py

View check run for this annotation

Codecov / codecov/patch

src/murfey/server/api/bootstrap.py#L82-L83

Added lines #L82 - L83 were not covered by tests
"X-Forwarded-Port", str(request.url.port) if request.url.port else None
)
proto = request.headers.get("X-Forwarded-Proto", request.url.scheme)

Check warning on line 86 in src/murfey/server/api/bootstrap.py

View check run for this annotation

Codecov / codecov/patch

src/murfey/server/api/bootstrap.py#L86

Added line #L86 was not covered by tests

# 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

Check warning on line 94 in src/murfey/server/api/bootstrap.py

View check run for this annotation

Codecov / codecov/patch

src/murfey/server/api/bootstrap.py#L94

Added line #L94 was not covered by tests

return f"{host}:{port}"

Check warning on line 96 in src/murfey/server/api/bootstrap.py

View check run for this annotation

Codecov / codecov/patch

src/murfey/server/api/bootstrap.py#L96

Added line #L96 was not covered by tests


"""
=======================================================================================
VERSION-RELATED API ENDPOINTS
Expand Down Expand Up @@ -126,15 +152,10 @@
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)

Check warning on line 156 in src/murfey/server/api/bootstrap.py

View check run for this annotation

Codecov / codecov/patch

src/murfey/server/api/bootstrap.py#L156

Added line #L156 was not covered by tests

# Find path to 'bootstrap' router using current URL path
proxy_path = request.url.path.removesuffix(f"{bootstrap.prefix}/")

return respond_with_template(
Expand Down Expand Up @@ -362,12 +383,7 @@
"""

# 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)

Check warning on line 386 in src/murfey/server/api/bootstrap.py

View check run for this annotation

Codecov / codecov/patch

src/murfey/server/api/bootstrap.py#L386

Added line #L386 was not covered by tests

# Find path to Rust router using current URL Path
path_to_router = request.url.path.removesuffix("/config/pacman.d.zip")
Expand Down Expand Up @@ -641,12 +657,7 @@
"""

# 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)

Check warning on line 660 in src/murfey/server/api/bootstrap.py

View check run for this annotation

Codecov / codecov/patch

src/murfey/server/api/bootstrap.py#L660

Added line #L660 was not covered by tests

# Find path to Rust router using current URL Path
path_to_router = request.url.path.removesuffix("/cargo/config.toml")
Expand Down Expand Up @@ -723,12 +734,7 @@
"""

# 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)

Check warning on line 737 in src/murfey/server/api/bootstrap.py

View check run for this annotation

Codecov / codecov/patch

src/murfey/server/api/bootstrap.py#L737

Added line #L737 was not covered by tests

# Find path to Rust router using current URL Path
path_to_router = request.url.path.removesuffix("/index/config.json")
Expand Down
24 changes: 2 additions & 22 deletions src/murfey/server/api/session_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand All @@ -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()
Expand Down
6 changes: 3 additions & 3 deletions src/murfey/templates/activevisits.html
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<html>
<head>
<title>{{ microscope }}</title>
<link href="{{ url_for('static', path='/styles.css') }}" rel="stylesheet" />
<title>Visits {{ "({})".format(microscope) if microscope else "" }}</title>
<link href="{{ proxy_path }}/static/styles.css" rel="stylesheet" />
<link
href="{{ url_for('images', path='/icon_268.png') }}"
href="{{ proxy_path }}/images/icon_268.png"
rel="icon"
type="image/png"
/>
Expand Down
19 changes: 7 additions & 12 deletions src/murfey/templates/base.html
Original file line number Diff line number Diff line change
@@ -1,26 +1,20 @@
<html>
<head>
<title>Murfey - {% block title %}{% endblock %}</title>
<link href="{{ proxy_path }}/static/styles.css" rel="stylesheet" />
<link
href="{{ proxy_path }}{{ url_for('static', path='/styles.css') }}"
rel="stylesheet"
/>
<link
href="{{ proxy_path }}{{ url_for('images', path='/icon_268.png') }}"
href="{{ proxy_path }}/images/icon_268.png"
rel="icon"
type="image/png"
/>
</head>
<body>
<div class="topnav">
<a href="{{ proxy_path }}/docs">Home</a>
<a href="{{ proxy_path }}/bootstrap">Installation Instructions</a>
<a href="{{ proxy_path }}/pypi/fastapi">FastAPI (PyPI)</a>
<a href="{{ proxy_path }}/bootstrap/">Installation Instructions</a>
<a href="{{ proxy_path }}/pypi/fastapi/">FastAPI (PyPI)</a>
<p style="text-align: right; margin: 8px 10px 0 auto">
<img
src="{{ proxy_path }}{{ url_for('images', path='/diamond.png') }}"
style="height: 25px"
/>
<img src="{{ proxy_path }}/images/diamond.png" style="height: 25px" />
</p>
</div>

Expand All @@ -31,7 +25,8 @@
For help please contact your local contact or the EM Data Analysis team.
</p>
<p style="font-size: 60%">
Murfey v{{ version }} running on {{ hostname }} ({{ microscope }})
Murfey v{{ version }} running on {{ hostname }} {{
"({})".format(microscope) if microscope else "" }}
</p>
</div>
</body>
Expand Down
8 changes: 0 additions & 8 deletions src/murfey/templates/home.html

This file was deleted.

32 changes: 0 additions & 32 deletions src/murfey/templates/visit.html

This file was deleted.

5 changes: 3 additions & 2 deletions tests/server/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,11 @@ def login(test_user):
@patch("murfey.server.api.auth.check_user", return_value=True)
def test_read_main(mock_check, test_user):
token = login(test_user)
response = client.get("/session_info", headers={"Authorization": f"Bearer {token}"})
response = client.get(
"/session_info/connections", headers={"Authorization": f"Bearer {token}"}
)
assert mock_check.called_once()
assert response.status_code == 200
assert "<title>" in response.text.lower()


def test_pypi_proxy():
Expand Down
Loading