Files
LifeFaq/app/main.py

81 lines
2.9 KiB
Python
Raw Normal View History

2024-12-11 23:56:15 +01:00
from fastapi import FastAPI
from contextlib import asynccontextmanager
from fastapi.staticfiles import StaticFiles
2024-12-20 22:35:39 +01:00
2025-10-05 15:54:42 +02:00
import app
2024-12-20 22:35:39 +01:00
from app.controllers.route_to_web import RouteToWeb
2024-12-11 23:56:15 +01:00
from app.services.markdown_processor import MarkdownProcessor
from app.services.metadata_processor import MetadataProcessor
from app.controllers.category_controller import CategoryController
2024-12-22 00:09:07 +01:00
from fastapi.middleware.gzip import GZipMiddleware
from app.services.image_service import ImageService
2024-12-11 23:56:15 +01:00
class Application:
def __init__(self):
"""Initialize the FastAPI app and configure it."""
2024-12-30 21:37:36 +01:00
self.app = FastAPI( lifespan = self._lifespan_event )
self._set_image_sizes()
2024-12-11 23:56:15 +01:00
self._setup_static_files()
2025-10-05 16:01:55 +02:00
self._setup_health_route()
2024-12-11 23:56:15 +01:00
self._include_routers()
2024-12-22 00:09:07 +01:00
self._include_middelware()
2024-12-11 23:56:15 +01:00
2024-12-30 21:37:36 +01:00
2024-12-11 23:56:15 +01:00
@asynccontextmanager
async def _lifespan_event(self, app: FastAPI):
"""Lifespan event for startup and shutdown logic."""
print("App startup: Processing Markdown files...")
# Generate dynamic JSON data
metadata_processor = MetadataProcessor(input_dir="./data", output_file="generated_data.json",app=self.app)
2024-12-11 23:56:15 +01:00
metadata_processor.generate_json()
print("Generated dynamic data file.")
2024-12-12 23:30:19 +01:00
# Process Markdown files into HTML
processor = MarkdownProcessor(input_dir="./data", templates_dir="./templates",app=self.app)
2024-12-12 23:30:19 +01:00
processor.run()
2024-12-11 23:56:15 +01:00
yield
print("App shutdown: Cleanup complete.")
def _setup_static_files(self):
2024-12-22 00:09:07 +01:00
"""Mount static file directories."""
self.app.mount("/data", StaticFiles(directory="data"), name="data")
self.app.mount("/static", StaticFiles(directory="static"), name="static")
self.app.mount( "/images", StaticFiles( directory = "static/images" ), name = "images" )
2024-12-11 23:56:15 +01:00
def _include_routers(self):
"""Include all route controllers."""
category_controller = CategoryController()
image_service = ImageService(self.app)
2024-12-20 22:35:39 +01:00
route_to_web = RouteToWeb(self.app)
2024-12-16 23:15:37 +01:00
2024-12-22 00:09:07 +01:00
self.app.include_router( category_controller.router )
2024-12-20 22:35:39 +01:00
self.app.include_router(route_to_web.router)
self.app.include_router( image_service.router )
2024-12-22 00:09:07 +01:00
2025-10-05 16:01:55 +02:00
def _setup_health_route(self):
@self.app.get("/health", tags=["Health"])
async def health_check():
return {"status": "ok"}
2024-12-22 00:09:07 +01:00
def _include_middelware(self):
self.app.add_middleware( GZipMiddleware, minimum_size = 500 )
2024-12-11 23:56:15 +01:00
def _set_image_sizes(self):
self.app.state.IMAGE_SIZES = {
2024-12-29 18:47:43 +01:00
'thumbnails': {'width': 150, 'height': 150},
'large': {'width': 800, 'height': 600},
2024-12-30 23:20:54 +01:00
'small': {'width': 300, 'height': 300},
'original': {'width': None, 'height': None}, # Original størrelse
}
2024-12-11 23:56:15 +01:00
def get_app(self):
"""Return the FastAPI app instance."""
return self.app
application = Application()
app = application.get_app()