main.py 961 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. # backend/app/main.py
  2. from fastapi import FastAPI
  3. from fastapi.middleware.cors import CORSMiddleware
  4. from .core.config import settings
  5. from .database import engine, Base
  6. from .api import categories, spiders, executions
  7. # 创建数据库表
  8. Base.metadata.create_all(bind=engine)
  9. app = FastAPI(
  10. title=settings.PROJECT_NAME,
  11. version=settings.VERSION
  12. )
  13. # CORS配置
  14. app.add_middleware(
  15. CORSMiddleware,
  16. allow_origins=settings.CORS_ORIGINS,
  17. allow_credentials=True,
  18. allow_methods=["*"],
  19. allow_headers=["*"],
  20. )
  21. # 注册路由
  22. app.include_router(categories.router)
  23. app.include_router(spiders.router)
  24. app.include_router(executions.router)
  25. @app.get("/")
  26. def read_root():
  27. return {"message": "爬虫任务调度系统 API"}
  28. @app.get("/health")
  29. def health_check():
  30. return {"status": "healthy", "service": "spider-scheduler"}
  31. if __name__ == "__main__":
  32. import uvicorn
  33. uvicorn.run(app, host="0.0.0.0", port=8000, reload=True)