Skip to content

Commit 13fb9af

Browse files
docs: add docstrings and comments to main.py
1 parent ade83e5 commit 13fb9af

File tree

1 file changed

+70
-11
lines changed

1 file changed

+70
-11
lines changed

src/main.py

Lines changed: 70 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,65 +13,124 @@
1313
from routers import download, dynamic, index
1414
from server_utils import limiter
1515

16+
# Load environment variables from .env file
1617
load_dotenv()
1718

19+
# Initialize the FastAPI application
1820
app = FastAPI()
1921
app.state.limiter = limiter
2022

2123

22-
# Define a wrapper handler with the correct signature
2324
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+
"""
2440
if isinstance(exc, RateLimitExceeded):
25-
# Delegate to the actual handler
41+
# Delegate to the default rate limit handler
2642
return _rate_limit_exceeded_handler(request, exc)
27-
# Optionally, handle other exceptions or re-raise
43+
# Re-raise other exceptions
2844
raise exc
2945

3046

31-
# Register the wrapper handler
47+
# Register the custom exception handler for rate limits
3248
app.add_exception_handler(RateLimitExceeded, rate_limit_exception_handler)
3349

50+
# Mount static files to serve CSS, JS, and other static assets
3451
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)
3852

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)
4156

42-
# Fetch allowed hosts from the environment variable or use the default
57+
# Fetch allowed hosts from the environment or use the default values
4358
allowed_hosts = os.getenv("ALLOWED_HOSTS")
4459
if allowed_hosts:
4560
allowed_hosts = allowed_hosts.split(",")
4661
else:
62+
# Define the default allowed hosts for the application
63+
default_allowed_hosts = ["gitingest.com", "*.gitingest.com", "localhost", "127.0.0.1"]
4764
allowed_hosts = default_allowed_hosts
4865

66+
# Add middleware to enforce allowed hosts
4967
app.add_middleware(TrustedHostMiddleware, allowed_hosts=allowed_hosts)
68+
69+
# Set up template rendering
5070
templates = Jinja2Templates(directory="templates")
5171

5272

5373
@app.get("/health")
5474
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+
"""
5583
return {"status": "healthy"}
5684

5785

5886
@app.head("/")
5987
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+
"""
6198
return HTMLResponse(content=None, headers={"content-type": "text/html; charset=utf-8"})
6299

63100

64101
@app.get("/api/", response_class=HTMLResponse)
65102
@app.get("/api", response_class=HTMLResponse)
66103
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+
"""
67117
return templates.TemplateResponse("api.jinja", {"request": request})
68118

69119

70120
@app.get("/robots.txt")
71121
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+
"""
72130
return FileResponse("static/robots.txt")
73131

74132

133+
# Include routers for modular endpoints
75134
app.include_router(index)
76135
app.include_router(download)
77136
app.include_router(dynamic)

0 commit comments

Comments
 (0)