37 lines
1.0 KiB
Python
37 lines
1.0 KiB
Python
"""
|
|
主页面蓝图:首页、管理后台页、静态文件
|
|
"""
|
|
import os
|
|
from flask import Blueprint, redirect, url_for, send_file, session
|
|
from werkzeug.utils import safe_join
|
|
|
|
from utils.auth import login_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
|
|
def admin_page():
|
|
return render_html('admin.html')
|
|
|
|
|
|
@main.route('/static/<path:filename>')
|
|
def serve_static(filename):
|
|
"""提供 static 目录及子目录下的静态文件访问"""
|
|
filepath = safe_join(STATIC_DIR, filename)
|
|
if filepath is None or not os.path.isfile(filepath):
|
|
return '', 404
|
|
return send_file(filepath, as_attachment=False)
|