| 1 |
from fastapi import BackgroundTasks, Depends, FastAPI |
| 2 |
|
| 3 |
app = FastAPI() |
| 4 |
|
| 5 |
|
| 6 |
def write_log(message: str): |
| 7 |
with open("log.txt", mode="a") as log: |
| 8 |
log.write(message) |
| 9 |
|
| 10 |
|
| 11 |
def get_query(background_tasks: BackgroundTasks, q: str | None = None): |
| 12 |
if q: |
| 13 |
message = f"found query: {q}\n" |
| 14 |
background_tasks.add_task(write_log, message) |
| 15 |
return q |
| 16 |
|
| 17 |
|
| 18 |
@app.post("/send-notification/{email}") |
| 19 |
async def send_notification( |
| 20 |
email: str, background_tasks: BackgroundTasks, q: str = Depends(get_query) |
| 21 |
): |
| 22 |
message = f"message to {email}\n" |
| 23 |
background_tasks.add_task(write_log, message) |
| 24 |
return {"message": "Message sent"} |