Skip to content

Commit

Permalink
fix(demos): change demodata signed urls to serve minio bytes instead (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
jayceslesar authored Jun 22, 2024
1 parent f0647d3 commit 0dc7682
Show file tree
Hide file tree
Showing 10 changed files with 136 additions and 110 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/integration.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ jobs:
- name: Create the bucket
run: ./mc alias set blobs http://127.0.0.1:9000 MEGASCATTERBOMB masterbase
- name: more minio bs
run: ./mc mb -p blobs/demos
run: ./mc mb -p blobs/demoblobs
- name: Remove mc client
run: rm -v ./mc

Expand Down
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## v1.1.1 - 2024-06-22

### Fixed

- Demo downloading now fixed

## v1.1.0 - 2024-06-07

### Added
Expand Down
3 changes: 2 additions & 1 deletion docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ services:
container_name: minio
ports:
- "9000:9000"
- "9001:9001"
environment:
MINIO_ROOT_USER: MEGASCATTERBOMB
MINIO_ROOT_PASSWORD: masterbase
Expand Down Expand Up @@ -50,6 +49,8 @@ services:
MINIO_SECRET_KEY: masterbase
ports:
- "8000:8000"
depends_on:
- minio


networks:
Expand Down
24 changes: 14 additions & 10 deletions masterbase/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,15 +156,19 @@ def list_demos(


@get("/demodata", guards=[valid_key_guard, session_closed_guard, analyst_guard])
async def demodata(request: Request, api_key: str, session_id: str) -> Redirect:
async def demodata(request: Request, api_key: str, session_id: str) -> Stream:
"""Return the demo."""
minio_client = request.app.state.minio_client
url = minio_client.presigned_get_object("demos", demo_blob_name(session_id))
return Redirect(
path=url,
status_code=303,
headers={"Content-Type": "application/octet-stream"},
)
blob_name = demo_blob_name(session_id)
file = minio_client.get_object("demoblobs", blob_name)
stat = minio_client.stat_object("demoblobs", blob_name)

headers = {
"Content-Disposition": f'attachment; filename="{blob_name}"',
"Content-Length": str(stat.size),
}

return Stream(file.stream(), media_type=MediaType.TEXT, headers=headers)


@get("/db_export", guards=[valid_key_guard, analyst_guard], sync_to_thread=False)
Expand Down Expand Up @@ -377,12 +381,11 @@ def provision_handler(request: Request) -> str:
def plain_text_exception_handler(_: Request, exc: Exception) -> Response:
"""Handle exceptions subclassed from HTTPException."""
status_code = getattr(exc, "status_code", HTTP_500_INTERNAL_SERVER_ERROR)
detail = getattr(exc, "detail", "")
logger.error(detail)
logger.error("Exception occurred!", exc_info=exc)

return Response(
media_type=MediaType.TEXT,
content="Internal Error Occurred",
content="Internal Error Occurred!",
status_code=status_code,
)

Expand All @@ -402,6 +405,7 @@ def plain_text_exception_handler(_: Request, exc: Exception) -> Response:
report_player,
db_export,
],
exception_handlers={Exception: plain_text_exception_handler},
on_shutdown=shutdown_registers,
opt={"DEVELOPMENT": bool(os.getenv("DEVELOPMENT"))},
)
Expand Down
2 changes: 1 addition & 1 deletion masterbase/lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,7 @@ def _close_session_with_demo(
head = io.BytesIO(sink.read(LATE_BYTES_START))
sink.seek(LATE_BYTES_END, os.SEEK_SET)
minio_client.put_object(
"demos",
"demoblobs",
demo_blob_name(session_id),
data=cast(BinaryIO, ConcatStream(head, late, sink)),
length=size,
Expand Down
4 changes: 2 additions & 2 deletions masterbase/registers.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ def get_minio_connection(app: Litestar) -> Minio:
"""Initialize and mount S3-compatible client, if not already attached."""
if not getattr(app.state, "minio_client", None):
minio_client = make_minio_client()
if not minio_client.bucket_exists("demos"):
minio_client.make_bucket("demos", "us-east-1")
if not minio_client.bucket_exists("demoblobs"):
minio_client.make_bucket("demoblobs", "us-east-1")
app.state.minio_client = minio_client

return cast(Minio, app.state.minio_client)
Expand Down
187 changes: 96 additions & 91 deletions pdm.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ dependencies = [
"toml>=0.10.2",
"requests>=2.31.0",
"pydantic>=2.5.3",
"litestar[standard]>=2.8.3",
"litestar[standard]>=2.9.1",
"alembic>=1.13.1",
"psycopg2-binary>=2.9.9",
"asyncpg>=0.29.0",
Expand Down
5 changes: 2 additions & 3 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,8 @@ def test_demo_streaming(test_client: TestClient[Litestar], api_key: str) -> None
close_session_response = test_client.get("/close_session", params={"api_key": api_key})
assert close_session_response.status_code == HTTP_200_OK

response = test_client.get("/demodata", params={"api_key": api_key, "session_id": session_id})
redirect = str(response.url)
demo_out = requests.get(redirect).content
with test_client.stream("GET", "/demodata", params={"api_key": api_key, "session_id": session_id}) as demo_stream:
demo_out = demo_stream.read()

with open("tests/data/test_demo.dem", "rb") as f:
demo_in = f.read()
Expand Down
11 changes: 11 additions & 0 deletions vars.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export DEVELOPMENT=true
export DEBUG_WAIT_FOR_ATTACH=true
export STEAM_API_KEY=foo
export POSTGRES_USER=MEGASCATTERBOMB
export POSTGRES_PASSWORD=masterbase
export POSTGRES_HOST=localhost
export POSTGRES_PORT=8050
export MINIO_HOST=localhost
export MINIO_PORT=9000
export MINIO_ACCESS_KEY=MEGASCATTERBOMB
export MINIO_SECRET_KEY=masterbase

0 comments on commit 0dc7682

Please sign in to comment.