-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.py
More file actions
70 lines (53 loc) · 2.13 KB
/
main.py
File metadata and controls
70 lines (53 loc) · 2.13 KB
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
from fastapi import FastAPI, APIRouter,HTTPException,UploadFile
from fastapi.middleware.cors import CORSMiddleware
from starlette.responses import Response
from .routes.index import router
import json
app = FastAPI()
app.add_middleware(
CORSMiddleware,
allow_origins=["*"], # or ["http://localhost:8000"] for stricter rules
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
app.include_router(router, prefix="/api")
# Routes
@app.get("/")
async def home():
return "server is running"
# Middleware to parse request body
@app.middleware("http")
async def body_parser(request, call_next):
content_type = request.headers.get("Content-Type", "")
if "application/json" in content_type:
# Parse the request body once and store it in request.state for easy access throughout the request
request.state.body = await request.json()
elif "multipart/form-data" in content_type:
form = await request.form()
json_data = form.get("data")
if not json_data:
raise HTTPException(status_code=400, detail="Missing 'data' in form")
try:
request.state.body = json.loads(json_data)
except json.JSONDecodeError:
raise HTTPException(status_code=400, detail="Invalid JSON in 'data'")
file: UploadFile = form.get("file")
if not file:
raise HTTPException(status_code=400, detail="No file provided")
else:
try:
contents = await file.read()
request.state.file = {
"filename": file.filename,
"content_type": file.content_type,
"data": contents
}
except Exception as e:
raise HTTPException(status_code=400, detail=f"Failed to read file: {str(e)}")
return await call_next(request)
def start_server(host="127.0.0.1", port=8000, reload=False):
import uvicorn
uvicorn.run("python_fm_dapi_weaver.main:app", host=host, port=port, reload=reload)
if __name__ == "__main__":
start_server(reload=True)