mirror of
https://github.com/Starlio-app/Starlio-web.git
synced 2025-03-03 22:41:25 +03:00
refactor: add English docstrings to all functions
This commit is contained in:
parent
06ca56f019
commit
cabcccc0fb
4 changed files with 82 additions and 9 deletions
11
main.py
11
main.py
|
@ -1,3 +1,14 @@
|
||||||
|
"""
|
||||||
|
This module defines the main FastAPI application, including route handlers,
|
||||||
|
middleware, static file serving, and error handling.
|
||||||
|
|
||||||
|
Routes:
|
||||||
|
- `/app-ads.txt`: Serves the `app-ads.txt` file.
|
||||||
|
- `/robots.txt`: Serves the `robots.txt` file.
|
||||||
|
- `/wallpaper/today`: Served by the wallpaper router, fetches today's wallpaper.
|
||||||
|
- `/wallpaper/{day}`: Served by the wallpaper router, fetches the wallpaper for a specific day.
|
||||||
|
"""
|
||||||
|
|
||||||
import http
|
import http
|
||||||
|
|
||||||
import uvicorn
|
import uvicorn
|
||||||
|
|
|
@ -1,13 +1,40 @@
|
||||||
|
"""
|
||||||
|
This module contains the PlausibleAnalytics middleware for sending analytics events
|
||||||
|
to Plausible Analytics after processing each request.
|
||||||
|
|
||||||
|
The middleware collects information about the request, such as the HTTP method, status code,
|
||||||
|
user agent, and source, and sends this data to the Plausible Analytics API.
|
||||||
|
|
||||||
|
In case of any client-side or server-side errors, no event is sent,
|
||||||
|
and the response is returned as usual.
|
||||||
|
"""
|
||||||
|
|
||||||
import httpx
|
import httpx
|
||||||
import yaml
|
import yaml
|
||||||
|
|
||||||
from http import HTTPStatus
|
from http import HTTPStatus
|
||||||
from user_agents import parse as ua_parse
|
from user_agents import parse as ua_parse
|
||||||
|
|
||||||
config = yaml.safe_load(open('./config.yaml'))
|
config = yaml.safe_load(open('./config.yaml'))
|
||||||
|
|
||||||
class PlausibleAnalytics:
|
class PlausibleAnalytics:
|
||||||
|
"""
|
||||||
|
Middleware for sending analytics data to Plausible Analytics
|
||||||
|
after processing each request.
|
||||||
|
"""
|
||||||
|
|
||||||
async def __call__(self, request, call_next):
|
async def __call__(self, request, call_next):
|
||||||
|
"""
|
||||||
|
Called for each request, sends an event to Plausible with
|
||||||
|
information about the request and user.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
request: FastAPI request object.
|
||||||
|
call_next: Function to call the next request handler.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
Response: FastAPI response object.
|
||||||
|
"""
|
||||||
|
|
||||||
response = await call_next(request)
|
response = await call_next(request)
|
||||||
|
|
||||||
user_agent = request.headers.get('user-agent', 'unknown')
|
user_agent = request.headers.get('user-agent', 'unknown')
|
||||||
|
@ -29,18 +56,18 @@ class PlausibleAnalytics:
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
async with httpx.AsyncClient() as client:
|
try:
|
||||||
try:
|
async with httpx.AsyncClient() as client:
|
||||||
await client.post(
|
await client.post(
|
||||||
config['analytics']['endpoint'],
|
config['analytics']['endpoint'],
|
||||||
json=event,
|
json=event,
|
||||||
headers={
|
headers={
|
||||||
"Authorization": f"Bearer {config['analytics']['token']}",
|
"Authorization": f"Bearer {config['analytics']['token']}",
|
||||||
"Content-Type": "application/json",
|
"Content-Type": "application/json",
|
||||||
"User-Agent": request.headers.get('user-agent', 'unknown'),
|
"User-Agent": user_agent,
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"Error sending event to Plausible: {e}")
|
print(f"Error sending event to Plausible: {e}")
|
||||||
|
|
||||||
return response
|
return response
|
||||||
|
|
|
@ -1,9 +1,13 @@
|
||||||
from fastapi import APIRouter, Request
|
"""
|
||||||
|
This module defines API routes for serving static HTML files.
|
||||||
|
"""
|
||||||
|
|
||||||
|
from fastapi import APIRouter
|
||||||
from fastapi.responses import FileResponse
|
from fastapi.responses import FileResponse
|
||||||
|
|
||||||
router = APIRouter()
|
router = APIRouter()
|
||||||
|
|
||||||
|
|
||||||
@router.get('/')
|
@router.get('/')
|
||||||
async def main():
|
async def main():
|
||||||
|
"""Returns the main HTML page."""
|
||||||
return FileResponse('./src/web/html/index.html')
|
return FileResponse('./src/web/html/index.html')
|
||||||
|
|
|
@ -1,3 +1,13 @@
|
||||||
|
"""
|
||||||
|
This module contains routes for serving wallpapers using FastAPI.
|
||||||
|
|
||||||
|
Routes:
|
||||||
|
- `/wallpaper/today`: Fetches the wallpaper of the day and renders it using a template.
|
||||||
|
- `/wallpaper/{day}`: Fetches a specific wallpaper by day and renders it using a template.
|
||||||
|
|
||||||
|
In case of any client or server errors or empty data, a 404 error page is returned.
|
||||||
|
"""
|
||||||
|
|
||||||
from http import HTTPStatus
|
from http import HTTPStatus
|
||||||
|
|
||||||
import requests
|
import requests
|
||||||
|
@ -12,6 +22,16 @@ template = Jinja2Templates(directory='./src/web/html')
|
||||||
|
|
||||||
@router.get('/wallpaper/today', response_class=HTMLResponse)
|
@router.get('/wallpaper/today', response_class=HTMLResponse)
|
||||||
async def today_wallpaper(request: Request):
|
async def today_wallpaper(request: Request):
|
||||||
|
"""
|
||||||
|
Fetches the wallpaper of the day and renders it using a template.
|
||||||
|
Returns a 404 page in case of client or server errors.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
request: The FastAPI request object.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
HTMLResponse: The wallpaper page or 404 error page.
|
||||||
|
"""
|
||||||
res = requests.get(f'https://api.starlio.space/last')
|
res = requests.get(f'https://api.starlio.space/last')
|
||||||
|
|
||||||
if HTTPStatus(res.status_code).is_server_error or HTTPStatus(res.status_code).is_client_error:
|
if HTTPStatus(res.status_code).is_server_error or HTTPStatus(res.status_code).is_client_error:
|
||||||
|
@ -26,6 +46,17 @@ async def today_wallpaper(request: Request):
|
||||||
|
|
||||||
@router.get('/wallpaper/{day}', response_class=HTMLResponse)
|
@router.get('/wallpaper/{day}', response_class=HTMLResponse)
|
||||||
async def wallpaper(request: Request, day):
|
async def wallpaper(request: Request, day):
|
||||||
|
"""
|
||||||
|
Fetches a specific wallpaper by day and renders it using a template.
|
||||||
|
Returns a 404 page in case of client or server errors or if the wallpaper data is empty.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
request: The FastAPI request object.
|
||||||
|
day: The day parameter used to fetch the wallpaper.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
HTMLResponse: The wallpaper page or 404 error page.
|
||||||
|
"""
|
||||||
res = requests.get(f'https://api.starlio.space/wallpaper/{day}')
|
res = requests.get(f'https://api.starlio.space/wallpaper/{day}')
|
||||||
|
|
||||||
if (HTTPStatus(res.status_code).is_server_error
|
if (HTTPStatus(res.status_code).is_server_error
|
||||||
|
|
Loading…
Add table
Reference in a new issue