From 956805ca5036cf0567b8fc8e33722692d9cf6458 Mon Sep 17 00:00:00 2001 From: Wrench56 Date: Mon, 8 Jul 2024 05:36:03 -0400 Subject: [PATCH] Add `serve` API to display Svelte pages created by plugins --- src/backend/api/serve.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 src/backend/api/serve.py diff --git a/src/backend/api/serve.py b/src/backend/api/serve.py new file mode 100644 index 0000000..5110c0f --- /dev/null +++ b/src/backend/api/serve.py @@ -0,0 +1,27 @@ +from fastapi.responses import FileResponse +import logging +import os + +from utils import config, stack + +_BUILD_PATH = None + + +async def page(path: str) -> FileResponse: + _cache_buildpath() + mname = stack.get_caller(depth=2)[0] + if not mname.startswith('plugins.plugins.'): + logging.error(f'Non-plugin called serve.page() API: "{mname}"') + return FileResponse(f'{_BUILD_PATH}/plugins') + pname = mname.split('.')[2] + fpath = f'{_BUILD_PATH}/plugins/{pname}/{path}.html' + if not os.path.exists(fpath): + logging.error(f'Plugin page "{path}" for plugin "{pname}" does not exist') + return FileResponse(f'{_BUILD_PATH}/dne.html') + return FileResponse(fpath) + + +def _cache_buildpath() -> None: + global _BUILD_PATH + if _BUILD_PATH is None: + _BUILD_PATH = config.fetch().get("frontend").get("build_path")