-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
109 lines (86 loc) · 3.03 KB
/
main.py
File metadata and controls
109 lines (86 loc) · 3.03 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
"""GoupixDex FastAPI application."""
from __future__ import annotations
import logging
try:
from dotenv import load_dotenv
load_dotenv()
except ImportError:
pass
from fastapi import FastAPI, Request, status
from fastapi.exceptions import RequestValidationError
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import JSONResponse
from config import get_settings
from routes import access_requests as access_requests_routes
from routes import articles as articles_routes
from routes import auth as auth_routes
from routes import ebay_market_route
from routes import ebay_route
from routes import pricing_route
from routes import scan as scan_routes
from routes import settings_route
from routes import shipping_route
from routes import stats_route
from routes import users as users_routes
from core.win32_asyncio import ensure_proactor_event_loop
ensure_proactor_event_loop()
logging.basicConfig(level=logging.INFO, format="%(levelname)s %(name)s %(message)s")
logger = logging.getLogger("goupixdex")
settings = get_settings()
app = FastAPI(
title="GoupixDex API",
description="Pokémon TCG reselling backend for GoupixDex.",
version="1.0.0",
)
@app.middleware("http")
async def log_errors_middleware(request: Request, call_next): # type: ignore[no-untyped-def]
try:
return await call_next(request)
except Exception:
logger.exception("Request failed: %s %s", request.method, request.url.path)
raise
_origins = [o.strip() for o in settings.cors_origins.split(",") if o.strip()]
app.add_middleware(
CORSMiddleware,
allow_origins=_origins if _origins != ["*"] else ["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
@app.exception_handler(RequestValidationError)
async def validation_exception_handler(
request: Request,
exc: RequestValidationError,
) -> JSONResponse:
return JSONResponse(
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
content={"detail": exc.errors(), "body": exc.body},
)
@app.exception_handler(Exception)
async def unhandled_exception_handler(
request: Request,
exc: Exception,
) -> JSONResponse:
logger.exception("Unhandled error on %s %s", request.method, request.url.path)
return JSONResponse(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
content={
"detail": str(exc) or "Internal Server Error",
"error": "internal_server_error",
"path": request.url.path,
},
)
@app.get("/health")
def health() -> dict[str, str]:
return {"status": "ok"}
app.include_router(auth_routes.router, prefix="/auth")
app.include_router(users_routes.router)
app.include_router(access_requests_routes.router)
app.include_router(articles_routes.router)
app.include_router(settings_route.router)
app.include_router(ebay_route.router)
app.include_router(ebay_market_route.router)
app.include_router(pricing_route.router)
app.include_router(stats_route.router)
app.include_router(scan_routes.router)
app.include_router(shipping_route.router)