File size: 1,201 Bytes
21c7db9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""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()