Skip to content

Commit

Permalink
Add production runtime
Browse files Browse the repository at this point in the history
  • Loading branch information
langdal committed Apr 20, 2021
1 parent 5609d33 commit c1f9c1f
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 4 deletions.
2 changes: 2 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ USER user

COPY optimizerapi/ /code

ENV FLASK_ENV=production

ENV PATH=/opt/venv/bin:${PATH}

CMD [ "python", "./server.py" ]
23 changes: 22 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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=<key from previous step> python optimizerapi/server.py

or use docker

docker run -d --name process-optimizer-api --env PICKLE_KEY=<key from previous step> -p 9090:9090 process-optimizer-api:latest
2 changes: 1 addition & 1 deletion optimizerapi/optimizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
11 changes: 10 additions & 1 deletion optimizerapi/server.py
Original file line number Diff line number Diff line change
@@ -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()
if os.getenv("FLASK_ENV", "development") == "development":
os.environ["FLASK_ENV"] = "development"
app.run()
else:
serve(app, listen='*:9090')
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ connexion[swagger-ui]
Flask==1.1.2
ProcessOptimizer==0.6.1
json-tricks==3.15.5
cryptography==3.4.7
cryptography==3.4.7
waitress==2.0.0

0 comments on commit c1f9c1f

Please sign in to comment.