test_server_8001.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. #!/usr/bin/env python3
  2. """
  3. 测试服务器 - 使用8001端口
  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. "port": 8001,
  38. "docs": "/docs"
  39. }
  40. @app.get("/health")
  41. async def health_check():
  42. """健康检查"""
  43. return {
  44. "status": "healthy",
  45. "message": "服务正常运行",
  46. "port": 8001
  47. }
  48. @app.get("/test")
  49. async def test_endpoint():
  50. """测试端点"""
  51. return {
  52. "message": "测试成功",
  53. "port": 8001,
  54. "data": {
  55. "server": "FastAPI",
  56. "python_version": sys.version,
  57. "working_directory": os.getcwd()
  58. }
  59. }
  60. if __name__ == "__main__":
  61. import uvicorn
  62. print("启动测试服务器...")
  63. print("访问地址: http://localhost:8001")
  64. print("API文档: http://localhost:8001/docs")
  65. print("按 Ctrl+C 停止服务器")
  66. uvicorn.run(
  67. app,
  68. host="0.0.0.0",
  69. port=8001,
  70. log_level="info"
  71. )