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
3 changes: 3 additions & 0 deletions src/murfey/server/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,9 @@ def respond_with_template(
"hostname": get_hostname(),
"microscope": get_microscope(),
"version": murfey.__version__,
# Extra parameters to reconstruct URLs for forwarded requests
"netloc": request.url.netloc,
"proxy_path": "",
}
if parameters:
template_parameters.update(parameters)
Expand Down
99 changes: 94 additions & 5 deletions src/murfey/server/api/bootstrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import logging
import random
import re
import zipfile
from io import BytesIO
from urllib.parse import quote

Expand Down Expand Up @@ -105,8 +106,23 @@
machine with no internet access.
"""

# Constructs the netloc (hostname + port) and proxy path depending on if the
# request was forwarded via proxy
netloc = (

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L111 was not covered by tests
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
proxy_path = request.url.path.removesuffix(f"{bootstrap.prefix}/")

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L118 was not covered by tests

return respond_with_template(
request=request,
parameters={
"netloc": netloc,
"proxy_path": proxy_path,
},
filename="bootstrap.html",
)

Expand Down Expand Up @@ -314,7 +330,80 @@
)


@msys2.get("/distrib/{setup_file}", response_class=StreamingResponse)
@msys2.get("/config/pacman.d.zip", response_class=StreamingResponse)
def get_pacman_mirrors(request: Request):
"""
Dynamically generates a zip file containing mirrorlist files that have been set
up to mirror the MSYS2 package database for each environment.

The files in this folder should be pasted into, and overwrite, the 'mirrorlist'
files present in the %MSYS64%\\etc\\pacman.d folder. The default path to this
folder is C:\\msys64\\etc\\pacman.d.
"""

# Check if this is a forwarded request from somewhere else and construct netloc
netloc = (

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L345 was not covered by tests
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
)

# Find path to Rust router using current URL Path
path_to_router = request.url.path.removesuffix("/config/pacman.d.zip")

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L353 was not covered by tests

# Construct base URL for subsequent use
base_url = f"{request.url.scheme}://{netloc}{path_to_router}"
logger.debug(f"Base URL to MSYS2 sub-router determined to be {base_url}")

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

View check run for this annotation

Codecov / codecov/patch

src/murfey/server/api/bootstrap.py#L356-L357

Added lines #L356 - L357 were not covered by tests

# Construct package database mirrors
# Files are called mirrorlist.{environment}
# URL format: {scheme}://{netloc}{proxy_path}/{router_prefix}/path/to/repo
url_paths = {

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L362 was not covered by tests
"clang64": "mingw/clang64",
"mingw": "mingw/$repo",
"mingw32": "mingw/i686",
"mingw64": "mingw/x86_64",
"msys": "msys/$arch",
"ucrt64": "mingw/ucrt64",
}
# Construct file names and contents
mirror_lists = {

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L371 was not covered by tests
f"mirrorlist.{env}": "\n".join(
[
"# See https://www.msys2.org/dev/mirrors",
"",
"## Primary",
f"Server = {base_url}/repo/{repo_path}",
"",
]
)
for env, repo_path in url_paths.items()
}

# Create in-memory buffer for the ZIP file
zip_buffer = BytesIO()

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L385 was not covered by tests

# Create a zip file in the buffer
with zipfile.ZipFile(zip_buffer, "w", zipfile.ZIP_DEFLATED) as zip_file:

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L388 was not covered by tests
for file_name, content in mirror_lists.items():
zip_file.writestr(file_name, content)
zip_buffer.seek(0) # Move object pointer back to start

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

View check run for this annotation

Codecov / codecov/patch

src/murfey/server/api/bootstrap.py#L390-L391

Added lines #L390 - L391 were not covered by tests

# Construct and return streaming response
headers = {

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L394 was not covered by tests
"Content-Disposition": "attachment; filename=pacman.d.zip",
"Content-Length": str(zip_buffer.getbuffer().nbytes),
}
return StreamingResponse(

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L398 was not covered by tests
zip_buffer,
status_code=200,
headers=headers,
media_type="application/zip",
)


@msys2.get("/repo/distrib/{setup_file}", response_class=StreamingResponse)
def get_msys2_setup(
request: Request,
setup_file: str,
Expand Down Expand Up @@ -351,7 +440,7 @@
)


@msys2.get("/", response_class=Response)
@msys2.get("/repo/", response_class=Response)
def get_msys2_main_index(
request: Request,
) -> Response:
Expand Down Expand Up @@ -391,7 +480,7 @@
)


@msys2.get("/{system}/", response_class=Response)
@msys2.get("/repo/{system}/", response_class=Response)
def get_msys2_environment_index(
request: Request,
system: str,
Expand Down Expand Up @@ -435,7 +524,7 @@
)


@msys2.get("/{system}/{environment}/", response_class=Response)
@msys2.get("/repo/{system}/{environment}/", response_class=Response)
def get_msys2_package_index(
request: Request,
system: str,
Expand Down Expand Up @@ -464,7 +553,7 @@
)


@msys2.get("/{system}/{environment}/{package}", response_class=StreamingResponse)
@msys2.get("/repo/{system}/{environment}/{package}", response_class=StreamingResponse)
def get_msys2_package_file(
request: Request,
system: str,
Expand Down
16 changes: 9 additions & 7 deletions src/murfey/templates/base.html
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
<html>
<head>
<title>Murfey - {% block title %}{% endblock %}</title>
<link href="{{ url_for('static', path='/styles.css') }}" rel="stylesheet" />
<link
href="{{ url_for('images', path='/icon_268.png') }}"
href="{{ proxy_path }}{{ url_for('static', path='/styles.css') }}"
rel="stylesheet"
/>
<link
href="{{ proxy_path }}{{ url_for('images', path='/icon_268.png') }}"
rel="icon"
type="image/png"
/>
</head>
<body>
<div class="topnav">
<a href="/">Home</a>
<a href="/visits">Active Visits</a>
<a href="/bootstrap">Installation instructions</a>
<a href="/pypi/fastapi">FastAPI PyPI</a>
<a href="{{ proxy_path }}/docs">Home</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="{{ url_for('images', path='/diamond.png') }}"
src="{{ proxy_path }}{{ url_for('images', path='/diamond.png') }}"
style="height: 25px"
/>
</p>
Expand Down
Loading