routes.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. """
  2. System API Module
  3. 提供系统管理相关的API接口
  4. """
  5. from flask import jsonify, request
  6. from app.api.system import bp
  7. from app.models.result import success, failed
  8. import logging
  9. from app.core.system import (
  10. check_neo4j_connection,
  11. check_system_health,
  12. get_system_info,
  13. get_system_config,
  14. validate_config,
  15. register_user,
  16. login_user,
  17. get_user_by_username
  18. )
  19. logger = logging.getLogger("app")
  20. # 健康检查接口
  21. @bp.route('/health', methods=['GET'])
  22. def health_check():
  23. """
  24. 系统健康状态检查
  25. 检查关键依赖组件的连接状态
  26. Returns:
  27. JSON: 系统健康状态信息
  28. """
  29. try:
  30. # 获取系统健康状态
  31. status = check_system_health()
  32. return jsonify(success(status))
  33. except Exception as e:
  34. logger.error(f"健康检查失败: {str(e)}")
  35. return jsonify(failed(str(e)))
  36. # 系统配置信息
  37. @bp.route('/config', methods=['GET'])
  38. def system_config():
  39. """
  40. 获取系统配置信息
  41. 返回非敏感的系统配置项
  42. Returns:
  43. JSON: 系统配置信息
  44. """
  45. try:
  46. # 获取系统配置信息
  47. config_info = get_system_config()
  48. return jsonify(success(config_info))
  49. except Exception as e:
  50. logger.error(f"获取系统配置失败: {str(e)}")
  51. return jsonify(failed(str(e)))
  52. # 系统信息接口
  53. @bp.route('/info', methods=['GET'])
  54. def system_info():
  55. """
  56. 获取系统运行环境信息
  57. 包括操作系统、Python版本、资源使用情况等
  58. Returns:
  59. JSON: 系统运行环境详细信息
  60. """
  61. try:
  62. # 获取系统详细信息
  63. info = get_system_info()
  64. return jsonify(success(info))
  65. except Exception as e:
  66. logger.error(f"获取系统信息失败: {str(e)}")
  67. return jsonify(failed(str(e)))
  68. # 配置验证接口
  69. @bp.route('/config/validate', methods=['GET'])
  70. def config_validate():
  71. """
  72. 验证系统配置的有效性
  73. 检查必要的配置项是否存在且有效
  74. Returns:
  75. JSON: 配置验证结果
  76. """
  77. try:
  78. is_valid, errors = validate_config()
  79. result = {
  80. "valid": is_valid,
  81. "errors": errors
  82. }
  83. return jsonify(success(result))
  84. except Exception as e:
  85. logger.error(f"配置验证失败: {str(e)}")
  86. return jsonify(failed(str(e)))
  87. # 用户注册接口
  88. @bp.route('/auth/register', methods=['POST'])
  89. def user_register():
  90. """
  91. 用户注册
  92. 请求参数:
  93. username: 用户名
  94. password: 密码
  95. Returns:
  96. JSON: 注册结果
  97. """
  98. try:
  99. # 获取请求参数
  100. data = request.json
  101. username = data.get('username')
  102. password = data.get('password')
  103. # 参数验证
  104. if not username or not password:
  105. return jsonify(failed("用户名和密码不能为空", code=400))
  106. # 注册用户
  107. success_flag, message = register_user(username, password)
  108. if success_flag:
  109. return jsonify(success(message="注册成功"))
  110. else:
  111. return jsonify(failed(message, code=400))
  112. except Exception as e:
  113. logger.error(f"用户注册失败: {str(e)}")
  114. return jsonify(failed(str(e)))
  115. # 用户登录接口
  116. @bp.route('/auth/login', methods=['POST'])
  117. def user_login():
  118. """
  119. 用户登录
  120. 请求参数:
  121. username: 用户名
  122. password: 密码
  123. Returns:
  124. JSON: 登录结果,包含用户信息
  125. """
  126. try:
  127. # 获取请求参数
  128. data = request.json
  129. username = data.get('username')
  130. password = data.get('password')
  131. # 参数验证
  132. if not username or not password:
  133. return jsonify(failed("用户名和密码不能为空", code=400))
  134. # 登录验证
  135. success_flag, result = login_user(username, password)
  136. if success_flag:
  137. return jsonify(success(result, "登录成功"))
  138. else:
  139. return jsonify(failed(result, code=401))
  140. except Exception as e:
  141. logger.error(f"用户登录失败: {str(e)}")
  142. return jsonify(failed(str(e)))
  143. # 获取用户信息接口
  144. @bp.route('/auth/user/<username>', methods=['GET'])
  145. def get_user(username):
  146. """
  147. 获取用户信息
  148. Args:
  149. username: 用户名
  150. Returns:
  151. JSON: 用户信息
  152. """
  153. try:
  154. user = get_user_by_username(username)
  155. if user:
  156. return jsonify(success(user))
  157. else:
  158. return jsonify(failed("用户不存在", code=404))
  159. except Exception as e:
  160. logger.error(f"获取用户信息失败: {str(e)}")
  161. return jsonify(failed(str(e)))