feat: first functioning code
This commit is contained in:
56
app.py
Normal file
56
app.py
Normal file
@@ -0,0 +1,56 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
from pathlib import Path
|
||||
|
||||
from flask import Flask, jsonify, request, send_from_directory
|
||||
|
||||
from compiler import CompileError, compile_graph
|
||||
|
||||
|
||||
def create_app() -> Flask:
|
||||
app = Flask(
|
||||
__name__,
|
||||
static_folder="static",
|
||||
static_url_path="/",
|
||||
)
|
||||
|
||||
@app.get("/")
|
||||
def spa_index():
|
||||
static_dir = Path(app.static_folder or "static")
|
||||
return send_from_directory(static_dir, "index.html")
|
||||
|
||||
@app.get("/<path:path>")
|
||||
def spa_assets(path: str):
|
||||
static_dir = Path(app.static_folder or "static")
|
||||
file_path = static_dir / path
|
||||
if file_path.exists() and file_path.is_file():
|
||||
return send_from_directory(static_dir, path)
|
||||
return send_from_directory(static_dir, "index.html")
|
||||
|
||||
@app.post("/api/compile")
|
||||
def api_compile():
|
||||
payload = request.get_json(silent=True)
|
||||
if not isinstance(payload, dict):
|
||||
return jsonify({"ok": False, "error": "request body must be a JSON object"}), 400
|
||||
|
||||
graph = payload.get("graph")
|
||||
if not isinstance(graph, dict):
|
||||
return jsonify({"ok": False, "error": "missing 'graph' object"}), 400
|
||||
|
||||
try:
|
||||
yml = compile_graph(graph)
|
||||
except CompileError as e:
|
||||
return jsonify({"ok": False, "error": str(e)}), 400
|
||||
except Exception:
|
||||
return jsonify({"ok": False, "error": "internal error"}), 500
|
||||
|
||||
return jsonify({"ok": True, "yaml": yml})
|
||||
|
||||
return app
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
port = int(os.environ.get("PORT", "7860"))
|
||||
app = create_app()
|
||||
app.run(host="0.0.0.0", port=port, debug=True)
|
||||
Reference in New Issue
Block a user