123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186 |
- """
- System API Module
- 提供系统管理相关的API接口
- """
- from flask import jsonify, request
- from app.api.system import bp
- from app.models.result import success, failed
- import logging
- from app.core.system import (
- check_neo4j_connection,
- check_system_health,
- get_system_info,
- get_system_config,
- validate_config,
- register_user,
- login_user,
- get_user_by_username
- )
- logger = logging.getLogger("app")
- # 健康检查接口
- @bp.route('/health', methods=['GET'])
- def health_check():
- """
- 系统健康状态检查
- 检查关键依赖组件的连接状态
-
- Returns:
- JSON: 系统健康状态信息
- """
- try:
- # 获取系统健康状态
- status = check_system_health()
- return jsonify(success(status))
- except Exception as e:
- logger.error(f"健康检查失败: {str(e)}")
- return jsonify(failed(str(e)))
- # 系统配置信息
- @bp.route('/config', methods=['GET'])
- def system_config():
- """
- 获取系统配置信息
- 返回非敏感的系统配置项
-
- Returns:
- JSON: 系统配置信息
- """
- try:
- # 获取系统配置信息
- config_info = get_system_config()
- return jsonify(success(config_info))
- except Exception as e:
- logger.error(f"获取系统配置失败: {str(e)}")
- return jsonify(failed(str(e)))
- # 系统信息接口
- @bp.route('/info', methods=['GET'])
- def system_info():
- """
- 获取系统运行环境信息
- 包括操作系统、Python版本、资源使用情况等
-
- Returns:
- JSON: 系统运行环境详细信息
- """
- try:
- # 获取系统详细信息
- info = get_system_info()
- return jsonify(success(info))
- except Exception as e:
- logger.error(f"获取系统信息失败: {str(e)}")
- return jsonify(failed(str(e)))
- # 配置验证接口
- @bp.route('/config/validate', methods=['GET'])
- def config_validate():
- """
- 验证系统配置的有效性
- 检查必要的配置项是否存在且有效
-
- Returns:
- JSON: 配置验证结果
- """
- try:
- is_valid, errors = validate_config()
- result = {
- "valid": is_valid,
- "errors": errors
- }
- return jsonify(success(result))
- except Exception as e:
- logger.error(f"配置验证失败: {str(e)}")
- return jsonify(failed(str(e)))
- # 用户注册接口
- @bp.route('/auth/register', methods=['POST'])
- def user_register():
- """
- 用户注册
-
- 请求参数:
- username: 用户名
- password: 密码
-
- Returns:
- JSON: 注册结果
- """
- try:
- # 获取请求参数
- data = request.json
- username = data.get('username')
- password = data.get('password')
-
- # 参数验证
- if not username or not password:
- return jsonify(failed("用户名和密码不能为空", code=400))
-
- # 注册用户
- success_flag, message = register_user(username, password)
-
- if success_flag:
- return jsonify(success(message="注册成功"))
- else:
- return jsonify(failed(message, code=400))
- except Exception as e:
- logger.error(f"用户注册失败: {str(e)}")
- return jsonify(failed(str(e)))
- # 用户登录接口
- @bp.route('/auth/login', methods=['POST'])
- def user_login():
- """
- 用户登录
-
- 请求参数:
- username: 用户名
- password: 密码
-
- Returns:
- JSON: 登录结果,包含用户信息
- """
- try:
- # 获取请求参数
- data = request.json
- username = data.get('username')
- password = data.get('password')
-
- # 参数验证
- if not username or not password:
- return jsonify(failed("用户名和密码不能为空", code=400))
-
- # 登录验证
- success_flag, result = login_user(username, password)
-
- if success_flag:
- return jsonify(success(result, "登录成功"))
- else:
- return jsonify(failed(result, code=401))
- except Exception as e:
- logger.error(f"用户登录失败: {str(e)}")
- return jsonify(failed(str(e)))
- # 获取用户信息接口
- @bp.route('/auth/user/<username>', methods=['GET'])
- def get_user(username):
- """
- 获取用户信息
-
- Args:
- username: 用户名
-
- Returns:
- JSON: 用户信息
- """
- try:
- user = get_user_by_username(username)
- if user:
- return jsonify(success(user))
- else:
- return jsonify(failed("用户不存在", code=404))
- except Exception as e:
- logger.error(f"获取用户信息失败: {str(e)}")
- return jsonify(failed(str(e)))
|