Skip to content
This repository was archived by the owner on Dec 8, 2022. It is now read-only.

Commit 7a397e6

Browse files
committed
Docker image for evaluating JavaScript
1 parent d084f5e commit 7a397e6

File tree

3 files changed

+58
-0
lines changed

3 files changed

+58
-0
lines changed

docker/Dockerfile

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
FROM alpine:latest
2+
LABEL maintainer="Sam Hoffman <sam@codewizardshq.com>"
3+
4+
RUN apk --update add --no-cache python3 bash curl binutils libc-dev python3-dev gcc
5+
RUN pip3 install -U pip
6+
RUN pip3 install flask flask-cors pyduktape gunicorn
7+
8+
RUN adduser -D -H webapp
9+
RUN mkdir -p "/var/www/flaskapp"
10+
11+
# default Flask port
12+
EXPOSE 5000/tcp
13+
14+
USER webapp
15+
16+
COPY "start.sh" "/tmp/start.sh"
17+
COPY "app.py" "/var/www/flaskapp/app.py"
18+
19+
ENTRYPOINT [ "/tmp/start.sh" ]

docker/app.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import pyduktape
2+
from flask import Flask, jsonify, request
3+
from flask_cors import CORS
4+
5+
app = Flask(__name__)
6+
CORS(app)
7+
8+
9+
@app.route("/js/eval", methods=["POST"])
10+
def js_eval():
11+
"""Eval some JavaScript using pyduktape"""
12+
13+
if not request.is_json:
14+
return jsonify(error="payload must be JSON format"), 400
15+
16+
data = request.get_json()
17+
18+
try:
19+
code = data["code"]
20+
except KeyError:
21+
return jsonify(error="missing 'code' parameter"), 400
22+
23+
ctx = pyduktape.DuktapeContext()
24+
25+
output = ""
26+
error = ""
27+
28+
code += ";output"
29+
30+
try:
31+
output = ctx.eval_js(code)
32+
except Exception as e:
33+
error = str(e)
34+
35+
return jsonify(output=output, error=error)

docker/start.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/bin/sh
2+
set -e
3+
cd "/var/www/flaskapp/"
4+
gunicorn -b 0.0.0.0:5000 app:app

0 commit comments

Comments
 (0)