Spaces:
Sleeping
Sleeping
Download backend/main.py from orakzai-io/Rednote: direct link, hf CLI and curl.
- Browser
- Download file 1.32 kB
-
https://huggingface.co/spaces/orakzai-io/Rednote/resolve/main/backend/main.py
- Command line
-
hf download hf://spaces/orakzai-io/Rednote/backend/main.py
-
curl -L -o main.py https://huggingface.co/spaces/orakzai-io/Rednote/resolve/main/backend/main.py
1.32 kB
| import os | |
| from contextlib import asynccontextmanager | |
| from fastapi import FastAPI | |
| from fastapi.middleware.cors import CORSMiddleware | |
| from fastapi.staticfiles import StaticFiles | |
| from db.init import init_database | |
| from routers import documents, chat, auth | |
| from core.config import settings | |
| async def lifespan(app: FastAPI): | |
| await init_database() | |
| yield | |
| app = FastAPI(title="RAG Backend API", lifespan=lifespan) | |
| # Configure CORS — restrict to allowed origins from settings | |
| app.add_middleware( | |
| CORSMiddleware, | |
| allow_origins=settings.allowed_origins_list, | |
| allow_credentials=True, | |
| allow_methods=["*"], | |
| allow_headers=["*"], | |
| ) | |
| # Register routers | |
| app.include_router(documents.router) | |
| app.include_router(chat.router) | |
| app.include_router(auth.router) | |
| # Serve frontend static files in single-container mode (e.g. Hugging Face Spaces). | |
| # Must be mounted AFTER all API routes to avoid catching API requests. | |
| static_dir = os.path.join(os.path.dirname(__file__), "static") | |
| if os.path.exists(static_dir): | |
| app.mount("/", StaticFiles(directory=static_dir, html=True), name="static") | |
| else: | |
| async def root(): | |
| return {"status": "running"} | |
| if __name__ == "__main__": | |
| import uvicorn | |
| uvicorn.run("main:app", host="0.0.0.0", port=8000, reload=True) | |