generated from hjess/PythonTemplateProject
Lets make the frontpage in markdown too
Some checks failed
Build, Push, and Deploy to Nomad / docker-nomad (push) Has been cancelled
Some checks failed
Build, Push, and Deploy to Nomad / docker-nomad (push) Has been cancelled
This commit is contained in:
27
app/main.py
27
app/main.py
@@ -7,29 +7,30 @@ from app.services.markdown_processor import MarkdownProcessor
|
||||
from app.services.metadata_processor import MetadataProcessor
|
||||
from app.controllers.category_controller import CategoryController
|
||||
from fastapi.middleware.gzip import GZipMiddleware
|
||||
from app.services.image_controller import ImageHandler
|
||||
from app.services.image_service import ImageService
|
||||
|
||||
|
||||
class Application:
|
||||
def __init__(self):
|
||||
"""Initialize the FastAPI app and configure it."""
|
||||
self.app = FastAPI(lifespan=self._lifespan_event)
|
||||
self._set_image_sizes()
|
||||
self._setup_static_files()
|
||||
self._include_routers()
|
||||
self._include_middelware()
|
||||
|
||||
|
||||
|
||||
@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")
|
||||
metadata_processor = MetadataProcessor(input_dir="./data", output_file="generated_data.json",app=self.app)
|
||||
metadata_processor.generate_json()
|
||||
print("Generated dynamic data file.")
|
||||
print("Markdown processing complete!")
|
||||
|
||||
# Process Markdown files into HTML
|
||||
processor = MarkdownProcessor(input_dir="./data", templates_dir="./templates")
|
||||
processor = MarkdownProcessor(input_dir="./data", templates_dir="./templates",app=self.app)
|
||||
processor.run()
|
||||
yield
|
||||
print("App shutdown: Cleanup complete.")
|
||||
@@ -38,24 +39,30 @@ class Application:
|
||||
"""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" )
|
||||
|
||||
def _include_routers(self):
|
||||
"""Include all route controllers."""
|
||||
category_controller = CategoryController()
|
||||
#dynamic_controller = DynamicController( "./data" )
|
||||
image_service = ImageService(self.app)
|
||||
route_to_web = RouteToWeb(self.app)
|
||||
|
||||
|
||||
|
||||
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( image_service.router )
|
||||
|
||||
|
||||
def _include_middelware(self):
|
||||
self.app.add_middleware( GZipMiddleware, minimum_size = 500 )
|
||||
|
||||
def _set_image_sizes(self):
|
||||
self.app.state.IMAGE_SIZES = {
|
||||
'thumbnails': {'width': 100, 'height': 100},
|
||||
'large': {'width': 800, 'height': 600},
|
||||
'small': {'width': 300, 'height': 200},
|
||||
'original': {'width': None, 'height': None}, # Original størrelse
|
||||
}
|
||||
|
||||
def get_app(self):
|
||||
"""Return the FastAPI app instance."""
|
||||
return self.app
|
||||
|
||||
Reference in New Issue
Block a user