""" 主页面蓝图:首页、管理后台页、静态文件 """ import os from flask import Blueprint, redirect, url_for, send_file, session from utils.auth import login_required, admin_required, is_session_user_valid from utils.render import render_html main = Blueprint('main', __name__, url_prefix='') BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) STATIC_DIR = os.path.join(BASE_DIR, 'static') @main.route('/') def index(): if session.get('user_id') and is_session_user_valid(): return redirect(url_for('main.admin_page')) return redirect(url_for('auth.login')) @main.route('/admin') @login_required @admin_required def admin_page(): return render_html('admin.html') @main.route('/static/') def serve_static(filename): """提供 static 目录及子目录下的静态文件访问""" filepath = os.path.normpath(os.path.join(STATIC_DIR, filename)) static_abs = os.path.abspath(STATIC_DIR) file_abs = os.path.abspath(filepath) if not file_abs.startswith(static_abs) or not os.path.isfile(file_abs): return '', 404 return send_file(file_abs, as_attachment=False)