Skip to content

Commit

Permalink
analyze file and return result
Browse files Browse the repository at this point in the history
  • Loading branch information
dnlbauer committed Nov 27, 2022
1 parent 233c14e commit 9ca1f9b
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 7 deletions.
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,6 @@ WORKDIR /app
COPY ./requirements.txt .
RUN pip install -r requirements.txt

COPY app.py gunicorn.conf.py ./
COPY app.py gunicorn_conf.py ./

ENTRYPOINT ["gunicorn", "--conf", "gunicorn.conf.py", "--bind", "0.0.0.0:80", "app:app"]
ENTRYPOINT ["gunicorn", "--conf", "gunicorn_conf.py", "--bind", "0.0.0.0:80", "app:app"]
54 changes: 49 additions & 5 deletions app.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,56 @@
from flask import Flask
from flask import Flask, request, flash, Response
from werkzeug.utils import secure_filename
import logging
from os import path, remove
import tempfile
import subprocess as sp

app = Flask(__name__)


@app.route('/', methods=["GET"])
def hello_world(): # put application's code here
return 'Hello World!'
def get_format(req, fallback=("application/json", "json")):
accept = req.headers.get("accept").lower()
if accept == "application/json":
return accept, "json"
elif accept == "application/xml":
return accept, "xml"
elif accept == "text/plain":
return accept, "txt"
else:
return fallback


@app.route('/analyze', methods=["POST"])
def analyze():
if "file" not in request.files or len(request.files["file"].filename) == 0:
flash("No file.")
return

# save file to tmp folder
file = request.files["file"]
file_path = path.join(tempfile.gettempdir(), secure_filename(file.filename))
app.logger.debug(f"saving file as {file_path}")
file.save(file_path)



mime, response_format = get_format(request)
cmd = ["java", "-jar", "/pdfact.jar", file_path, "--format", response_format]
app.logger.debug(f"running {' '.join(cmd)}")

try:
result = sp.check_output(cmd)
return Response(result, mimetype=mime)
except sp.CalledProcessError:
return "Something went wrong", 500
finally:
remove(file_path)

if __name__ == '__main__':
app.run(host="0.0.0.0", port=5000)
app.run()
else:
# use gunicorns loglevel
gunicorn_logger = logging.getLogger("gunicorn.error")
app.logger.handlers = gunicorn_logger.handlers
app.logger.setLevel(gunicorn_logger.level)
app.logger.warn(gunicorn_logger.level)
File renamed without changes.

0 comments on commit 9ca1f9b

Please sign in to comment.