diff --git a/app/__init__.py b/app/__init__.py new file mode 100644 index 0000000..2f62bb5 --- /dev/null +++ b/app/__init__.py @@ -0,0 +1 @@ +"""Application package for the dashboard service.""" diff --git a/app/main.py b/app/main.py index 04f5c9f..8dedc16 100644 --- a/app/main.py +++ b/app/main.py @@ -1,6 +1,7 @@ from __future__ import annotations import dataclasses +from contextlib import asynccontextmanager from fastapi import FastAPI, HTTPException, Request from fastapi.responses import HTMLResponse @@ -13,12 +14,23 @@ from app.services import PortScanner from app.storage import Storage from app.tunnel_manager import TunnelManager -app = FastAPI(title="Gradio Tunnel Dashboard") policy = TunnelPolicy.load() storage = Storage.from_env() scanner = PortScanner(policy=policy, storage=storage) manager = TunnelManager(storage=storage) + +@asynccontextmanager +async def lifespan(_: FastAPI): + try: + yield + finally: + manager.shutdown() + storage.close() + + +app = FastAPI(title="Gradio Tunnel Dashboard", lifespan=lifespan) + app.mount("/static", StaticFiles(directory="app/static"), name="static") templates = Jinja2Templates(directory="app/templates") @@ -99,8 +111,3 @@ async def stop_tunnel(port: int) -> dict[str, bool]: raise HTTPException(status_code=404, detail="Tunnel not running.") return {"stopped": True} - -@app.on_event("shutdown") -def shutdown_event() -> None: - manager.shutdown() - storage.close() diff --git a/pytest.ini b/pytest.ini new file mode 100644 index 0000000..c7b23ec --- /dev/null +++ b/pytest.ini @@ -0,0 +1,3 @@ +[pytest] +pythonpath = . +testpaths = tests diff --git a/scripts/auto-test.sh b/scripts/auto-test.sh index 5bb49ac..fb93df7 100755 --- a/scripts/auto-test.sh +++ b/scripts/auto-test.sh @@ -3,8 +3,18 @@ set -euo pipefail cd "$(dirname "$0")/.." +if ! command -v python3 >/dev/null 2>&1; then + echo "python3 non trovato nel PATH." >&2 + exit 1 +fi + +if [[ ! -x ".venv/bin/python3" ]]; then + echo "Creating virtual environment..." + python3 -m venv .venv +fi + echo "Installing dependencies..." -python -m pip install -r requirements.txt >/dev/null +.venv/bin/python3 -m pip install -r requirements.txt >/dev/null echo "Running pytest..." -python -m pytest tests +PYTHONPATH=. .venv/bin/python3 -m pytest tests