From c1f9c1f354ac9b2b8b5a279c11e30683bb250b51 Mon Sep 17 00:00:00 2001 From: Jakob Langdal Date: Tue, 20 Apr 2021 22:47:59 +0200 Subject: [PATCH] Add production runtime --- Dockerfile | 2 ++ README.md | 23 ++++++++++++++++++++++- optimizerapi/optimizer.py | 2 +- optimizerapi/server.py | 11 ++++++++++- requirements.txt | 3 ++- 5 files changed, 37 insertions(+), 4 deletions(-) diff --git a/Dockerfile b/Dockerfile index 88eb14e..1d70ac1 100644 --- a/Dockerfile +++ b/Dockerfile @@ -22,6 +22,8 @@ USER user COPY optimizerapi/ /code +ENV FLASK_ENV=production + ENV PATH=/opt/venv/bin:${PATH} CMD [ "python", "./server.py" ] \ No newline at end of file diff --git a/README.md b/README.md index ea51886..af771c2 100644 --- a/README.md +++ b/README.md @@ -16,9 +16,30 @@ Alternatively the project can be build and run with the following commands: python optimizerapi/server.py Now open [http://localhost:9090/v1.0/ui/](http://localhost:9090/v1.0/ui/) in a browser to explore the API through Swagger UI - # Running tests Unit tetsts are located in the "tests" folder and can be run witht the following command python -m pytest + +# Building docker container + + docker build -t process-optimizer-api . +# Obtain encryption key + +Run server once and extract a fresh encryption key from the logs. + + python optimizerapi/server.py + +or using docker + + docker run --rm -it process-optimizer-api +# Running in production + +Running using python + + FLASK_ENV=production PICKLE_KEY= python optimizerapi/server.py + +or use docker + + docker run -d --name process-optimizer-api --env PICKLE_KEY= -p 9090:9090 process-optimizer-api:latest diff --git a/optimizerapi/optimizer.py b/optimizerapi/optimizer.py index 6903c61..adf401b 100644 --- a/optimizerapi/optimizer.py +++ b/optimizerapi/optimizer.py @@ -110,7 +110,7 @@ def processResult(result, optimizer, dimensions, cfg, data, space): addPlot(response["plots"], "convergence") plot_objective(result, dimensions=dimensions, usepartialdependence=False) - addPlot(response["plots"], "objective", debug=True) + addPlot(response["plots"], "objective") prettyResult["pickled"] = pickleToString(result, get_crypto()) # print(str(response)) diff --git a/optimizerapi/server.py b/optimizerapi/server.py index fd5fcae..f46f7ab 100644 --- a/optimizerapi/server.py +++ b/optimizerapi/server.py @@ -1,6 +1,15 @@ +import os import connexion +from securepickle import get_crypto +from waitress import serve if __name__ == '__main__': + # Initialize crypto + get_crypto() app = connexion.FlaskApp(__name__, port=9090, specification_dir='./openapi/') app.add_api('specification.yml', arguments={'title': 'Hello World Example'}) - app.run() \ No newline at end of file + if os.getenv("FLASK_ENV", "development") == "development": + os.environ["FLASK_ENV"] = "development" + app.run() + else: + serve(app, listen='*:9090') \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index 351437f..0ea2dba 100644 --- a/requirements.txt +++ b/requirements.txt @@ -3,4 +3,5 @@ connexion[swagger-ui] Flask==1.1.2 ProcessOptimizer==0.6.1 json-tricks==3.15.5 -cryptography==3.4.7 \ No newline at end of file +cryptography==3.4.7 +waitress==2.0.0 \ No newline at end of file