Harshith Reddy commited on
Commit
c478bee
·
1 Parent(s): 88312c3

Fix CORS for GitHub Pages and improve multipart request handling

Browse files
Files changed (1) hide show
  1. app.py +16 -3
app.py CHANGED
@@ -281,13 +281,23 @@ async def http_exception_handler(request: Request, exc: StarletteHTTPException):
281
  content={"detail": exc.detail}
282
  )
283
 
 
 
 
 
 
 
 
 
284
  api_app.add_middleware(
285
  CORSMiddleware,
286
- allow_origins=["*"],
287
  allow_credentials=True,
288
- allow_methods=["*"],
289
  allow_headers=["*"],
 
290
  )
 
291
 
292
  @api_app.get("/")
293
  @api_app.get("/api")
@@ -344,14 +354,17 @@ async def download_mask(token: str):
344
  @api_app.post("/segment", response_class=JSONResponse, include_in_schema=True)
345
  @api_app.post("/api/segment", response_class=JSONResponse, include_in_schema=True)
346
  async def segment_liver(
 
347
  file: UploadFile = File(..., description="NIfTI file to segment"),
348
  modality: str = Form("T1", description="MRI modality: T1 or T2"),
349
  slice_idx: Optional[int] = Form(None, description="Optional slice index")
350
  ):
351
  print("=" * 60)
352
  print(f"API REQUEST RECEIVED: /api/segment")
 
 
353
  print(f" File: {file.filename if file else 'None'}")
354
- print(f" Content-Type: {file.content_type if file else 'None'}")
355
  print(f" Modality: {modality}")
356
  print(f" Slice Index: {slice_idx}")
357
  print("=" * 60)
 
281
  content={"detail": exc.detail}
282
  )
283
 
284
+ allowed_origins = [
285
+ "https://harshithreddy01.github.io",
286
+ "https://harshithreddy01.github.io/frontend-SRMA-Liver",
287
+ "http://localhost:5173",
288
+ "http://localhost:3000",
289
+ "http://localhost:8080",
290
+ ]
291
+
292
  api_app.add_middleware(
293
  CORSMiddleware,
294
+ allow_origins=allowed_origins,
295
  allow_credentials=True,
296
+ allow_methods=["GET", "POST", "OPTIONS", "HEAD"],
297
  allow_headers=["*"],
298
+ expose_headers=["*"],
299
  )
300
+ print(f"✓ CORS configured for origins: {allowed_origins}")
301
 
302
  @api_app.get("/")
303
  @api_app.get("/api")
 
354
  @api_app.post("/segment", response_class=JSONResponse, include_in_schema=True)
355
  @api_app.post("/api/segment", response_class=JSONResponse, include_in_schema=True)
356
  async def segment_liver(
357
+ request: Request,
358
  file: UploadFile = File(..., description="NIfTI file to segment"),
359
  modality: str = Form("T1", description="MRI modality: T1 or T2"),
360
  slice_idx: Optional[int] = Form(None, description="Optional slice index")
361
  ):
362
  print("=" * 60)
363
  print(f"API REQUEST RECEIVED: /api/segment")
364
+ print(f" Origin: {request.headers.get('origin', 'N/A')}")
365
+ print(f" Content-Type: {request.headers.get('content-type', 'N/A')}")
366
  print(f" File: {file.filename if file else 'None'}")
367
+ print(f" File Content-Type: {file.content_type if file else 'None'}")
368
  print(f" Modality: {modality}")
369
  print(f" Slice Index: {slice_idx}")
370
  print("=" * 60)