Aasher commited on
Commit
9577d95
·
1 Parent(s): 8fe9ff0

feat(cors): add CORS configuration and FastAPI app setup

Browse files

- Add FRONTEND_URL to settings for CORS configuration
- Create main.py with FastAPI app, CORS middleware, and routers
- Implement lifespan management for database initialization

Files changed (2) hide show
  1. core/config.py +3 -0
  2. main.py +44 -0
core/config.py CHANGED
@@ -8,6 +8,9 @@ class Settings(BaseSettings):
8
  APP_NAME: str = "Makhfi AI"
9
  APP_VERSION: str = "0.1.0"
10
 
 
 
 
11
  # --- Supabase Configuration ---
12
  SUPABASE_KEY: str
13
  SUPABASE_URL: str
 
8
  APP_NAME: str = "Makhfi AI"
9
  APP_VERSION: str = "0.1.0"
10
 
11
+ # --- CORS ---
12
+ FRONTEND_URL: str
13
+
14
  # --- Supabase Configuration ---
15
  SUPABASE_KEY: str
16
  SUPABASE_URL: str
main.py ADDED
@@ -0,0 +1,44 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI
2
+ from fastapi.middleware.cors import CORSMiddleware
3
+ from contextlib import asynccontextmanager
4
+
5
+ from api.routers import chats
6
+ from api.routers import memory
7
+ from db.session import create_db_and_tables
8
+ from core.config import get_settings
9
+
10
+ settings = get_settings()
11
+
12
+ @asynccontextmanager
13
+ async def lifespan(app: FastAPI):
14
+ print(f"INFO: Starting up {settings.APP_NAME} v{settings.APP_VERSION}...")
15
+ create_db_and_tables()
16
+ print("INFO: Database tables checked/created.")
17
+ yield
18
+ print(f"INFO: Shutting down {settings.APP_NAME}...")
19
+
20
+ # FastAPI app instance
21
+ app = FastAPI(
22
+ title=settings.APP_NAME,
23
+ description=f"An API for interacting with {settings.APP_NAME}.",
24
+ version=settings.APP_VERSION,
25
+ lifespan=lifespan
26
+ )
27
+
28
+ # --- Middleware ---
29
+ app.add_middleware(
30
+ CORSMiddleware,
31
+ allow_origins=[settings.FRONTEND_URL],
32
+ allow_credentials=True,
33
+ allow_methods=["*"],
34
+ allow_headers=["*"],
35
+ )
36
+
37
+ # --- Routers ---
38
+ app.include_router(chats.router, prefix="/api/chats", tags=["Chats & Messages"])
39
+ app.include_router(memory.router, prefix="/api/memories", tags=["Memory Management"])
40
+
41
+ # --- Root Endpoint ---
42
+ @app.get("/", tags=["Health"])
43
+ async def read_root():
44
+ return {"status": "ok", "message": "Welcome to Makhfi AI!"}