96 lines
3.2 KiB
Python
96 lines
3.2 KiB
Python
"""
|
|
ParamHub - 参数百科
|
|
AI大模型与硬件参数速查平台
|
|
v1.8.0 - 模块化重构 + 后台登录认证
|
|
"""
|
|
from flask import Flask, request, jsonify, session, redirect, url_for
|
|
from flask_cors import CORS
|
|
from datetime import timedelta
|
|
|
|
from config import SECRET_KEY
|
|
from utils import load_config
|
|
|
|
# ─── Flask 应用创建 ────────────────────────────────────────────
|
|
|
|
app = Flask(__name__, static_folder='static', static_url_path='/static')
|
|
CORS(app)
|
|
|
|
app.secret_key = SECRET_KEY
|
|
app.config['PERMANENT_SESSION_LIFETIME'] = timedelta(hours=24)
|
|
|
|
# ─── 全局认证拦截 ──────────────────────────────────────────────
|
|
|
|
@app.before_request
|
|
def check_auth():
|
|
"""所有 POST/PUT/DELETE 请求需要登录(登录接口和阅读计数除外)"""
|
|
if request.method in ('POST', 'PUT', 'DELETE'):
|
|
# 登录接口本身不需要认证
|
|
if request.path == '/login':
|
|
return None
|
|
# 阅读计数公开访问
|
|
if request.path.endswith('/view'):
|
|
return None
|
|
# 检查 session
|
|
if not session.get('authenticated'):
|
|
if request.path.startswith('/api/'):
|
|
return jsonify({'error': '未登录,请先登录', 'redirect': '/login'}), 401
|
|
return redirect(url_for('auth.login_page'))
|
|
return None
|
|
|
|
|
|
# ─── 注册蓝图 ──────────────────────────────────────────────────
|
|
|
|
from auth import auth_bp
|
|
app.register_blueprint(auth_bp)
|
|
|
|
from modules.routes.pages import pages_bp
|
|
app.register_blueprint(pages_bp)
|
|
|
|
from modules.routes.api_models import models_bp
|
|
app.register_blueprint(models_bp)
|
|
|
|
from modules.routes.api_gpus import gpus_bp
|
|
app.register_blueprint(gpus_bp)
|
|
|
|
from modules.routes.api_cpus import cpus_bp
|
|
app.register_blueprint(cpus_bp)
|
|
|
|
from modules.routes.api_categories import cat_bp
|
|
app.register_blueprint(cat_bp)
|
|
|
|
from modules.routes.api_knowledge import knowledge_bp
|
|
app.register_blueprint(knowledge_bp)
|
|
|
|
from modules.routes.api_items import items_bp
|
|
app.register_blueprint(items_bp)
|
|
|
|
from modules.routes.api_smart import smart_bp
|
|
app.register_blueprint(smart_bp)
|
|
|
|
from modules.routes.api_search import search_bp
|
|
app.register_blueprint(search_bp)
|
|
|
|
from modules.routes.api_config import config_bp
|
|
app.register_blueprint(config_bp)
|
|
|
|
from modules.routes.api_upload import upload_bp
|
|
app.register_blueprint(upload_bp)
|
|
|
|
from modules.routes.api_pin import pin_bp
|
|
app.register_blueprint(pin_bp)
|
|
|
|
|
|
# ─── 启动 ──────────────────────────────────────────────────────
|
|
|
|
if __name__ == '__main__':
|
|
print("=" * 50)
|
|
print("ParamHub - 参数百科 v1.8.0")
|
|
print("=" * 50)
|
|
print("模块化重构 + 后台登录认证")
|
|
print(f"访问地址: http://localhost:16031")
|
|
print(f"后台管理: http://localhost:16031/admin")
|
|
print(f"默认密码: admin123 (可在 config.json 中修改)")
|
|
print("=" * 50)
|
|
|
|
app.run(host='0.0.0.0', port=16031, debug=False)
|