Skip to content

Commit 6cc31e9

Browse files
authored
Merge pull request #3 from yanokwa/switch-to-waitress
Switch to waitress to support chunked encoding
2 parents 402715a + 63a12c1 commit 6cc31e9

File tree

5 files changed

+57
-53
lines changed

5 files changed

+57
-53
lines changed

Dockerfile

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
FROM tiangolo/uwsgi-nginx-flask:python3.7-alpine3.8
1+
FROM python:3.8-alpine
22

33
COPY requirements.txt /tmp/
4-
RUN pip install --upgrade pip
54
RUN pip install --requirement /tmp/requirements.txt
65

76
RUN apk --update add openjdk8-jre-base
87

98
COPY ./app /app
9+
WORKDIR /app
10+
11+
CMD ["waitress-serve", "--port=80", "--call", "main:app"]

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ pyxform-http is a Flask-based web service that uses pyxform to convert a XLSForm
88
# Run locally
99
```
1010
pip install --requirement requirements.txt
11-
FLASK_APP=app/main.py FLASK_DEBUG=1 flask run
11+
FLASK_APP=app/main.py:app FLASK_DEBUG=1 flask run
1212
```
1313

1414
# Run in Docker
@@ -19,9 +19,9 @@ docker run --detach --name pyxform-http --publish 5000:80 pyxform-http
1919

2020
# Test forms
2121

22-
A form that converts successfully
22+
A form that converts successfully (with chunked encoding!)
2323
```
24-
curl --request POST --header "X-XlsForm-FormId-Fallback: pyxform-clean" --data-binary @test/pyxform-clean.xlsx http://127.0.0.1:5000/api/v1/convert
24+
curl --request POST --header "X-XlsForm-FormId-Fallback: pyxform-clean" --header 'Transfer-Encoding: chunked' --data-binary @test/pyxform-clean.xlsx http://127.0.0.1:5000/api/v1/convert
2525
```
2626

2727
A form that fails to convert and returns a pyxform error

app/main.py

Lines changed: 48 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -5,50 +5,54 @@
55
from flask import Flask, jsonify, request, escape
66
from pyxform import xls2xform
77

8-
app = Flask(__name__)
9-
logger = logging.getLogger(__name__)
10-
11-
12-
@app.route("/")
13-
def index():
14-
return "Welcome to the pyxform-http! Make a POST request to '/api/v1/convert' to convert an XLSForm to an ODK XForm."
15-
16-
17-
@app.route("/api/v1/convert", methods=["POST"])
18-
def post():
19-
20-
xlsform_formid_fallback = sanitize(request.headers.get("X-XlsForm-FormId-Fallback"))
21-
if xlsform_formid_fallback is None:
22-
xlsform_formid_fallback = "tmp"
23-
24-
with TemporaryDirectory() as temp_dir_name:
25-
try:
26-
with open(
27-
os.path.join(temp_dir_name, xlsform_formid_fallback + ".xml"), "w+"
28-
) as xform, open(
29-
os.path.join(temp_dir_name, xlsform_formid_fallback + ".xlsx"), "wb"
30-
) as xlsform:
31-
xlsform.write(request.get_data())
32-
convert_status = xls2xform.xls2xform_convert(
33-
xlsform_path=str(xlsform.name),
34-
xform_path=str(xform.name),
35-
validate=True,
36-
pretty_print=False,
37-
)
38-
39-
if convert_status:
40-
logger.warning(convert_status)
41-
42-
if os.path.isfile(xform.name):
43-
return response(
44-
status=200, result=xform.read(), warnings=convert_status
8+
9+
def app():
10+
app = Flask(__name__)
11+
logger = logging.getLogger(__name__)
12+
13+
@app.route("/")
14+
def index():
15+
return "Welcome to the pyxform-http! Make a POST request to '/api/v1/convert' to convert an XLSForm to an ODK XForm."
16+
17+
@app.route("/api/v1/convert", methods=["POST"])
18+
def post():
19+
20+
xlsform_formid_fallback = sanitize(
21+
request.headers.get("X-XlsForm-FormId-Fallback")
22+
)
23+
if xlsform_formid_fallback is None:
24+
xlsform_formid_fallback = "tmp"
25+
26+
with TemporaryDirectory() as temp_dir_name:
27+
try:
28+
with open(
29+
os.path.join(temp_dir_name, xlsform_formid_fallback + ".xml"), "w+"
30+
) as xform, open(
31+
os.path.join(temp_dir_name, xlsform_formid_fallback + ".xlsx"), "wb"
32+
) as xlsform:
33+
xlsform.write(request.get_data())
34+
convert_status = xls2xform.xls2xform_convert(
35+
xlsform_path=str(xlsform.name),
36+
xform_path=str(xform.name),
37+
validate=True,
38+
pretty_print=False,
4539
)
46-
else:
47-
return response(error=convert_status)
4840

49-
except Exception as e:
50-
logger.error(e)
51-
return response(error=str(e))
41+
if convert_status:
42+
logger.warning(convert_status)
43+
44+
if os.path.isfile(xform.name):
45+
return response(
46+
status=200, result=xform.read(), warnings=convert_status
47+
)
48+
else:
49+
return response(error=convert_status)
50+
51+
except Exception as e:
52+
logger.error(e)
53+
return response(error=str(e))
54+
55+
return app
5256

5357

5458
def sanitize(string):
@@ -60,5 +64,5 @@ def response(status=400, result=None, warnings=None, error=None):
6064

6165

6266
if __name__ == "__main__":
63-
# Only for debugging while developing
64-
app.run(host="0.0.0.0", debug=True, port=80)
67+
app = app()
68+
app.run()

app/uwsgi.ini

Lines changed: 0 additions & 3 deletions
This file was deleted.

requirements.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1-
Flask==1.1.0
1+
Flask==1.1.1
2+
waitress==1.2.1
23
pyxform==0.15.1

0 commit comments

Comments
 (0)