""" 卖相AI - Flask 后端 按功能拆分为蓝图:认证(auth)、主页面(main)、管理员API(admin_api)、版本(version) """ import os import secrets from datetime import timedelta from flask import Flask from flask_cors import CORS from utils.db import init_db from blueprints.auth import auth from blueprints.main import main from blueprints.admin_api import admin_api from blueprints.version import version_bp BASE_DIR = os.path.dirname(os.path.abspath(__file__)) app = Flask(__name__, template_folder=BASE_DIR, static_folder=BASE_DIR) # CORS配置:限制为可信域名 CORS(app, resources={ r"/api/*": { "origins": [ "http://localhost:*", "http://127.0.0.1:*", "https://*.aishufu.top", "http://*.aishufu.top" ], "methods": ["GET", "POST", "PUT", "DELETE", "OPTIONS"], "allow_headers": ["Authorization", "Content-Type", "X-Requested-With", "X-Device-Id"], "supports_credentials": True } }) app.secret_key = os.environ.get('SECRET_KEY', secrets.token_hex(32)) app.config['PERMANENT_SESSION_LIFETIME'] = timedelta(days=7) # 文件上传大小限制:2GB(数字人 ZIP 包等大文件) app.config['MAX_CONTENT_LENGTH'] = 2 * 1024 * 1024 * 1024 # 会话安全配置 app.config['SESSION_COOKIE_SECURE'] = os.environ.get('SESSION_COOKIE_SECURE', 'false').lower() == 'true' app.config['SESSION_COOKIE_HTTPONLY'] = True app.config['SESSION_COOKIE_SAMESITE'] = 'Lax' # 注册蓝图 app.register_blueprint(auth) app.register_blueprint(main) app.register_blueprint(admin_api) app.register_blueprint(version_bp) def run_app(host='0.0.0.0', port=15124): init_db() app.run(host=host, port=port, threaded=True, use_reloader=False) if __name__ == '__main__': run_app()