modified index.js and snakeapi

This commit is contained in:
yoshi 2025-05-02 00:12:05 -07:00
parent 5419e3f479
commit e64478b9bc
5 changed files with 86127 additions and 105 deletions

View file

@ -1,64 +1,55 @@
import os
import logging
import typing
import importlib.util
from flask import Flask, request, jsonify
from flask import Flask, request, jsonify, Response
from flask_cors import CORS
notebook_path = os.environ.get("NOTEBOOK_PATH")
if not notebook_path or not os.path.isfile(notebook_path):
raise RuntimeError(f"Notebook module not found: {notebook_path}")
logging.basicConfig(level=logging.INFO)
spec = importlib.util.spec_from_file_location("notebook_module", notebook_path)
NOTEBOOK_PATH = os.environ.get("NOTEBOOK_PATH")
if not NOTEBOOK_PATH or not os.path.isfile(NOTEBOOK_PATH):
raise RuntimeError(f"NOTEBOOK_PATH not set or file not found: {NOTEBOOK_PATH}")
# Determine .ipynb path
notebook_ipynb_path = NOTEBOOK_PATH[:-3] + ".ipynb"
spec = importlib.util.spec_from_file_location("notebook_module", NOTEBOOK_PATH)
notebook_module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(notebook_module)
handlers = {
"info": notebook_module.info,
"start": notebook_module.start,
"move": notebook_module.move,
"end": notebook_module.end,
}
app = Flask(__name__)
CORS(app)
def run_server(handlers: typing.Dict):
app = Flask(__name__)
CORS(app)
@app.route("/health", methods=["GET"])
def health():
return jsonify({"status": "ok"}), 200
@app.get("/")
def on_info():
return handlers["info"]()
@app.route("/", methods=["GET"])
def info():
return jsonify(notebook_module.info())
@app.post("/start")
def on_start():
game_state = request.get_json()
handlers["start"](game_state)
return "ok"
@app.route("/start", methods=["POST"])
def start():
data = request.get_json()
return jsonify(notebook_module.start(data))
@app.post("/move")
def on_move():
game_state = request.get_json()
return handlers["move"](game_state)
@app.route("/move", methods=["POST"])
def move():
data = request.get_json()
return jsonify(notebook_module.move(data))
@app.post("/end")
def on_end():
end_game = request.get_json()
handlers["end"](end_game)
return "ok"
@app.route("/end", methods=["POST"])
def end():
data = request.get_json()
return jsonify(notebook_module.end(data))
@app.get("/notebook")
def get_notebook():
with open(notebook_path.replace('.py', '.ipynb'), "r", encoding="utf-8") as f:
return f.read(), 200, {"Content-Type": "application/json"}
@app.get("/notebook/path")
def get_notebook_path():
return jsonify({"path": notebook_path})
host = "0.0.0.0"
port = int(os.environ.get("PORT", "3006"))
logging.getLogger("werkzeug").setLevel(logging.ERROR)
app.run(host=host, port=port)
@app.route("/notebook", methods=["GET"])
def get_notebook():
if os.path.isfile(notebook_ipynb_path):
with open(notebook_ipynb_path, "r", encoding="utf-8") as f:
return Response(f.read(), mimetype="application/json")
return jsonify({"error": "notebook not found"}), 404
if __name__ == "__main__":
run_server(handlers)
port = int(os.environ.get("PORT", 8000))
app.run(host="0.0.0.0", port=port)