File size: 1,492 Bytes
f0ebec2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
34
35
36
37
38
from fastapi import FastAPI
from fastapi.responses import JSONResponse
from pydantic import BaseModel # Bleibt, da FastAPI es für Request Body Parsing nutzt

# Deine FastAPI-Instanz
app = FastAPI(title="Minimal FastAPI Test App")

# Eine einfache Request-Modell-Klasse (auch wenn wir sie hier nicht wirklich nutzen,
# zeigt es, dass Pydantic funktioniert)
class TestRequest(BaseModel):
    message: str = "Hello"

# Ein einfacher "Hello World"-Endpunkt, der auf POST-Anfragen reagiert
@app.post("/test")
async def test_api_endpoint(request_data: TestRequest):
    """
    Ein einfacher Test-Endpunkt.
    Gibt eine Begrüßungsnachricht zurück, die die empfangene Nachricht enthält.
    """
    print(f"INFO: Received test request with message: {request_data.message}") # Log für den Space
    return JSONResponse(content={"status": "success", "message": f"Hello from FastAPI! You sent: {request_data.message}"})

# Ein optionaler Root-Endpunkt (oft gut für Health-Checks)
@app.get("/")
async def read_root():
    """
    Root-Endpunkt für grundlegenden Health-Check.
    """
    return {"message": "FastAPI is running!"}

print("INFO: FastAPI application script finished execution and defined 'app' variable.")

# Dieser Block wird in Hugging Face Spaces nicht direkt ausgeführt, da Uvicorn
# die App direkt lädt, aber er ist für lokale Tests nützlich.
if __name__ == "__main__":
    import uvicorn
    uvicorn.run(app, host="0.0.0.0", port=7860) # Lokaler Port 7860, wie in Space