Primo commit
This commit is contained in:
66
main.py
66
main.py
@@ -1,3 +1,6 @@
|
||||
from dotenv import load_dotenv
|
||||
|
||||
load_dotenv() # Carica le variabili dal file .env
|
||||
"""
|
||||
Parking Manager Application
|
||||
FastAPI + SQLite + Vanilla JS
|
||||
@@ -10,11 +13,12 @@ from contextlib import asynccontextmanager
|
||||
from slowapi import Limiter, _rate_limit_exceeded_handler
|
||||
from slowapi.util import get_remote_address
|
||||
from slowapi.errors import RateLimitExceeded
|
||||
from datetime import datetime
|
||||
|
||||
from app import config
|
||||
from app.routes.auth import router as auth_router
|
||||
from app.routes.users import router as users_router
|
||||
from app.routes.managers import router as managers_router
|
||||
from app.routes.offices import router as offices_router
|
||||
from app.routes.presence import router as presence_router
|
||||
from app.routes.parking import router as parking_router
|
||||
from database.connection import init_db
|
||||
@@ -26,11 +30,50 @@ limiter = Limiter(key_func=get_remote_address)
|
||||
@asynccontextmanager
|
||||
async def lifespan(app: FastAPI):
|
||||
"""Initialize database on startup"""
|
||||
config.logger.info("Starting Parking Manager application")
|
||||
def log(msg):
|
||||
config.logger.info(f"[{datetime.now().strftime('%Y-%m-%d %H:%M:%S')}] {msg}")
|
||||
|
||||
log("Starting Parking Manager application")
|
||||
|
||||
# Caddy Integration Logs
|
||||
log("--- Caddy Integration & Handshake ---")
|
||||
|
||||
# Step 1: Auth / Forward Auth
|
||||
log("1. Checking Caddy Forward Auth Configuration...")
|
||||
status = "ENABLED (Authelia)" if config.AUTHELIA_ENABLED else "DISABLED (Internal Auth)"
|
||||
log(f" - Auth Mode: {status}")
|
||||
|
||||
if config.AUTHELIA_ENABLED:
|
||||
log(" - Configuring Trusted Headers from Caddy:")
|
||||
log(f" * User: {config.AUTHELIA_HEADER_USER}")
|
||||
log(f" * Name: {config.AUTHELIA_HEADER_NAME}")
|
||||
log(f" * Email: {config.AUTHELIA_HEADER_EMAIL}")
|
||||
log(f" * Groups: {config.AUTHELIA_HEADER_GROUPS}")
|
||||
else:
|
||||
log(" - No trusted headers configured (Standalone mode)")
|
||||
|
||||
# Step 2: CORS / Origins
|
||||
log("2. Configuring Caddy CORS / Origins...")
|
||||
for origin in config.ALLOWED_ORIGINS:
|
||||
log(f" - Trusted Origin: {origin}")
|
||||
|
||||
init_db()
|
||||
config.logger.info("Database initialized")
|
||||
log("3. Database Connection: ESTABLISHED")
|
||||
|
||||
# Step 3: Network / Reachability
|
||||
local_url = f"http://{config.HOST}:{config.PORT}"
|
||||
# Try to find a public URL from allowed origins (excluding localhost/ips)
|
||||
public_candidates = [o for o in config.ALLOWED_ORIGINS if "localhost" not in o and "127.0.0.1" not in o and not o.startswith("*")]
|
||||
reachable_url = public_candidates[0] if public_candidates else local_url.replace("0.0.0.0", "localhost")
|
||||
|
||||
log("4. Finalizing Caddy Handshake...")
|
||||
log(f" - Listening on: {config.HOST}:{config.PORT}")
|
||||
log("--- Handshake Complete ---")
|
||||
|
||||
log(f"feedback: App reachable via Caddy at {reachable_url}")
|
||||
|
||||
yield
|
||||
config.logger.info("Shutting down Parking Manager application")
|
||||
log("Shutting down Parking Manager application")
|
||||
|
||||
|
||||
app = FastAPI(title="Parking Manager", version="1.0.0", lifespan=lifespan)
|
||||
@@ -51,13 +94,14 @@ app.add_middleware(
|
||||
# API Routes
|
||||
app.include_router(auth_router)
|
||||
app.include_router(users_router)
|
||||
app.include_router(managers_router)
|
||||
app.include_router(offices_router)
|
||||
app.include_router(presence_router)
|
||||
app.include_router(parking_router)
|
||||
|
||||
# Static Files
|
||||
app.mount("/css", StaticFiles(directory=str(config.FRONTEND_DIR / "css")), name="css")
|
||||
app.mount("/js", StaticFiles(directory=str(config.FRONTEND_DIR / "js")), name="js")
|
||||
app.mount("/assets", StaticFiles(directory=str(config.FRONTEND_DIR / "assets")), name="assets")
|
||||
|
||||
|
||||
# Page Routes
|
||||
@@ -109,6 +153,12 @@ async def admin_users_page():
|
||||
return FileResponse(config.FRONTEND_DIR / "pages" / "admin-users.html")
|
||||
|
||||
|
||||
@app.get("/admin/offices")
|
||||
async def admin_offices_page():
|
||||
"""Admin Offices page"""
|
||||
return FileResponse(config.FRONTEND_DIR / "pages" / "admin-offices.html")
|
||||
|
||||
|
||||
@app.get("/profile")
|
||||
async def profile_page():
|
||||
"""Profile page"""
|
||||
@@ -121,6 +171,12 @@ async def settings_page():
|
||||
return FileResponse(config.FRONTEND_DIR / "pages" / "settings.html")
|
||||
|
||||
|
||||
@app.get("/parking-settings")
|
||||
async def parking_settings_page():
|
||||
"""Parking Settings page"""
|
||||
return FileResponse(config.FRONTEND_DIR / "pages" / "parking-settings.html")
|
||||
|
||||
|
||||
@app.get("/favicon.svg")
|
||||
async def favicon():
|
||||
"""Favicon"""
|
||||
|
||||
Reference in New Issue
Block a user