- server.py after_request:HTML 响应删掉 ETag/Last-Modified 校验头(之前仅 no-store 不够,浏览器带 If-None-Match 仍收 304 用旧 JS,导致运行按钮/渲染逻辑是旧的、点击无反应);改用 headers.pop 避免 Werkzeug Headers 无 discard 方法导致的 500 - 顺带 touch 全部模块 index.html 刷新 mtime,使已缓存的旧 ETag 失效,刷新即拉新页
192 lines
8.3 KiB
Python
192 lines
8.3 KiB
Python
"""尚数网算子集市 · 统一后端入口(23 个算子模块,含合规检测 / 场景落地 / 零知识证明面板)。
|
||
|
||
目录结构::
|
||
|
||
demo/
|
||
├── server.py ← 本文件
|
||
├── index.html ← 导航首页
|
||
├── _shared/demo.css ← 子模块共享 CSS
|
||
├── psi/ ← L1 · 完整 7 阶段动画
|
||
│ ├── app.py / index.html / PROTOCOL.md / requirements.txt / screenshots/
|
||
├── scql/ ← L1 · 安全 SQL 5 阶段
|
||
├── pir/ ← L1 · 1-out-of-N 隐匿查询
|
||
├── vertical_lr/ ← L1 · 纵向 LR(密文 vs 明文)
|
||
├── horizontal_fedavg/ ← L2 · 横向 FedAvg + 两种安全聚合
|
||
├── dp_fedavg/ ← L2 · 差分隐私 FedAvg
|
||
├── secure_mlp/ ← L2 · 密文 MLP
|
||
├── hetero_binning/ ← L2 · 异构分箱 + WOE/IV
|
||
├── policy_audit/ ← L2 · 出域策略守卫 + 审计
|
||
├── secure_compare/ ← L2 · 密文 lt/gt/eq/select/max/argmax
|
||
├── secure_stats/ ← L2 · secure_mean/var/pearson/correlation
|
||
├── secure_metrics/ ← L2 · 密文 AUC/ACC/log_loss/MSE
|
||
├── dp_primitives/ ← L2 · clip_l2 + 高斯/拉普拉斯 + RDP 账本
|
||
├── aggregator_chain/ ← L3 · TFF 风格可插拔聚合链
|
||
├── vframe_hframe/ ← L3 · 纵向/横向分区框架
|
||
├── scql_explain/ ← L3 · SCQL 查询计划 explain()
|
||
├── ole_paillier/ ← L3 · OLE / Paillier 加法同态
|
||
├── hello/ ← L3 · 三方密文算术入门
|
||
├── dry_run/ ← L3 · 双阶段执行
|
||
└── k8s_two_party/ ← L4 · 部署指引
|
||
|
||
访问::
|
||
|
||
http://<host>:18850/ 导航首页
|
||
http://<host>:18850/psi/ PSI 演示
|
||
http://<host>:18850/scql/ SCQL 演示
|
||
... (每个子模块 /<name>/ 暴露)
|
||
http://<host>:18850/psi/api/psi PSI 接口
|
||
... (每个子模块 /<name>/api/* 暴露)
|
||
http://<host>:18850/_shared/demo.css 共享 CSS
|
||
"""
|
||
|
||
from __future__ import annotations
|
||
|
||
import argparse
|
||
import sys
|
||
from functools import partial
|
||
from pathlib import Path
|
||
|
||
# ── 让所有子模块能 import,让 nax 子包能被各子模块 import ─────
|
||
HERE = Path(__file__).resolve().parent
|
||
sys.path.insert(0, str(HERE)) # from <name>.app import ...
|
||
sys.path.insert(0, str(HERE.parent / "nax")) # from nax.crypto.psi import ...
|
||
|
||
from flask import Flask, abort, jsonify, send_from_directory # noqa: E402
|
||
|
||
# ── 20 个算子 Blueprint ───────────────────────────
|
||
from psi.app import psi_bp # noqa: E402
|
||
from scql.app import scql_bp # noqa: E402
|
||
from pir.app import pir_bp # noqa: E402
|
||
from vertical_lr.app import vlr_bp # noqa: E402
|
||
from horizontal_fedavg.app import hfa_bp # noqa: E402
|
||
from dp_fedavg.app import dpfa_bp # noqa: E402
|
||
from secure_mlp.app import mlp_bp # noqa: E402
|
||
from hetero_binning.app import hb_bp # noqa: E402
|
||
from policy_audit.app import pa_bp # noqa: E402
|
||
from secure_compare.app import cmp_bp # noqa: E402
|
||
from secure_stats.app import stats_bp # noqa: E402
|
||
from secure_metrics.app import m_bp # noqa: E402
|
||
from dp_primitives.app import dp_bp # noqa: E402
|
||
from aggregator_chain.app import chain_bp # noqa: E402
|
||
from vframe_hframe.app import vf_bp # noqa: E402
|
||
from scql_explain.app import exp_bp # noqa: E402
|
||
from ole_paillier.app import ole_bp # noqa: E402
|
||
from hello.app import hello_bp # noqa: E402
|
||
from dry_run.app import dry_bp # noqa: E402
|
||
from k8s_two_party.app import k8s_bp # noqa: E402
|
||
from security_audit.app import sa_bp # noqa: E402
|
||
from secure_scenario.app import ss_bp # noqa: E402
|
||
from zkp.app import zkp_bp # noqa: E402
|
||
|
||
|
||
# ──────────────────────────── Flask app ────────────────────────────
|
||
app = Flask(__name__, static_folder=str(HERE), static_url_path="/static")
|
||
|
||
|
||
@app.after_request
|
||
def _no_store_html(resp):
|
||
"""HTML 页面禁止缓存,避免浏览器长期缓存旧版算子页面(导致「运行按钮消失」)。
|
||
静态资源(css/js/图片)仍走默认缓存,不影响性能。"""
|
||
ct = resp.headers.get("Content-Type", "")
|
||
if "text/html" in ct:
|
||
resp.headers["Cache-Control"] = "no-store, no-cache, must-revalidate, max-age=0"
|
||
resp.headers["Pragma"] = "no-cache"
|
||
resp.headers["Expires"] = "0"
|
||
# 关键:删掉校验头,浏览器才无法发 If-None-Match/If-Modified-Since,
|
||
# 否则仍会收到 304 并继续用磁盘缓存的旧 JS(运行按钮/渲染逻辑就还是旧的)。
|
||
resp.headers.pop("ETag", None)
|
||
resp.headers.pop("Last-Modified", None)
|
||
return resp
|
||
|
||
|
||
# 长耗时 LLM 调用的异步任务轮询表(审计 / 方案生成 共用)
|
||
from _shared import jobs # noqa: E402
|
||
|
||
|
||
@app.route("/api/job/<job_id>")
|
||
def api_job(job_id):
|
||
"""前端轮询长耗时 LLM 任务进度与结果。"""
|
||
job = jobs.get(job_id)
|
||
if job is None:
|
||
return jsonify({"ok": False, "msg": "任务不存在或已过期(30 分钟 TTL)"}), 404
|
||
return jsonify(job)
|
||
|
||
# 20 个子模块
|
||
modules = {
|
||
"psi": psi_bp,
|
||
"scql": scql_bp,
|
||
"pir": pir_bp,
|
||
"vertical_lr": vlr_bp,
|
||
"horizontal_fedavg": hfa_bp,
|
||
"dp_fedavg": dpfa_bp,
|
||
"secure_mlp": mlp_bp,
|
||
"hetero_binning": hb_bp,
|
||
"policy_audit": pa_bp,
|
||
"secure_compare": cmp_bp,
|
||
"secure_stats": stats_bp,
|
||
"secure_metrics": m_bp,
|
||
"dp_primitives": dp_bp,
|
||
"aggregator_chain": chain_bp,
|
||
"vframe_hframe": vf_bp,
|
||
"scql_explain": exp_bp,
|
||
"ole_paillier": ole_bp,
|
||
"hello": hello_bp,
|
||
"dry_run": dry_bp,
|
||
"k8s_two_party": k8s_bp,
|
||
"security_audit": sa_bp,
|
||
"secure_scenario": ss_bp,
|
||
"zkp": zkp_bp,
|
||
}
|
||
for name, bp in modules.items():
|
||
app.register_blueprint(bp, url_prefix=f"/{name}")
|
||
|
||
|
||
# ── 入口 ─────────────────────────
|
||
@app.route("/")
|
||
def root():
|
||
return send_from_directory(HERE, "index.html")
|
||
|
||
|
||
def _make_submodule_page(name):
|
||
"""工厂:返回一个 closure,serve demo/<name>/index.html。"""
|
||
sub_dir = HERE / name
|
||
|
||
def view():
|
||
if not (sub_dir / "index.html").exists():
|
||
abort(404)
|
||
return send_from_directory(sub_dir, "index.html")
|
||
|
||
view.__name__ = f"submodule_page_{name}"
|
||
return view
|
||
|
||
|
||
# 为每个子模块显式注册 /<name>/ 与 /<name> 路由(Blueprint 的 /<name>/ 是子模块自己定义的 API/HTML 路径)
|
||
for name in modules:
|
||
view = _make_submodule_page(name)
|
||
app.add_url_rule(f"/{name}/", endpoint=f"page_{name}", view_func=view)
|
||
app.add_url_rule(f"/{name}", endpoint=f"page_{name}_root", view_func=view)
|
||
|
||
|
||
# 共享 CSS 静态
|
||
@app.route("/_shared/<path:filename>")
|
||
def shared_static(filename):
|
||
return send_from_directory(HERE / "_shared", filename)
|
||
|
||
|
||
# 入口点
|
||
def main():
|
||
p = argparse.ArgumentParser(description="尚数网算子集市 · 统一后端")
|
||
p.add_argument("--host", default="0.0.0.0")
|
||
p.add_argument("--port", type=int, default=18850)
|
||
a = p.parse_args()
|
||
|
||
print(f"[demo] 导航首页 : http://{a.host}:{a.port}/")
|
||
print(f"[demo] 23 个算子:")
|
||
for name in modules:
|
||
print(f"[demo] /{name:24s} http://{a.host}:{a.port}/{name}/")
|
||
app.run(host=a.host, port=a.port, threaded=True)
|
||
|
||
|
||
if __name__ == "__main__":
|
||
main()
|