modified index.js and snakeapi
This commit is contained in:
parent
5419e3f479
commit
e64478b9bc
5 changed files with 86127 additions and 105 deletions
|
|
@ -1,15 +1,28 @@
|
|||
FROM python:3.11-slim
|
||||
WORKDIR /app
|
||||
|
||||
RUN pip install --no-cache-dir jupyter flask awscli flask_cors nbconvert nbformat
|
||||
# disable Python output buffering
|
||||
ENV PYTHONUNBUFFERED=1
|
||||
# wrapper server port
|
||||
ENV PORT=8000
|
||||
# notebook server port
|
||||
ENV NOTEBOOK_PORT=3006
|
||||
|
||||
RUN pip install --no-cache-dir \
|
||||
flask \
|
||||
flask-cors \
|
||||
awscli \
|
||||
jupyter \
|
||||
nbconvert \
|
||||
nbformat \
|
||||
requests
|
||||
|
||||
COPY entrypoint.sh .
|
||||
COPY notebooks ./notebooks
|
||||
COPY snakeapi_server.py .
|
||||
COPY notebooks ./notebooks
|
||||
|
||||
RUN chmod +x entrypoint.sh
|
||||
|
||||
ENV PORT=3006
|
||||
EXPOSE 3006
|
||||
EXPOSE ${PORT} ${NOTEBOOK_PORT}
|
||||
|
||||
CMD ["./entrypoint.sh"]
|
||||
|
|
|
|||
|
|
@ -1,17 +1,29 @@
|
|||
#!/usr/bin/env bash
|
||||
set -eux
|
||||
|
||||
NOTEBOOK_DIR="notebooks"
|
||||
mkdir -p "${NOTEBOOK_DIR}"
|
||||
|
||||
# fetch latest notebook via prefix sync
|
||||
# sync notebooks from S3
|
||||
aws --endpoint-url "$AWS_ENDPOINT_URL_S3" --region "$AWS_REGION" \
|
||||
s3 sync "s3://$BUCKET_NAME/$INSTANCE_PREFIX/notebooks/" "${NOTEBOOK_DIR}/"
|
||||
s3 sync "s3://$COMMON_BUCKET/$INSTANCE_PREFIX/notebooks/" "${NOTEBOOK_DIR}/"
|
||||
|
||||
# pick latest .ipynb and normalize name
|
||||
latest_ipynb=$(ls -t "${NOTEBOOK_DIR}"/*.ipynb | head -1)
|
||||
if [ -n "$latest_ipynb" ]; then
|
||||
mv "$latest_ipynb" "${NOTEBOOK_DIR}/notebook.ipynb"
|
||||
jupyter nbconvert --to script "${NOTEBOOK_DIR}/notebook.ipynb" --output "${NOTEBOOK_DIR}/notebook.py"
|
||||
export NOTEBOOK_PATH="$(pwd)/${NOTEBOOK_DIR}/notebook.py"
|
||||
fi
|
||||
# pick latest notebook
|
||||
latest_ipynb=$(ls -t "${NOTEBOOK_DIR}"/*.ipynb | head -n1)
|
||||
cp "$latest_ipynb" "${NOTEBOOK_DIR}/notebook.ipynb"
|
||||
|
||||
# convert notebook to script
|
||||
jupyter nbconvert --to script "${NOTEBOOK_DIR}/notebook.ipynb" \
|
||||
--output notebook --output-dir "${NOTEBOOK_DIR}"
|
||||
|
||||
# remove IPython magics only
|
||||
sed -i '/get_ipython()/d' "${NOTEBOOK_DIR}/notebook.py"
|
||||
sed -i '/^!/d' "${NOTEBOOK_DIR}/notebook.py"
|
||||
sed -i '/^%/d' "${NOTEBOOK_DIR}/notebook.py"
|
||||
|
||||
# remove standalone Flask run calls
|
||||
sed -i '/app\.run(/d' "${NOTEBOOK_DIR}/notebook.py"
|
||||
|
||||
# export path and launch server
|
||||
export NOTEBOOK_PATH="$(pwd)/${NOTEBOOK_DIR}/notebook.py"
|
||||
exec python snakeapi_server.py
|
||||
|
|
|
|||
File diff suppressed because it is too large
Load diff
|
|
@ -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)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue