main.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. from fastapi import FastAPI
  2. from .api import vector, raster, cd_prediction
  3. from .database import engine, Base
  4. from fastapi.middleware.cors import CORSMiddleware
  5. # 创建数据库表
  6. Base.metadata.create_all(bind=engine)
  7. app = FastAPI(
  8. title="地图数据处理系统",
  9. description="一个用于处理地图数据的API系统",
  10. version="1.0.0",
  11. openapi_tags=[
  12. {
  13. "name": "vector",
  14. "description": "矢量数据相关接口",
  15. },
  16. {
  17. "name": "raster",
  18. "description": "栅格数据相关接口",
  19. },
  20. {
  21. "name": "cd-prediction",
  22. "description": "Cd预测模型相关接口",
  23. }
  24. ]
  25. )
  26. # ---------------------------
  27. # 添加 CORS 配置(关键修改)
  28. # ---------------------------
  29. app.add_middleware(
  30. CORSMiddleware,
  31. allow_origins=["https://soilgd.com"], # 允许的前端域名(需与前端实际域名一致)
  32. allow_methods=["*"], # 允许的 HTTP 方法(GET/POST/PUT/DELETE等)
  33. allow_headers=["*"], # 允许的请求头
  34. allow_credentials=True, # 允许携带 Cookie(如需)
  35. )
  36. # 注册路由
  37. app.include_router(vector.router, prefix="/api/vector", tags=["vector"])
  38. app.include_router(raster.router, prefix="/api/raster", tags=["raster"])
  39. app.include_router(cd_prediction.router, prefix="/api/cd-prediction", tags=["cd-prediction"])
  40. @app.get("/")
  41. async def root():
  42. return {"message": "Welcome to the GIS Data Management API"}
  43. # if __name__ == "__main__":
  44. # import uvicorn
  45. # uvicorn.run(app, host="0.0.0.0", port=8000)