generated from root/feiqiu-ETL
正式起服务打底
This commit is contained in:
51
app/main.py
Normal file
51
app/main.py
Normal file
@@ -0,0 +1,51 @@
|
||||
# app/main.py
|
||||
from fastapi import FastAPI, Header, Depends
|
||||
|
||||
from .config import get_settings
|
||||
from .db import check_health
|
||||
|
||||
app = FastAPI(
|
||||
title="LLZQ Backend",
|
||||
version="0.1.0",
|
||||
)
|
||||
|
||||
|
||||
def resolve_db_name(x_llzq_env: str | None = Header(default=None, alias="X-LLZQ-Env")) -> str:
|
||||
"""
|
||||
从请求头 X-LLZQ-Env 中解析要用的 DB 名。
|
||||
"""
|
||||
settings = get_settings()
|
||||
return settings.resolve_db_name(x_llzq_env)
|
||||
|
||||
|
||||
@app.get("/api/ping")
|
||||
def ping(db_name: str = Depends(resolve_db_name)):
|
||||
"""
|
||||
最简单调通接口:
|
||||
- 返回当前后端环境
|
||||
- 返回实际连接的 DB 名
|
||||
- 简单做一个 DB 健康检查
|
||||
"""
|
||||
settings = get_settings()
|
||||
ok = False
|
||||
error_msg = None
|
||||
|
||||
try:
|
||||
ok = check_health(db_name)
|
||||
except Exception as e:
|
||||
error_msg = str(e)
|
||||
|
||||
return {
|
||||
"ok": ok,
|
||||
"app_env": settings.app_env,
|
||||
"db": db_name,
|
||||
"error": error_msg,
|
||||
}
|
||||
|
||||
|
||||
@app.get("/api/healthz")
|
||||
def healthz():
|
||||
"""
|
||||
给 Nginx / 监控用的探活接口,不查 DB。
|
||||
"""
|
||||
return {"status": "ok"}
|
||||
Reference in New Issue
Block a user