56 lines
1.9 KiB
Python
56 lines
1.9 KiB
Python
"""
|
||
卖相AI - Flask 后端
|
||
按功能拆分为蓝图:认证(auth)、主页面(main)、管理员(admin)、图片(image)、品牌(brand)
|
||
"""
|
||
import os
|
||
import secrets
|
||
from datetime import timedelta
|
||
|
||
from flask import Flask
|
||
from flask_cors import CORS
|
||
|
||
from app_common import init_db, BASE_DIR
|
||
from blueprints.auth import auth_bp
|
||
from blueprints.main import main_bp
|
||
from blueprints.admin import admin_bp
|
||
from blueprints.image import image_bp
|
||
from blueprints.brand import brand_bp
|
||
from blueprints.communication import communication_bp
|
||
def create_app():
|
||
app = Flask(__name__, template_folder=BASE_DIR, static_folder=BASE_DIR)
|
||
frontend_origin = os.environ.get('FRONTEND_ORIGIN', '*')
|
||
cors_origins = frontend_origin if frontend_origin != '*' else '*'
|
||
CORS(
|
||
app,
|
||
supports_credentials=True,
|
||
resources={r"/api/*": {"origins": cors_origins}, r"/login": {"origins": cors_origins}},
|
||
)
|
||
# 生产环境必须通过环境变量注入固定 SECRET_KEY;否则服务重启会使旧会话失效。
|
||
app.secret_key = os.environ.get('SECRET_KEY', 'dev-secret-key-change-me')
|
||
app.config['PERMANENT_SESSION_LIFETIME'] = timedelta(days=7)
|
||
app.config['SESSION_COOKIE_HTTPONLY'] = True
|
||
app.config['SESSION_COOKIE_SAMESITE'] = os.environ.get('SESSION_COOKIE_SAMESITE', 'Lax')
|
||
app.config['SESSION_COOKIE_SECURE'] = os.environ.get('SESSION_COOKIE_SECURE', '0') == '1'
|
||
|
||
# 注册蓝图(不设 url_prefix,保持原有 URL 路径不变,前端无需改动)
|
||
app.register_blueprint(auth_bp)
|
||
app.register_blueprint(main_bp)
|
||
app.register_blueprint(admin_bp)
|
||
app.register_blueprint(image_bp)
|
||
app.register_blueprint(brand_bp)
|
||
app.register_blueprint(communication_bp)
|
||
|
||
return app
|
||
|
||
|
||
app = create_app()
|
||
|
||
|
||
def run_app(host='127.0.0.1', port=5123):
|
||
# init_db()
|
||
app.run(host=host, port=port, threaded=True, use_reloader=False)
|
||
|
||
|
||
if __name__ == '__main__':
|
||
run_app()
|