test_server.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #!/usr/bin/env python3
  2. """
  3. 测试服务器 - 最小化版本
  4. """
  5. import sys
  6. import os
  7. # 添加src目录到Python路径
  8. sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'src'))
  9. # 加载环境变量
  10. from dotenv import load_dotenv
  11. load_dotenv()
  12. from fastapi import FastAPI
  13. from fastapi.middleware.cors import CORSMiddleware
  14. # 创建FastAPI应用
  15. app = FastAPI(
  16. title="SSO认证中心",
  17. version="1.0.0",
  18. description="OAuth2单点登录认证中心",
  19. docs_url="/docs",
  20. redoc_url="/redoc"
  21. )
  22. # 配置CORS
  23. app.add_middleware(
  24. CORSMiddleware,
  25. allow_origins=["*"],
  26. allow_credentials=True,
  27. allow_methods=["*"],
  28. allow_headers=["*"],
  29. )
  30. @app.get("/")
  31. async def root():
  32. """根路径"""
  33. return {
  34. "message": "SSO认证中心测试服务器",
  35. "version": "1.0.0",
  36. "status": "running",
  37. "docs": "/docs"
  38. }
  39. @app.get("/health")
  40. async def health_check():
  41. """健康检查"""
  42. return {
  43. "status": "healthy",
  44. "message": "服务正常运行"
  45. }
  46. @app.get("/test")
  47. async def test_endpoint():
  48. """测试端点"""
  49. return {
  50. "message": "测试成功",
  51. "data": {
  52. "server": "FastAPI",
  53. "python_version": sys.version,
  54. "working_directory": os.getcwd()
  55. }
  56. }
  57. if __name__ == "__main__":
  58. import uvicorn
  59. print("启动测试服务器...")
  60. print("访问地址: http://localhost:8000")
  61. print("API文档: http://localhost:8000/docs")
  62. print("按 Ctrl+C 停止服务器")
  63. uvicorn.run(
  64. app,
  65. host="0.0.0.0",
  66. port=8000,
  67. log_level="info"
  68. )