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
21 changes: 21 additions & 0 deletions src/labthings_fastapi/server/fallback.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from typing import Any, TYPE_CHECKING

from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import HTMLResponse
from jinja2 import Environment, BaseLoader, select_autoescape
from starlette.responses import RedirectResponse
Expand Down Expand Up @@ -149,6 +150,15 @@ def fallback_page(self) -> HTMLResponse:

app = FallbackApp()

# Add middleware so contacting the the fallback server doesn't throw CORS errors.
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)


@app.get("/")
async def root() -> HTMLResponse:
Expand All @@ -159,6 +169,17 @@ async def root() -> HTMLResponse:
return app.fallback_page()


@app.get("/labthings_fallback")
async def fallback_route() -> bool:
"""Return True, this is a LabThings Fallback Server.

Use this to check over the API if this is a LabThings Fallback Server.

:return: returns True. This is a LabThings Fallback Server.
"""
return True


def _format_error_and_traceback(context: FallbackContext) -> tuple[str, str]:
"""Format the error and traceback.

Expand Down
8 changes: 8 additions & 0 deletions tests/test_fallback.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,14 @@ def test_fallback_with_log():
assert "Fake log content" in html


def test_fallback_identification():
"""Test the server identifies as a fallback server."""
app.set_context(FallbackContext())
with TestClient(app) as client:
response = client.get("/labthings_fallback")
assert response.json() is True


def test_actual_server_fallback():
"""Test that the the server configures its startup failure correctly.

Expand Down
Loading