46 lines
1.0 KiB
Python
46 lines
1.0 KiB
Python
"""
|
|
FastAPI 应用主入口
|
|
"""
|
|
from fastapi import FastAPI
|
|
from fastapi.middleware.cors import CORSMiddleware
|
|
from app.core.config import settings
|
|
from app.api.api_v1.api import api_router
|
|
|
|
# 创建 FastAPI 应用实例
|
|
app = FastAPI(
|
|
title=settings.PROJECT_NAME,
|
|
version=settings.VERSION,
|
|
openapi_url=f"{settings.API_V1_STR}/openapi.json",
|
|
)
|
|
|
|
# 配置 CORS
|
|
if settings.BACKEND_CORS_ORIGINS:
|
|
app.add_middleware(
|
|
CORSMiddleware,
|
|
allow_origins=[str(origin) for origin in settings.BACKEND_CORS_ORIGINS],
|
|
allow_credentials=True,
|
|
allow_methods=["*"],
|
|
allow_headers=["*"],
|
|
)
|
|
|
|
# 注册 API 路由
|
|
app.include_router(api_router, prefix=settings.API_V1_STR)
|
|
|
|
|
|
@app.get("/")
|
|
def root():
|
|
"""根路径"""
|
|
return {
|
|
"message": "欢迎使用个人博客网站 API",
|
|
"version": settings.VERSION,
|
|
"docs": "/docs",
|
|
"api": settings.API_V1_STR
|
|
}
|
|
|
|
|
|
@app.get("/health")
|
|
def health_check():
|
|
"""健康检查"""
|
|
return {"status": "ok"}
|
|
|