mirror of
https://github.com/Starlio-app/Starlio-web.git
synced 2025-03-03 22:41:25 +03:00
refactor: The long lines were broken into smaller ones
This commit is contained in:
parent
6f304d650e
commit
4f70c6c0e3
3 changed files with 34 additions and 14 deletions
11
main.py
11
main.py
|
@ -5,8 +5,10 @@ middleware, static file serving, and error handling.
|
||||||
Routes:
|
Routes:
|
||||||
- `/app-ads.txt`: Serves the `app-ads.txt` file.
|
- `/app-ads.txt`: Serves the `app-ads.txt` file.
|
||||||
- `/robots.txt`: Serves the `robots.txt` file.
|
- `/robots.txt`: Serves the `robots.txt` file.
|
||||||
- `/wallpaper/today`: Served by the wallpaper router, fetches today's wallpaper.
|
- `/wallpaper/today`: Served by the wallpaper router,
|
||||||
- `/wallpaper/{day}`: Served by the wallpaper router, fetches the wallpaper for a specific day.
|
fetches today's wallpaper.
|
||||||
|
- `/wallpaper/{day}`: Served by the wallpaper router,
|
||||||
|
fetches the wallpaper for a specific day.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import http
|
import http
|
||||||
|
@ -46,7 +48,10 @@ async def robots_txt():
|
||||||
|
|
||||||
@app.exception_handler(404)
|
@app.exception_handler(404)
|
||||||
async def not_found(req, __):
|
async def not_found(req, __):
|
||||||
return FileResponse('./src/web/html/error/404.html', status_code=http.HTTPStatus.NOT_FOUND)
|
return FileResponse(
|
||||||
|
'./src/web/html/error/404.html',
|
||||||
|
status_code=http.HTTPStatus.NOT_FOUND
|
||||||
|
)
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
uvicorn.run(app,
|
uvicorn.run(app,
|
||||||
|
|
|
@ -1,8 +1,10 @@
|
||||||
"""
|
"""
|
||||||
This module contains the PlausibleAnalytics middleware for sending analytics events
|
This module contains the PlausibleAnalytics middleware
|
||||||
|
for sending analytics events
|
||||||
to Plausible Analytics after processing each request.
|
to Plausible Analytics after processing each request.
|
||||||
|
|
||||||
The middleware collects information about the request, such as the HTTP method, status code,
|
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.
|
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,
|
In case of any client-side or server-side errors, no event is sent,
|
||||||
|
@ -51,8 +53,10 @@ class PlausibleAnalytics:
|
||||||
"props": {
|
"props": {
|
||||||
"method": request.method,
|
"method": request.method,
|
||||||
"statusCode": response.status_code,
|
"statusCode": response.status_code,
|
||||||
"browser": f"{user_agent_parsed.browser.family} {user_agent_parsed.browser.version_string}",
|
"browser": f"{user_agent_parsed.browser.family}" \
|
||||||
"os": f"{user_agent_parsed.os.family} {user_agent_parsed.os.version_string}",
|
f"{user_agent_parsed.browser.version_string}",
|
||||||
|
"os": f"{user_agent_parsed.os.family}" \
|
||||||
|
f"{user_agent_parsed.os.version_string}",
|
||||||
"source": request.headers.get('referer', 'direct'),
|
"source": request.headers.get('referer', 'direct'),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,10 +2,13 @@
|
||||||
This module contains routes for serving wallpapers using FastAPI.
|
This module contains routes for serving wallpapers using FastAPI.
|
||||||
|
|
||||||
Routes:
|
Routes:
|
||||||
- `/wallpaper/today`: Fetches the wallpaper of the day and renders it using a template.
|
- `/wallpaper/today`: Fetches the wallpaper of the day
|
||||||
- `/wallpaper/{day}`: Fetches a specific wallpaper by day and renders it using a template.
|
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.
|
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
|
||||||
|
@ -34,8 +37,12 @@ async def today_wallpaper(request: Request):
|
||||||
"""
|
"""
|
||||||
res = requests.get('https://api.starlio.space/last', timeout=3)
|
res = requests.get('https://api.starlio.space/last', timeout=3)
|
||||||
|
|
||||||
if HTTPStatus(res.status_code).is_server_error or HTTPStatus(res.status_code).is_client_error:
|
if (HTTPStatus(res.status_code).is_server_error
|
||||||
return FileResponse('../web/html/error/404.html', status_code=HTTPStatus.NOT_FOUND)
|
or HTTPStatus(res.status_code).is_client_error):
|
||||||
|
return FileResponse(
|
||||||
|
'../web/html/error/404.html',
|
||||||
|
status_code=HTTPStatus.NOT_FOUND
|
||||||
|
)
|
||||||
|
|
||||||
return template.TemplateResponse(
|
return template.TemplateResponse(
|
||||||
request,
|
request,
|
||||||
|
@ -48,7 +55,8 @@ async def today_wallpaper(request: Request):
|
||||||
async def wallpaper(request: Request, day):
|
async def wallpaper(request: Request, day):
|
||||||
"""
|
"""
|
||||||
Fetches a specific wallpaper by day and renders it using a template.
|
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.
|
Returns a 404 page in case of client or server errors \
|
||||||
|
or if the wallpaper data is empty.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
request: The FastAPI request object.
|
request: The FastAPI request object.
|
||||||
|
@ -63,7 +71,10 @@ async def wallpaper(request: Request, day):
|
||||||
or HTTPStatus(res.status_code).is_client_error
|
or HTTPStatus(res.status_code).is_client_error
|
||||||
or len(res.json()) <= 0):
|
or len(res.json()) <= 0):
|
||||||
|
|
||||||
return FileResponse('./src/web/html/error/404.html', status_code=HTTPStatus.NOT_FOUND)
|
return FileResponse(
|
||||||
|
'./src/web/html/error/404.html',
|
||||||
|
status_code=HTTPStatus.NOT_FOUND
|
||||||
|
)
|
||||||
|
|
||||||
return template.TemplateResponse(
|
return template.TemplateResponse(
|
||||||
request,
|
request,
|
||||||
|
|
Loading…
Add table
Reference in a new issue