From 9e46de900729e36b0cfbfc16b99c1764daa7921d Mon Sep 17 00:00:00 2001 From: Donkie Date: Mon, 4 Sep 2023 20:22:31 +0200 Subject: [PATCH] Moved SPA definition to a new file --- spoolman/client.py | 25 +++++++++++++++++++++++++ spoolman/main.py | 21 +-------------------- 2 files changed, 26 insertions(+), 20 deletions(-) create mode 100644 spoolman/client.py diff --git a/spoolman/client.py b/spoolman/client.py new file mode 100644 index 0000000..3964813 --- /dev/null +++ b/spoolman/client.py @@ -0,0 +1,25 @@ +"""Functions for providing the client interface.""" + +# ruff: noqa: PTH118 + +import os +from typing import Union + +from fastapi.staticfiles import StaticFiles + + +class SinglePageApplication(StaticFiles): + """Serve a single page application.""" + + def __init__(self, directory: str) -> None: + """Construct.""" + super().__init__(directory=directory, packages=None, html=True, check_dir=True) + + def lookup_path(self, path: str) -> tuple[str, Union[os.stat_result, None]]: + """Return index.html if the requested file cannot be found.""" + full_path, stat_result = super().lookup_path(path) + + if stat_result is None: + return super().lookup_path("index.html") + + return (full_path, stat_result) diff --git a/spoolman/main.py b/spoolman/main.py index 83c51c8..1e16948 100644 --- a/spoolman/main.py +++ b/spoolman/main.py @@ -1,21 +1,19 @@ """Main entrypoint to the server.""" import logging -import os import subprocess from logging.handlers import TimedRotatingFileHandler from pathlib import Path -from typing import Union import uvicorn from fastapi import FastAPI from fastapi.middleware.cors import CORSMiddleware from fastapi.middleware.gzip import GZipMiddleware -from fastapi.staticfiles import StaticFiles from scheduler.asyncio.scheduler import Scheduler from spoolman import env from spoolman.api.v1.router import app as v1_app +from spoolman.client import SinglePageApplication from spoolman.database import database # Define a file logger with log rotation @@ -38,23 +36,6 @@ logger = logging.getLogger(__name__) # Setup FastAPI -class SinglePageApplication(StaticFiles): - """Serve a single page application.""" - - def __init__(self, directory: str) -> None: - """Construct.""" - super().__init__(directory=directory, packages=None, html=True, check_dir=True) - - def lookup_path(self, path: str) -> tuple[str, Union[os.stat_result, None]]: - """Return index.html if the requested file cannot be found.""" - full_path, stat_result = super().lookup_path(path) - - if stat_result is None: - return super().lookup_path("index.html") - - return (full_path, stat_result) - - app = FastAPI( debug=env.is_debug_mode(), title="Spoolman",