main.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. """
  2. 文献启明星 API 服务器
  3. FastAPI应用程序入口
  4. """
  5. import logging
  6. import uvicorn
  7. from fastapi import FastAPI, HTTPException
  8. from fastapi.middleware.cors import CORSMiddleware
  9. from pathlib import Path
  10. import json
  11. import sys
  12. import os
  13. # 添加项目根目录到Python路径
  14. sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
  15. from backend.api.research import router as research_router
  16. from backend.config import CORS_ORIGINS, BASE_DIR
  17. # 配置日志
  18. logging.basicConfig(
  19. level=logging.INFO,
  20. format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
  21. handlers=[
  22. logging.StreamHandler(),
  23. logging.FileHandler(Path(BASE_DIR) / "lightstar_api.log")
  24. ]
  25. )
  26. logger = logging.getLogger(__name__)
  27. # 创建FastAPI应用
  28. app = FastAPI(
  29. title="LightStar API",
  30. description="文献启明星 - 科研调研智能体API服务",
  31. version="0.1.0",
  32. )
  33. # 配置CORS
  34. app.add_middleware(
  35. CORSMiddleware,
  36. allow_origins=CORS_ORIGINS,
  37. allow_credentials=True,
  38. allow_methods=["*"],
  39. allow_headers=["*"],
  40. )
  41. # 注册路由
  42. app.include_router(research_router)
  43. # 健康检查端点
  44. @app.get("/health")
  45. async def health_check():
  46. return {"status": "healthy", "version": "0.1.0"}
  47. # API信息端点
  48. @app.get("/")
  49. async def root():
  50. return {
  51. "name": "LightStar API",
  52. "description": "文献启明星 - 科研调研智能体API服务",
  53. "version": "0.1.0",
  54. "endpoints": [
  55. "/api/research/process",
  56. "/api/research/extract-keywords",
  57. "/api/research/search-papers",
  58. "/api/research/cluster-papers",
  59. "/api/research/generate-report"
  60. ]
  61. }
  62. # 启动服务器
  63. if __name__ == "__main__":
  64. logger.info("Starting LightStar API server")
  65. uvicorn.run("main:app", host="0.0.0.0", port=8000, reload=True)