From 87efffe1c153d1e8f310588e3ada775025886d8e Mon Sep 17 00:00:00 2001 From: Henrik Jess Nielsen Date: Sun, 22 Dec 2024 00:09:07 +0100 Subject: [PATCH] Lets see what lighthouse says --- app/main.py | 45 ++++++++---------------- data/Budget/0100_budget_indkøb_priser.md | 2 +- generated_data.json | 2 +- templates/base_template.html | 12 +++---- templates/navigation.html | 2 +- 5 files changed, 24 insertions(+), 39 deletions(-) diff --git a/app/main.py b/app/main.py index 2277d80..7061c0b 100644 --- a/app/main.py +++ b/app/main.py @@ -1,31 +1,13 @@ from fastapi import FastAPI from contextlib import asynccontextmanager from fastapi.staticfiles import StaticFiles -from fastapi.middleware.gzip import GZipMiddleware from app.controllers.route_to_web import RouteToWeb from app.services.markdown_processor import MarkdownProcessor from app.services.metadata_processor import MetadataProcessor from app.controllers.category_controller import CategoryController - - -class CustomStaticFiles(StaticFiles): - """Custom StaticFiles class to add cache headers.""" - def __init__(self, directory: str, cache_timeout: int = 31536000): - """ - Initialize the custom static files handler. - - :param directory: Directory containing static files. - :param cache_timeout: Cache timeout in seconds (default: 1 year). - """ - super().__init__(directory=directory) - self.cache_timeout = cache_timeout - - async def get_response(self, path: str, scope): - """Override to add custom cache headers.""" - response = await super().get_response(path, scope) - response.headers["Cache-Control"] = f"public, max-age={self.cache_timeout}, immutable" - return response +from fastapi.middleware.gzip import GZipMiddleware +from app.services.image_controller import ImageHandler class Application: @@ -34,7 +16,7 @@ class Application: self.app = FastAPI(lifespan=self._lifespan_event) self._setup_static_files() self._include_routers() - self._include_middleware() + self._include_middelware() @asynccontextmanager async def _lifespan_event(self, app: FastAPI): @@ -53,23 +35,26 @@ class Application: print("App shutdown: Cleanup complete.") def _setup_static_files(self): - """Mount static file directories with caching.""" - self.app.mount("/data", CustomStaticFiles(directory="data"), name="data") - self.app.mount("/static", CustomStaticFiles(directory="static"), name="static") - self.app.mount("/img", CustomStaticFiles(directory="static/images"), name="img") - self.app.mount( "/js", CustomStaticFiles( directory = "static/js" ), name = "js" ) + """Mount static file directories.""" + self.app.mount("/data", StaticFiles(directory="data"), name="data") + self.app.mount("/static", StaticFiles(directory="static"), name="static") def _include_routers(self): """Include all route controllers.""" category_controller = CategoryController() + #dynamic_controller = DynamicController( "./data" ) route_to_web = RouteToWeb(self.app) - self.app.include_router(category_controller.router) + + + self.app.include_router( category_controller.router ) + #self.app.include_router( dynamic_controller.router ) self.app.include_router(route_to_web.router) - def _include_middleware(self): - """Add middleware for compression and other features.""" - self.app.add_middleware(GZipMiddleware, minimum_size=500) + + + def _include_middelware(self): + self.app.add_middleware( GZipMiddleware, minimum_size = 500 ) def get_app(self): """Return the FastAPI app instance.""" diff --git a/data/Budget/0100_budget_indkøb_priser.md b/data/Budget/0100_budget_indkøb_priser.md index 035aa1b..c8f50b7 100644 --- a/data/Budget/0100_budget_indkøb_priser.md +++ b/data/Budget/0100_budget_indkøb_priser.md @@ -5,7 +5,7 @@ author: Henrik Jess date: ons 11 dec 23:25:00 CET 2024 summary: Fødevarer er markant billigere i Portugal med få undtagelser som bær og specialvarer. favorite: true -image: img/budget2.jpg +image: images/budget2.jpg category: Økonomi tags: [Portugal, Budget, Økonomi] --- diff --git a/generated_data.json b/generated_data.json index b66a838..babac64 100644 --- a/generated_data.json +++ b/generated_data.json @@ -89,7 +89,7 @@ "favorites": [ { "name": "Budget - Indkøb", - "image": "img/budget2.jpg", + "image": "images/budget2.jpg", "description": "Fødevarer er markant billigere i Portugal med få undtagelser som bær og specialvarer.", "path": "Budget" } diff --git a/templates/base_template.html b/templates/base_template.html index 14c1232..a8a35f8 100644 --- a/templates/base_template.html +++ b/templates/base_template.html @@ -110,11 +110,11 @@ - - - - - - + + + + + + diff --git a/templates/navigation.html b/templates/navigation.html index 6b8d6fc..f27dad0 100644 --- a/templates/navigation.html +++ b/templates/navigation.html @@ -26,7 +26,7 @@ {% for favorite in data.favorites %}
- {{ favorite.name }} + {{ favorite.name }}

{{ favorite.path }}: {{ favorite.description }}