Lets see what lighthouse says
All checks were successful
Build, Push, and Deploy to Nomad / docker-nomad (push) Successful in 37s

This commit is contained in:
2024-12-22 00:09:07 +01:00
parent 42f765366d
commit 87efffe1c1
5 changed files with 24 additions and 39 deletions

View File

@@ -1,31 +1,13 @@
from fastapi import FastAPI from fastapi import FastAPI
from contextlib import asynccontextmanager from contextlib import asynccontextmanager
from fastapi.staticfiles import StaticFiles from fastapi.staticfiles import StaticFiles
from fastapi.middleware.gzip import GZipMiddleware
from app.controllers.route_to_web import RouteToWeb from app.controllers.route_to_web import RouteToWeb
from app.services.markdown_processor import MarkdownProcessor from app.services.markdown_processor import MarkdownProcessor
from app.services.metadata_processor import MetadataProcessor from app.services.metadata_processor import MetadataProcessor
from app.controllers.category_controller import CategoryController from app.controllers.category_controller import CategoryController
from fastapi.middleware.gzip import GZipMiddleware
from app.services.image_controller import ImageHandler
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
class Application: class Application:
@@ -34,7 +16,7 @@ class Application:
self.app = FastAPI(lifespan=self._lifespan_event) self.app = FastAPI(lifespan=self._lifespan_event)
self._setup_static_files() self._setup_static_files()
self._include_routers() self._include_routers()
self._include_middleware() self._include_middelware()
@asynccontextmanager @asynccontextmanager
async def _lifespan_event(self, app: FastAPI): async def _lifespan_event(self, app: FastAPI):
@@ -53,23 +35,26 @@ class Application:
print("App shutdown: Cleanup complete.") print("App shutdown: Cleanup complete.")
def _setup_static_files(self): def _setup_static_files(self):
"""Mount static file directories with caching.""" """Mount static file directories."""
self.app.mount("/data", CustomStaticFiles(directory="data"), name="data") self.app.mount("/data", StaticFiles(directory="data"), name="data")
self.app.mount("/static", CustomStaticFiles(directory="static"), name="static") self.app.mount("/static", StaticFiles(directory="static"), name="static")
self.app.mount("/img", CustomStaticFiles(directory="static/images"), name="img")
self.app.mount( "/js", CustomStaticFiles( directory = "static/js" ), name = "js" )
def _include_routers(self): def _include_routers(self):
"""Include all route controllers.""" """Include all route controllers."""
category_controller = CategoryController() category_controller = CategoryController()
#dynamic_controller = DynamicController( "./data" )
route_to_web = RouteToWeb(self.app) 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) 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): def get_app(self):
"""Return the FastAPI app instance.""" """Return the FastAPI app instance."""

View File

@@ -5,7 +5,7 @@ author: Henrik Jess
date: ons 11 dec 23:25:00 CET 2024 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. summary: Fødevarer er markant billigere i Portugal med få undtagelser som bær og specialvarer.
favorite: true favorite: true
image: img/budget2.jpg image: images/budget2.jpg
category: Økonomi category: Økonomi
tags: [Portugal, Budget, Økonomi] tags: [Portugal, Budget, Økonomi]
--- ---

View File

@@ -89,7 +89,7 @@
"favorites": [ "favorites": [
{ {
"name": "Budget - Indkøb", "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.", "description": "Fødevarer er markant billigere i Portugal med få undtagelser som bær og specialvarer.",
"path": "Budget" "path": "Budget"
} }

View File

@@ -110,11 +110,11 @@
</div> <!-- Closing wrapper div --> </div> <!-- Closing wrapper div -->
<!-- Scripts med defer for at undgå blokerende indlæsning --> <!-- Scripts med defer for at undgå blokerende indlæsning -->
<script src="/js/jquery.min.js" defer></script> <script src="/static/js/jquery.min.js" defer></script>
<script src="/js/browser.min.js" defer></script> <script src="/static/js/browser.min.js" defer></script>
<script src="/js/breakpoints.min.js" defer></script> <script src="/static/js/breakpoints.min.js" defer></script>
<script src="/js/util.js" defer></script> <script src="/static/js/util.js" defer></script>
<script src="/js/main.js" defer></script> <script src="/static/js/main.js" defer></script>
<script src="/js/modal_handler.js?v={{ timestamp }}" defer></script> <script src="/static/js/modal_handler.js?v={{ timestamp }}" defer></script>
</body> </body>
</html> </html>

View File

@@ -26,7 +26,7 @@
{% for favorite in data.favorites %} {% for favorite in data.favorites %}
<article> <article>
<a href="/category/{{ favorite.path }}" class="image"> <a href="/category/{{ favorite.path }}" class="image">
<img src="{{favorite.image}}" alt="{{ favorite.name }}" height=300 width=40/> <img src="/static/{{favorite.image}}" alt="{{ favorite.name }}" height=300 width=40/>
</a> </a>
<p><b>{{ favorite.path }}</b>: {{ favorite.description }}</p> <p><b>{{ favorite.path }}</b>: {{ favorite.description }}</p>
</article> </article>