app.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. from flask import Flask, jsonify
  2. from flask_cors import CORS # 导入Flask-CORS
  3. from api.routes import register_routes
  4. def create_app():
  5. app = Flask(__name__)
  6. # 配置CORS - 核心修改部分
  7. CORS(app,
  8. origins=[
  9. 'http://localhost:9527',
  10. 'https://adminpre.bjzxtw.org.cn',
  11. 'http://adminpre.bjzxtw.org.cn',
  12. 'https://admin.bjzxtw.org.cn',
  13. 'http://admin.bjzxtw.org.cn'
  14. ], # 明确允许你的前端域名
  15. supports_credentials=True, # 允许携带Cookie等凭证
  16. methods=['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'], # 允许的HTTP方法
  17. allow_headers=['Content-Type', 'Authorization'], # 允许的请求头
  18. expose_headers=None, # 允许浏览器访问的响应头
  19. max_age=600) # 预检请求缓存时间(秒)
  20. # 路由检查
  21. @app.route('/health')
  22. def health():
  23. return jsonify({'status': 'healthy', 'service': 'aiserv'})
  24. # 添加根路由
  25. @app.route('/')
  26. def index():
  27. return jsonify({
  28. 'service': 'AIServ',
  29. 'version': '1.2',
  30. 'status': 'running'
  31. })
  32. # 注册其他路由
  33. register_routes(app)
  34. return app
  35. if __name__ == '__main__':
  36. app = create_app()
  37. print("🚀 小龙包服务启动...")
  38. print("📡 监听地址: 0.0.0.0:5000")
  39. print("🌐 外部访问: http://localhost:5000")
  40. print("🔓 CORS已启用,允许跨域访问")
  41. print(f"✅ 允许的源: https://apipre1.bjzxtw.org.cn")
  42. app.run(
  43. host='0.0.0.0',
  44. port=5000,
  45. debug=False,
  46. threaded=True
  47. )