54 lines
1.4 KiB
Python
54 lines
1.4 KiB
Python
import io
|
|
import os
|
|
|
|
from flask import Flask, jsonify, render_template, request, send_file
|
|
|
|
from handbook.generator import generate_handbook
|
|
|
|
app = Flask(__name__)
|
|
|
|
|
|
@app.route("/")
|
|
def index():
|
|
return render_template("index.html")
|
|
|
|
|
|
@app.route("/api/generate", methods=["POST"])
|
|
def api_generate():
|
|
data = request.get_json(silent=True) or {}
|
|
topic = (data.get("topic") or "").strip()
|
|
if not topic:
|
|
return jsonify({"error": "主题不能为空"}), 400
|
|
|
|
try:
|
|
markdown = generate_handbook(topic)
|
|
except Exception as exc: # noqa: BLE001
|
|
return jsonify({"error": f"生成失败: {exc}"}), 500
|
|
|
|
return jsonify({"topic": topic, "markdown": markdown})
|
|
|
|
|
|
@app.route("/api/export", methods=["POST"])
|
|
def api_export():
|
|
data = request.get_json(silent=True) or {}
|
|
topic = (data.get("topic") or "handbook").strip() or "handbook"
|
|
markdown = data.get("markdown") or ""
|
|
|
|
if not markdown:
|
|
return jsonify({"error": "内容不能为空"}), 400
|
|
|
|
buffer = io.BytesIO(markdown.encode("utf-8"))
|
|
buffer.seek(0)
|
|
safe_name = "".join(c for c in topic if c.isalnum() or c in "-_ ").strip() or "handbook"
|
|
return send_file(
|
|
buffer,
|
|
mimetype="text/markdown",
|
|
as_attachment=True,
|
|
download_name=f"{safe_name}-登记手册.md",
|
|
)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
port = int(os.environ.get("PORT", "5000"))
|
|
app.run(host="0.0.0.0", port=port, debug=True)
|