Deploy-Backend / main.py
Quratulain1's picture
Add application file
b45d578
Raw History Blame Contribute Delete
1.79 kB
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from core.config import settings
from api.router import api_router
import logging
# Configure logging to show INFO level messages
logging.basicConfig(
level=logging.INFO,
format='%(levelname)s: %(message)s'
)
app = FastAPI(
title=settings.PROJECT_NAME,
openapi_url=f"{settings.API_V1_STR}/openapi.json",
docs_url="/docs",
redoc_url="/redoc",
)
# Set all CORS enabled origins
# CRITICAL: Must explicitly allow Authorization header for Better Auth JWT tokens
app.add_middleware(
CORSMiddleware,
allow_origins=[
"http://localhost:3000",
"http://localhost:3001",
"http://localhost:3002",
"http://localhost:3003",
"http://localhost:3004",
"http://localhost:3005"
], # Frontend URLs - support any port Next.js chooses
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"], # This includes Authorization header
expose_headers=["*"], # Expose all headers including custom ones
)
app.include_router(api_router, prefix=settings.API_V1_STR)
@app.post("/api/log")
async def log_message(data: dict):
# Fixed: Use the directory of this file (main.py) to ensure app.log is in the backend/ folder
import os
current_dir = os.path.dirname(os.path.abspath(__file__))
log_path = os.path.join(current_dir, "app.log")
with open(log_path, "a") as f:
import datetime
timestamp = datetime.datetime.now().isoformat()
f.write(f"[{timestamp}] {data.get('message')}\n")
return {"status": "logged"}
@app.get("/health")
async def health_check():
return {"status": "ok"}
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=8000)