diff --git a/README.md b/README.md index c69d3a4..f2dcc98 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,8 @@ Initially designed to run directly on a raspberry pi, it has been containerized podman or docker! Additionally, this can be run without the frontend in case you just wanted a slightly more sane way to query MTA's data -sources compared to their stock APIs. +sources compared to their stock APIs. Swagger is hosted at /swagger +![swagger.png](attachments/swagger.png) ## Features @@ -46,9 +47,10 @@ The application will be available at `http://localhost:8000` ### Environment Variables -| Variable | Description | Default | Required | -|-------------------|---------------------------------|---------|----------| -| `FRONTEND_ENABLE` | Enable/disable the web frontend | `true` | No | +| Variable | Description | Default | Required | +|-------------------|------------------------------------------------------------|---------|----------| +| `FRONTEND_ENABLE` | Enable/disable the web frontend | `true` | No | +| `SHOW_SWAGGER` | Enable/disable Swagger API documentation at `/swagger` | `false` | No | ### Running with Docker Compose diff --git a/attachments/swagger.png b/attachments/swagger.png new file mode 100644 index 0000000..19be9f4 Binary files /dev/null and b/attachments/swagger.png differ diff --git a/main.py b/main.py index 514f05c..967d351 100644 --- a/main.py +++ b/main.py @@ -15,7 +15,15 @@ from mta_sign_server.config.router import router as config_router load_dotenv() -app = FastAPI() +# Setup Swagger documentation +show_swagger = os.getenv("SHOW_SWAGGER", "false").lower() in ("true", "1", "yes") +swagger_config = { + "docs_url": "/swagger" if show_swagger else None, + "redoc_url": None, + "openapi_url": "/openapi.json" if show_swagger else None, +} + +app = FastAPI(**swagger_config) app.add_middleware( CORSMiddleware, allow_origins=['*'] @@ -49,6 +57,10 @@ if frontend_enabled and static_dir.exists(): @app.get("/{path:path}") async def serve_spa(path: str): """Serve static files or fall back to index.html for SPA routing""" + # Exclude API and documentation routes from SPA fallback + if path.startswith(("api/", "swagger", "openapi", "redoc", "docs")): + return {"error": "Not found"} + file_path = static_dir / path if file_path.exists() and file_path.is_file(): return FileResponse(file_path)