Spaces:
Sleeping
Sleeping
File size: 2,587 Bytes
9993c90 21e8ee7 9993c90 21e8ee7 9993c90 21e8ee7 9993c90 21e8ee7 9993c90 21e8ee7 9993c90 21e8ee7 9993c90 21e8ee7 9993c90 21e8ee7 9993c90 21e8ee7 9993c90 21e8ee7 9993c90 21e8ee7 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 | # main.py
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from fastapi.staticfiles import StaticFiles
import logging
from pathlib import Path
from dotenv import load_dotenv
import os
# Load .env
load_dotenv()
# ----------------- Import Routers -----------------
from .detection import router as detection_router
from .auth import router as auth_router
from .camera import router as camera_router
from .config import UPLOAD_DIR
from .analytics import router as analytics_router
from .view_image import router as view_images_router
# ---------------- Logging Setup -----------------
logger = logging.getLogger("ServerLogger")
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s [%(levelname)s] %(name)s: %(message)s"
)
# Ensure UPLOAD_DIR exists
Path(UPLOAD_DIR).mkdir(parents=True, exist_ok=True)
# ---------------- CORS Config -----------------
raw_origins = os.getenv("ALLOWED_ORIGINS", "")
# Clean + split safely
allowed_origins = [origin.strip() for origin in raw_origins.split(",") if origin.strip()]
print("CORS Origins:", allowed_origins)
# ---------------- Create App -----------------
def create_app() -> FastAPI:
app = FastAPI(title="Wildlife Detection & Camera API Server")
app.add_middleware(
CORSMiddleware,
allow_origins=allowed_origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# ---- Static Files ----
app.mount("/user_data", StaticFiles(directory=UPLOAD_DIR), name="user_data")
logger.info(f"Static folder mounted at /user_data -> {UPLOAD_DIR}")
# ---- Routers ----
app.include_router(auth_router, prefix="/auth", tags=["Auth"])
app.include_router(camera_router, prefix="/api", tags=["Camera"])
app.include_router(detection_router, prefix="/api/detection", tags=["Detection"])
app.include_router(analytics_router, prefix="/api", tags=["Analytics"])
app.include_router(view_images_router, prefix="/api", tags=["Images"])
# ---- Startup ----
@app.on_event("startup")
async def startup_check():
logger.info("Server started successfully. All routers are active.")
# ---- Root ----
@app.get("/")
def root():
return {
"message": "Wildlife Detection & Camera API Server is Running",
"routes": {
"auth": "/auth/...",
"camera": "/api/camera/...",
"detection": "/api/detection/...",
"analytics": "/api/analytics/..."
}
}
return app
app = create_app() |