Add CORS support for frontend on localhost:3000

The frontend at http://localhost:3000 was getting CORS (Cross-Origin Resource Sharing)
errors when trying to make API requests to http://localhost:8000.

Added CORSMiddleware to FastAPI backend with:
- allow_origins: http://localhost:3000 and http://127.0.0.1:3000
- allow_credentials: true (for Authorization headers)
- allow_methods: all (GET, POST, OPTIONS, etc.)
- allow_headers: all (including Authorization, Content-Type, etc.)

This allows the browser to make cross-origin requests from the frontend to the API.

CORS headers now properly returned:
  access-control-allow-origin: http://localhost:3000
  access-control-allow-credentials: true

Co-authored-by: Copilot <[email protected]>
This commit is contained in:
John Patrick TeruelandCopilot committed 2026-04-24 21:11:56 +08:00
1 parent 78a73acec3
commit 186cd4cc7c
4 files changed
+10

No files matched your search

+10
View File
@@ -15,6 +15,7 @@ fundamental to scalable microservices architecture.
from fastapi import FastAPI, Depends, HTTPException, status
from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel
from jose import jwt, JWTError
from kafka import KafkaProducer
@@ -129,6 +130,15 @@ app = FastAPI(
version="1.0.0"
)
# Add CORS middleware to allow requests from frontend
app.add_middleware(
CORSMiddleware,
allow_origins=["http://localhost:3000", "http://127.0.0.1:3000"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
@app.get("/health")
async def health_check():
"""Health check endpoint"""