Spaces:
Sleeping
Sleeping
ZakharZokhar
commited on
Commit
•
55d189c
1
Parent(s):
ae4dde1
Update main.py
Browse files
main.py
CHANGED
@@ -3,7 +3,6 @@ from pydantic import BaseModel
|
|
3 |
import logging
|
4 |
from fastapi.responses import JSONResponse
|
5 |
from fastapi.templating import Jinja2Templates
|
6 |
-
import httpx
|
7 |
|
8 |
# Настройка логгера
|
9 |
logger = logging.getLogger(__name__)
|
@@ -25,31 +24,11 @@ class TextRequest(BaseModel):
|
|
25 |
# Подключаем шаблоны Jinja2
|
26 |
templates = Jinja2Templates(directory="templates")
|
27 |
|
28 |
-
async def send_post_request(url: str, filename: str):
|
29 |
-
logger.info(f"POST request must be sent to {url} with filename: {filename}")
|
30 |
-
async with httpx.AsyncClient() as client:
|
31 |
-
response = await client.post(url, json={"text": filename})
|
32 |
-
|
33 |
-
logger.info(f"Staus code: {response.status_code}, Data: {response.text}")
|
34 |
-
|
35 |
# Главная страница с текстом "server is running"
|
36 |
@app.get("/")
|
37 |
async def read_root(request: Request):
|
38 |
return templates.TemplateResponse("index.html", {"request": request, "text": "server is running"})
|
39 |
|
40 |
-
# Определяем единственный POST endpoint
|
41 |
@app.post("/echo")
|
42 |
-
def echo_text(
|
43 |
-
|
44 |
-
return {"echoed_text": text_request.text}
|
45 |
-
|
46 |
-
@app.post("/upload")
|
47 |
-
async def upload_file_and_post(file: UploadFile = File(...), url: str = Form(...), background_tasks: BackgroundTasks = BackgroundTasks()):
|
48 |
-
try:
|
49 |
-
# Добавляем задачу на отправку POST запроса в фоновые задачи
|
50 |
-
background_tasks.add_task(send_post_request, url, file.filename)
|
51 |
-
|
52 |
-
# Возвращаем имя файла в ответе
|
53 |
-
return JSONResponse(content={"filename": file.filename})
|
54 |
-
except Exception as e:
|
55 |
-
raise HTTPException(status_code=500, detail=str(e))
|
|
|
3 |
import logging
|
4 |
from fastapi.responses import JSONResponse
|
5 |
from fastapi.templating import Jinja2Templates
|
|
|
6 |
|
7 |
# Настройка логгера
|
8 |
logger = logging.getLogger(__name__)
|
|
|
24 |
# Подключаем шаблоны Jinja2
|
25 |
templates = Jinja2Templates(directory="templates")
|
26 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
# Главная страница с текстом "server is running"
|
28 |
@app.get("/")
|
29 |
async def read_root(request: Request):
|
30 |
return templates.TemplateResponse("index.html", {"request": request, "text": "server is running"})
|
31 |
|
|
|
32 |
@app.post("/echo")
|
33 |
+
def echo_text(json_request: Dict[str, Any]):
|
34 |
+
return {"id": "1234456"}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|