TheJackBright's picture
Deploy GitHub root master to Space
c296d62
"""API application entrypoint."""
from __future__ import annotations
import os
import uvicorn
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from app.common.config import load_project_env
from app.api.routes import router
load_project_env()
_cors_local = [
"http://127.0.0.1:5173",
"http://localhost:5173",
]
_extra = os.getenv("POLYGUARD_CORS_ORIGINS", "").strip()
if _extra and _extra != "*":
_cors_local = _cors_local + [o.strip() for o in _extra.split(",") if o.strip()]
_hf_space_regex = None
if os.getenv("POLYGUARD_ALLOW_HF_SPACE_CORS", "").lower() in {"1", "true", "yes", "on"}:
_hf_space_regex = r"https://.*\.hf\.space"
app = FastAPI(title="POLYGUARD-RL API", version="0.1.0")
app.add_middleware(
CORSMiddleware,
allow_origins=_cors_local,
allow_origin_regex=_hf_space_regex,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
app.include_router(router)
def main() -> None:
host = os.getenv("POLYGUARD_API_HOST", "127.0.0.1")
port = int(os.getenv("POLYGUARD_API_PORT", "8200"))
uvicorn.run("app.api:app", host=host, port=port, reload=False)
if __name__ == "__main__":
main()