import sys,os
current_path = os.getcwd()
sys.path.append(current_path+"\\web")

import json
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from router.graph_router import graph_router
def save_api_spec(app: FastAPI):
    """
    保存 FastAPI 应用的 OpenAPI 规范到文件中。
    """
    from fastapi.openapi.utils import get_openapi
    import yaml
    openapi_schema = get_openapi(
        title="FastAPI - Swagger UI",
        version="1.0.0",
        routes=app.routes
    )

    # 将 JSON 转换为 YAML
    yaml_data = yaml.dump(openapi_schema, sort_keys=False)

    # 保存为 YAML 文件
    with open("openapi.yaml", "w") as f:
        f.write(yaml_data)

    print("OpenAPI YAML 文件已生成：openapi.yaml")
    

app = FastAPI()
# 允许所有来源的跨域请求
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

app.include_router(graph_router)
#app.include_router(dify_kb_router)
save_api_spec(app)