-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
33 lines (24 loc) · 860 Bytes
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
from fastapi import FastAPI, HTTPException
import asyncpg
import config
app = FastAPI()
pool = None
@app.get("/avatars/{user_id}")
async def get_avatar(user_id: int):
if not pool or not isinstance(pool, asyncpg.Pool):
raise HTTPException(500, "Database connection pool is not initialized.")
try:
avatar = await pool.fetchval("SELECT avatar_url FROM avatars WHERE user_id = $1", user_id)
if not avatar:
raise HTTPException(404, "Avatar not found.")
return {"user_id": user_id, "avatar_url": avatar}
except asyncpg.exceptions.PostgresError:
raise HTTPException(500, "Database error.")
@app.on_event("startup")
async def startup():
global pool
pool = await asyncpg.create_pool(config.DATABASE)
@app.on_event("shutdown")
async def shutdown():
if pool:
await pool.close()