feat: DSH编排基建首版(server/tests 88用例/P2插件/8020端口/PORT-NOTE/MOBILE_APP)
This commit is contained in:
@@ -0,0 +1,26 @@
|
||||
"""Request-scoped dependencies shared by the routers."""
|
||||
from __future__ import annotations
|
||||
|
||||
import psycopg
|
||||
from fastapi import HTTPException, Request
|
||||
|
||||
from .auth import Ctx
|
||||
from .config import Config
|
||||
from .db import connect
|
||||
|
||||
|
||||
def get_cfg(request: Request) -> Config:
|
||||
return request.app.state.cfg
|
||||
|
||||
|
||||
def get_ctx(request: Request):
|
||||
cfg: Config = request.app.state.cfg
|
||||
try:
|
||||
conn = connect(cfg)
|
||||
except psycopg.Error:
|
||||
# A transient DB outage must degrade to 503, not kill the server.
|
||||
raise HTTPException(503, "database unavailable, try again shortly")
|
||||
try:
|
||||
yield Ctx(cfg, conn)
|
||||
finally:
|
||||
conn.close()
|
||||
Reference in New Issue
Block a user