Moved SPA definition to a new file

This commit is contained in:
Donkie
2023-09-04 20:22:31 +02:00
parent 89c588aaaf
commit 9e46de9007
2 changed files with 26 additions and 20 deletions

25
spoolman/client.py Normal file
View File

@@ -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)

View File

@@ -1,21 +1,19 @@
"""Main entrypoint to the server.""" """Main entrypoint to the server."""
import logging import logging
import os
import subprocess import subprocess
from logging.handlers import TimedRotatingFileHandler from logging.handlers import TimedRotatingFileHandler
from pathlib import Path from pathlib import Path
from typing import Union
import uvicorn import uvicorn
from fastapi import FastAPI from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware from fastapi.middleware.cors import CORSMiddleware
from fastapi.middleware.gzip import GZipMiddleware from fastapi.middleware.gzip import GZipMiddleware
from fastapi.staticfiles import StaticFiles
from scheduler.asyncio.scheduler import Scheduler from scheduler.asyncio.scheduler import Scheduler
from spoolman import env from spoolman import env
from spoolman.api.v1.router import app as v1_app from spoolman.api.v1.router import app as v1_app
from spoolman.client import SinglePageApplication
from spoolman.database import database from spoolman.database import database
# Define a file logger with log rotation # Define a file logger with log rotation
@@ -38,23 +36,6 @@ logger = logging.getLogger(__name__)
# Setup FastAPI # 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( app = FastAPI(
debug=env.is_debug_mode(), debug=env.is_debug_mode(),
title="Spoolman", title="Spoolman",