2024-12-10 22:41:10 +01:00
|
|
|
import uvicorn
|
2024-12-10 16:40:17 +01:00
|
|
|
from fastapi import FastAPI, Request
|
|
|
|
|
from fastapi.staticfiles import StaticFiles
|
|
|
|
|
from fastapi.responses import HTMLResponse
|
|
|
|
|
from fastapi.templating import Jinja2Templates
|
|
|
|
|
import json
|
2024-12-10 15:16:17 +01:00
|
|
|
import os
|
|
|
|
|
|
2024-12-10 17:25:29 +01:00
|
|
|
from starlette.middleware.httpsredirect import HTTPSRedirectMiddleware
|
2024-12-10 21:42:33 +01:00
|
|
|
from fastapi import FastAPI
|
|
|
|
|
from starlette.middleware.base import BaseHTTPMiddleware
|
|
|
|
|
from starlette.requests import Request
|
2024-12-10 21:48:34 +01:00
|
|
|
from fastapi.middleware.trustedhost import TrustedHostMiddleware
|
2024-12-10 21:42:33 +01:00
|
|
|
|
2024-12-10 17:25:29 +01:00
|
|
|
|
2024-12-10 16:40:17 +01:00
|
|
|
app = FastAPI()
|
2024-12-10 15:16:17 +01:00
|
|
|
|
2024-12-10 16:40:17 +01:00
|
|
|
# Mount static files
|
|
|
|
|
app.mount("/static", StaticFiles(directory="static"), name="static")
|
2024-12-10 21:57:31 +01:00
|
|
|
|
2024-12-10 15:16:17 +01:00
|
|
|
|
2024-12-10 16:40:17 +01:00
|
|
|
# Templates directory
|
|
|
|
|
templates = Jinja2Templates(directory="templates")
|
2024-12-10 15:26:30 +01:00
|
|
|
|
2024-12-10 16:40:17 +01:00
|
|
|
# Load JSON data
|
|
|
|
|
with open("mock_data.json") as file:
|
|
|
|
|
data = json.load(file)
|
2024-12-10 15:26:30 +01:00
|
|
|
|
2024-12-10 16:40:17 +01:00
|
|
|
# Index route
|
|
|
|
|
@app.get("/", response_class=HTMLResponse)
|
|
|
|
|
async def get_index(request: Request):
|
2024-12-10 17:09:37 +01:00
|
|
|
return templates.TemplateResponse(
|
|
|
|
|
"index.html",
|
|
|
|
|
{"request": request, "data": data, "page_title": "Forside", "author": "Henrik"}
|
|
|
|
|
)
|
2024-12-10 16:40:17 +01:00
|
|
|
|
|
|
|
|
# Category route
|
|
|
|
|
@app.get("/category/{category_name}", response_class=HTMLResponse)
|
|
|
|
|
async def get_category(request: Request, category_name: str):
|
2024-12-10 17:09:37 +01:00
|
|
|
# Find den korrekte kategori
|
|
|
|
|
category = next((cat for cat in data["categories"] if cat["path"] == category_name), None)
|
|
|
|
|
if category:
|
|
|
|
|
category_file = f"data/{category_name}/index.html"
|
|
|
|
|
if os.path.exists(category_file):
|
|
|
|
|
with open(category_file) as file:
|
|
|
|
|
category_content = file.read()
|
|
|
|
|
return templates.TemplateResponse(
|
|
|
|
|
"category.html",
|
|
|
|
|
{
|
|
|
|
|
"request": request,
|
|
|
|
|
"data": data,
|
|
|
|
|
"page_title": category["name"],
|
|
|
|
|
"author": category["author"],
|
|
|
|
|
"content": category_content
|
|
|
|
|
},
|
|
|
|
|
)
|
2024-12-10 22:41:10 +01:00
|
|
|
return HTMLResponse("Kategori ikke fundet", status_code=404)
|
|
|
|
|
|
|
|
|
|
# Main entry point
|
|
|
|
|
def main():
|
|
|
|
|
"""Run the FastAPI app directly"""
|
|
|
|
|
uvicorn.run("app:app", host="0.0.0.0", port=8000, reload=True, proxy_headers=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Run main when executed directly
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
main()
|