Spaces:
Runtime error
Runtime error
Download main.py from cloudaiRambo/cloudairambo: direct link, hf CLI and curl.
- Browser
- Download file 3.94 kB
-
https://huggingface.co/spaces/cloudaiRambo/cloudairambo/resolve/main/main.py
- Command line
-
hf download hf://spaces/cloudaiRambo/cloudairambo/main.py
-
curl -L -o main.py https://huggingface.co/spaces/cloudaiRambo/cloudairambo/resolve/main/main.py
3.94 kB
| import os | |
| from fastapi import FastAPI | |
| from fastapi.middleware.cors import CORSMiddleware | |
| from starlette.middleware.sessions import SessionMiddleware | |
| from fastapi.staticfiles import StaticFiles | |
| # π 1. ENSURE DIRECTORIES EXIST | |
| os.makedirs("static/output", exist_ok=True) | |
| os.makedirs("uploads", exist_ok=True) | |
| os.makedirs("tmp", exist_ok=True) | |
| os.makedirs("temp", exist_ok=True) | |
| # π 2. INITIALIZE RAM DATABASE TABLES | |
| from core.database import Base, engine | |
| Base.metadata.create_all(bind=engine) | |
| from core.admin import setup_admin | |
| from core.config import settings | |
| # Routers | |
| from routers.ratings import router as ratings_router | |
| from routers import ratings | |
| from routers import network_tools | |
| from routers import ( | |
| pdf, | |
| image, | |
| audio, | |
| video, | |
| utility, | |
| business, | |
| developer, | |
| converter_tools, | |
| admin_auth, | |
| stats, | |
| ads, | |
| tool_tracking, | |
| image_tools, | |
| devtools, | |
| ) | |
| from routers.pdf_watermark import router as watermark_router | |
| from routers.universal_converter_full import router as universal_converter_router | |
| # ------------------------------------------------------- | |
| # π APP INIT | |
| # ------------------------------------------------------- | |
| app = FastAPI(title="Tools Backend API", version="1.0.0") | |
| LOCAL_ORIGINS = [ | |
| "http://localhost:3000", | |
| "http://127.0.0.1:3000", | |
| "http://localhost:5173", | |
| "http://127.0.0.1:5173", | |
| "http://localhost:7860", | |
| ] | |
| PRODUCTION_ORIGINS = [ | |
| "https://freetoolshub.cloudairambo.com", | |
| "https://www.freetoolshub.cloudairambo.com", | |
| "https://cloudaipdf.com", | |
| "https://www.cloudaipdf.com", | |
| "https://api.cloudaipdf.com", | |
| "https://upload.cloudaipdf.com", | |
| "https://cloudairambo-cloudairambo.hf.space", | |
| ] | |
| # ------------------------------------------------------- | |
| # π‘οΈ MIDDLEWARES (Order is crucial in FastAPI/Starlette) | |
| # Middlewares run in reverse order: | |
| # 1. SessionMiddleware runs inside | |
| # 2. CORSMiddleware is added LAST so it is the outermost wrapper, | |
| # ensuring ALL responses (even 403, 405, 500) have CORS headers. | |
| # ------------------------------------------------------- | |
| app.add_middleware( | |
| SessionMiddleware, | |
| secret_key=getattr(settings, "SECRET_KEY", "default-fallback-secret-key-32chars"), | |
| same_site="none", | |
| https_only=True, | |
| ) | |
| app.add_middleware( | |
| CORSMiddleware, | |
| allow_origins=LOCAL_ORIGINS + PRODUCTION_ORIGINS, | |
| allow_origin_regex=r"https://.*\.hf\.space", | |
| allow_credentials=True, | |
| allow_methods=["*"], | |
| allow_headers=["*"], | |
| expose_headers=["*"], | |
| ) | |
| app.mount("/static", StaticFiles(directory="static"), name="static") | |
| app.mount("/uploads", StaticFiles(directory="uploads"), name="uploads") | |
| # π‘οΈ ADMIN | |
| setup_admin(app) | |
| # π ROUTERS | |
| app.include_router(pdf.router, prefix="/pdf") | |
| app.include_router(image.router, prefix="/image") | |
| app.include_router(image_tools.router, prefix="/image-tools") | |
| app.include_router(image_tools.router, prefix="/converters") | |
| app.include_router(audio.router, prefix="/audio") | |
| app.include_router(video.router, prefix="/video") | |
| app.include_router(utility.router, prefix="/utility") | |
| app.include_router(business.router, prefix="/business") | |
| app.include_router(developer.router, prefix="/developer") | |
| app.include_router(converter_tools.router, prefix="/tools") | |
| app.include_router(universal_converter_router, prefix="/convert") | |
| app.include_router(network_tools.router, prefix="/network") | |
| app.include_router(ads.router) | |
| app.include_router(devtools.router) | |
| app.include_router(stats.router) | |
| app.include_router(tool_tracking.router) | |
| app.include_router(ratings_router) | |
| app.include_router(admin_auth.router, prefix="/admin-auth") | |
| # ------------------------------------------------------- | |
| # π API STATUS | |
| # ------------------------------------------------------- | |
| def home(): | |
| return {"message": "Tools Backend Running Successfully!"} | |
| def health(): | |
| return {"status": "ok"} |