Skip to content

Commit

Permalink
Iainland/healthz html response (#158)
Browse files Browse the repository at this point in the history
init `/html` route.
  • Loading branch information
imaitland authored Sep 10, 2024
1 parent 3e46745 commit 5d06993
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
28 changes: 28 additions & 0 deletions evolver/app/html_routes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import socket

from fastapi import FastAPI
from fastapi.responses import HTMLResponse

from evolver import __project__, __version__

html_app = FastAPI()


@html_app.get("/network", operation_id="network_state_html", response_class=HTMLResponse)
async def network_html():
hostname = socket.gethostname()
ip_address = socket.gethostbyname(hostname)
html_content = f"""
<html>
<head>
<title>Evolver</title>
</head>
<body>
<h1>device network details</h1>
<p>Running '{__project__}' ver: '{__version__}'</p>
<p>Hostname: {hostname}</p>
<p>IP Address: {ip_address}</p>
</body>
</html>
"""
return html_content
4 changes: 4 additions & 0 deletions evolver/app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import evolver.util
from evolver import __project__, __version__
from evolver.app.exceptions import CalibratorNotFoundError, HardwareNotFoundError
from evolver.app.html_routes import html_app
from evolver.app.models import SchemaResponse
from evolver.base import require_all_fields
from evolver.device import Evolver
Expand Down Expand Up @@ -118,6 +119,9 @@ async def calibrate(name: str, data: dict = None):
return calibrator.run_calibration_procedure(data) # TODO: or **data?


app.mount("/html", html_app)


def start():
import uvicorn

Expand Down
17 changes: 17 additions & 0 deletions evolver/app/tests/test_html.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from evolver import __version__


class TestHtmlApp:
def test_html_network(self, app_client):
response = app_client.get("/html/network")
assert response.status_code == 200
assert response.headers["content-type"] == "text/html; charset=utf-8"

html_content = response.text
assert "<html>" in html_content
assert "<title>Evolver</title>" in html_content
if __version__:
assert __version__ in html_content
assert "device network details" in html_content
assert "Hostname:" in html_content
assert "IP Address:" in html_content

0 comments on commit 5d06993

Please sign in to comment.