app.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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','userurl'], # 允许的请求头
  18. allow_headers='*', # 允许的请求头
  19. expose_headers=None, # 允许浏览器访问的响应头
  20. max_age=600) # 预检请求缓存时间(秒)
  21. # 路由检查
  22. @app.route('/health')
  23. def health():
  24. return jsonify({'status': 'healthy', 'service': 'aiserv'})
  25. # 添加根路由
  26. @app.route('/')
  27. def index():
  28. return jsonify({
  29. 'service': 'AIServ',
  30. 'version': '1.2',
  31. 'status': 'running'
  32. })
  33. # 注册其他路由
  34. register_routes(app)
  35. return app
  36. if __name__ == '__main__':
  37. app = create_app()
  38. print("🚀 小龙包服务启动...")
  39. print("📡 监听地址: 0.0.0.0:5000")
  40. print("🌐 外部访问: http://localhost:5000")
  41. print("🔓 CORS已启用,允许跨域访问")
  42. print(f"✅ 允许的源: https://apipre1.bjzxtw.org.cn")
  43. app.run(
  44. host='0.0.0.0',
  45. port=5000,
  46. debug=False,
  47. threaded=True
  48. )