|
13 | 13 | from routers import download, dynamic, index |
14 | 14 | from server_utils import limiter |
15 | 15 |
|
| 16 | +# Load environment variables from .env file |
16 | 17 | load_dotenv() |
17 | 18 |
|
| 19 | +# Initialize the FastAPI application |
18 | 20 | app = FastAPI() |
19 | 21 | app.state.limiter = limiter |
20 | 22 |
|
21 | 23 |
|
22 | | -# Define a wrapper handler with the correct signature |
23 | 24 | async def rate_limit_exception_handler(request: Request, exc: Exception) -> Response: |
| 25 | + """ |
| 26 | + Custom exception handler for rate-limiting errors. |
| 27 | +
|
| 28 | + Parameters |
| 29 | + ---------- |
| 30 | + request : Request |
| 31 | + The incoming HTTP request. |
| 32 | + exc : Exception |
| 33 | + The exception raised, expected to be RateLimitExceeded. |
| 34 | +
|
| 35 | + Returns |
| 36 | + ------- |
| 37 | + Response |
| 38 | + A response indicating that the rate limit has been exceeded. |
| 39 | + """ |
24 | 40 | if isinstance(exc, RateLimitExceeded): |
25 | | - # Delegate to the actual handler |
| 41 | + # Delegate to the default rate limit handler |
26 | 42 | return _rate_limit_exceeded_handler(request, exc) |
27 | | - # Optionally, handle other exceptions or re-raise |
| 43 | + # Re-raise other exceptions |
28 | 44 | raise exc |
29 | 45 |
|
30 | 46 |
|
31 | | -# Register the wrapper handler |
| 47 | +# Register the custom exception handler for rate limits |
32 | 48 | app.add_exception_handler(RateLimitExceeded, rate_limit_exception_handler) |
33 | 49 |
|
| 50 | +# Mount static files to serve CSS, JS, and other static assets |
34 | 51 | app.mount("/static", StaticFiles(directory="static"), name="static") |
35 | | -app_analytics_key = os.getenv("API_ANALYTICS_KEY") |
36 | | -if app_analytics_key: |
37 | | - app.add_middleware(Analytics, api_key=app_analytics_key) |
38 | 52 |
|
39 | | -# Define the default allowed hosts |
40 | | -default_allowed_hosts = ["gitingest.com", "*.gitingest.com", "localhost", "127.0.0.1"] |
| 53 | +# Set up API analytics middleware if an API key is provided |
| 54 | +if app_analytics_key := os.getenv("API_ANALYTICS_KEY"): |
| 55 | + app.add_middleware(Analytics, api_key=app_analytics_key) |
41 | 56 |
|
42 | | -# Fetch allowed hosts from the environment variable or use the default |
| 57 | +# Fetch allowed hosts from the environment or use the default values |
43 | 58 | allowed_hosts = os.getenv("ALLOWED_HOSTS") |
44 | 59 | if allowed_hosts: |
45 | 60 | allowed_hosts = allowed_hosts.split(",") |
46 | 61 | else: |
| 62 | + # Define the default allowed hosts for the application |
| 63 | + default_allowed_hosts = ["gitingest.com", "*.gitingest.com", "localhost", "127.0.0.1"] |
47 | 64 | allowed_hosts = default_allowed_hosts |
48 | 65 |
|
| 66 | +# Add middleware to enforce allowed hosts |
49 | 67 | app.add_middleware(TrustedHostMiddleware, allowed_hosts=allowed_hosts) |
| 68 | + |
| 69 | +# Set up template rendering |
50 | 70 | templates = Jinja2Templates(directory="templates") |
51 | 71 |
|
52 | 72 |
|
53 | 73 | @app.get("/health") |
54 | 74 | async def health_check() -> dict[str, str]: |
| 75 | + """ |
| 76 | + Health check endpoint to verify that the server is running. |
| 77 | +
|
| 78 | + Returns |
| 79 | + ------- |
| 80 | + dict[str, str] |
| 81 | + A JSON object with a "status" key indicating the server's health status. |
| 82 | + """ |
55 | 83 | return {"status": "healthy"} |
56 | 84 |
|
57 | 85 |
|
58 | 86 | @app.head("/") |
59 | 87 | async def head_root() -> HTMLResponse: |
60 | | - """Mirror the headers and status code of the index page""" |
| 88 | + """ |
| 89 | + Respond to HTTP HEAD requests for the root URL. |
| 90 | +
|
| 91 | + Mirrors the headers and status code of the index page. |
| 92 | +
|
| 93 | + Returns |
| 94 | + ------- |
| 95 | + HTMLResponse |
| 96 | + An empty HTML response with appropriate headers. |
| 97 | + """ |
61 | 98 | return HTMLResponse(content=None, headers={"content-type": "text/html; charset=utf-8"}) |
62 | 99 |
|
63 | 100 |
|
64 | 101 | @app.get("/api/", response_class=HTMLResponse) |
65 | 102 | @app.get("/api", response_class=HTMLResponse) |
66 | 103 | async def api_docs(request: Request) -> HTMLResponse: |
| 104 | + """ |
| 105 | + Render the API documentation page. |
| 106 | +
|
| 107 | + Parameters |
| 108 | + ---------- |
| 109 | + request : Request |
| 110 | + The incoming HTTP request. |
| 111 | +
|
| 112 | + Returns |
| 113 | + ------- |
| 114 | + HTMLResponse |
| 115 | + A rendered HTML page displaying API documentation. |
| 116 | + """ |
67 | 117 | return templates.TemplateResponse("api.jinja", {"request": request}) |
68 | 118 |
|
69 | 119 |
|
70 | 120 | @app.get("/robots.txt") |
71 | 121 | async def robots() -> FileResponse: |
| 122 | + """ |
| 123 | + Serve the `robots.txt` file to guide search engine crawlers. |
| 124 | +
|
| 125 | + Returns |
| 126 | + ------- |
| 127 | + FileResponse |
| 128 | + The `robots.txt` file located in the static directory. |
| 129 | + """ |
72 | 130 | return FileResponse("static/robots.txt") |
73 | 131 |
|
74 | 132 |
|
| 133 | +# Include routers for modular endpoints |
75 | 134 | app.include_router(index) |
76 | 135 | app.include_router(download) |
77 | 136 | app.include_router(dynamic) |
0 commit comments