-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
58 lines (48 loc) · 2 KB
/
Copy pathmain.py
File metadata and controls
58 lines (48 loc) · 2 KB
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
from app import __version__
from fastapi import FastAPI, Request
from fastapi.responses import JSONResponse
from fastapi.exceptions import RequestValidationError
from app.utils.make_meta import make_meta
from fastapi.middleware.cors import CORSMiddleware
from fastapi.staticfiles import StaticFiles
from fastapi.responses import FileResponse
import os
from app.api.routes import router
app = FastAPI(
title="Python°",
description="FastAPI, Postgres, tsvector",
version=__version__,
)
def get_allowed_origins() -> list[str]:
configured_origins = os.getenv("ALLOWED_ORIGINS", "")
if configured_origins:
return [origin.strip() for origin in configured_origins.split(",") if origin.strip()]
return [
"http://localhost:3000",
"http://localhost:8000",
"http://127.0.0.1:3000",
"http://127.0.0.1:8000",
"https://goldlabel.pro"
]
# CORS middleware with an explicit, environment-driven allow-list.
app.add_middleware(
CORSMiddleware,
allow_origins=get_allowed_origins(),
allow_origin_regex=os.getenv("CORS_ALLOW_ORIGIN_REGEX"),
allow_credentials=False,
allow_methods=["GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"],
allow_headers=["Accept", "Accept-Language", "Content-Language", "Content-Type", "Authorization", "X-API-Key"],
)
# Global validation error handler for make_meta pattern
@app.exception_handler(RequestValidationError)
async def validation_exception_handler(request: Request, exc: RequestValidationError):
msg = exc.errors()[0]['msg'] if exc.errors() else str(exc)
return JSONResponse(status_code=422, content={"meta": make_meta("error", msg)})
app.include_router(router)
# Mount static directory
app.mount("/static", StaticFiles(directory=os.path.join(os.path.dirname(__file__), "static")), name="static")
# Favicon route
@app.get("/favicon.ico", include_in_schema=False)
async def favicon():
favicon_path = os.path.join(os.path.dirname(__file__), "static", "favicon.ico")
return FileResponse(favicon_path)