|
58 | 58 | ) |
59 | 59 | from .log import get_logger |
60 | 60 | from .registry import FunctionRegistry, ServiceDef, default_registry |
| 61 | +from .static_content import StaticContent, resolve_folder |
61 | 62 | from .trace import MY_CID_TAG, MY_CORRELATION_ID |
62 | 63 |
|
63 | 64 | OCTET_STREAM = "application/octet-stream" |
@@ -223,6 +224,9 @@ class EventApiServer: |
223 | 224 | def __init__(self, registry: FunctionRegistry | None = None): |
224 | 225 | self.registry = registry or default_registry |
225 | 226 | self.actuator = Actuator(self.registry) |
| 227 | + # the static content folder is resolved from the configuration at construction, as the |
| 228 | + # Actuator's settings are |
| 229 | + self.static = StaticContent(resolve_folder(app_config())) |
226 | 230 |
|
227 | 231 | async def handle_event(self, request: web.Request) -> web.StreamResponse: |
228 | 232 | raw = await request.read() |
@@ -420,24 +424,33 @@ async def _next_segment(response: web.StreamResponse, |
420 | 424 |
|
421 | 425 | def create_app(self, routes: Iterable[RouteMounter] = ()) -> web.Application: |
422 | 426 | """The Event API application: ``POST /api/event``, the actuator paths, then the routes |
423 | | - of the extensions given (``Platform.add_routes``) and, last, the catch-all that answers |
424 | | - the engines' error shape - so the host's paths always win and an extension's path wins |
425 | | - over the catch-all.""" |
| 427 | + of the extensions given (``Platform.add_routes``), then the static content folder for a |
| 428 | + GET or HEAD and, last, the catch-all that answers the engines' error shape - so the host's |
| 429 | + paths always win, an extension's path wins over a file and a file wins over the catch-all. |
| 430 | + A folder's ``index.html`` replaces the engines' landing page at ``/``.""" |
426 | 431 | app = web.Application(client_max_size=16 * 1024 * 1024) |
427 | 432 | app.router.add_post("/api/event", self.handle_event) |
428 | | - # the engines' landing page + actuator endpoints (see actuator.py) |
429 | | - for path in ("/", "/info", "/info/routes", "/env", "/health", "/livenessprobe"): |
| 433 | + # the engines' landing page (or the static folder's index.html) + actuator endpoints |
| 434 | + # (see actuator.py) |
| 435 | + app.router.add_get("/", self.handle_other) |
| 436 | + for path in ("/info", "/info/routes", "/env", "/health", "/livenessprobe"): |
430 | 437 | app.router.add_get(path, self.actuator.handle) |
431 | 438 | # an extension's own routes - a reply page, a stream a browser holds - registered |
432 | 439 | # after the host's and before the catch-all |
433 | 440 | for mount in routes: |
434 | 441 | mount(app) |
435 | | - # any other path or method answers the engines' error shape, not |
436 | | - # aiohttp's default text page (exact routes above win - they are |
| 442 | + # any other path: a static file for a GET or HEAD that names one, else the engines' |
| 443 | + # error shape, not aiohttp's default text page (exact routes above win - they are |
437 | 444 | # registered first) |
438 | | - app.router.add_route("*", "/{unknown:.*}", self.actuator.handle) |
| 445 | + app.router.add_route("*", "/{unknown:.*}", self.handle_other) |
439 | 446 | return app |
440 | 447 |
|
| 448 | + async def handle_other(self, request: web.Request) -> web.StreamResponse: |
| 449 | + """A path no route claimed, and ``/``: the static folder's file (``index.html`` for a |
| 450 | + folder) when a GET or HEAD names one, else the actuator - the landing page at ``/``, the |
| 451 | + engines' error shape elsewhere.""" |
| 452 | + return self.static.serve(request) or await self.actuator.handle(request) |
| 453 | + |
441 | 454 |
|
442 | 455 | class Platform: |
443 | 456 | """Runs the Event API host for the default (or a given) registry.""" |
|
0 commit comments