Skip to content

Commit

Permalink
Switches from flask development server to gunicorn for serving
Browse files Browse the repository at this point in the history
  • Loading branch information
Dennis Muth committed Jul 15, 2018
1 parent ea26f54 commit 4c327c9
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 19 deletions.
5 changes: 3 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ ENV WORKDIR /rpi-433rc
RUN mkdir -p ${WORKDIR} && \
cd ${WORKDIR}


# Install rest-api wrapper for rpi-rf
COPY . ${WORKDIR}
RUN pip3 install -r ${WORKDIR}/requirements.txt
# Setting correct entrypoint
ENV FLASK_APP ${WORKDIR}/rpi433rc/app.py
# ENV FLASK_APP ${WORKDIR}/rpi433rc/app.py
ENV PYTHONPATH=${WORKDIR}
ENV FLASK_MODULE=rpi433rc.app:app

# Re-copy the entrypoint.sh to the root
COPY ./entrypoint.sh /entrypoint.sh
Expand Down
8 changes: 7 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,11 @@ clean-build:

clean: clean-pyc clean-build

pin:
pip-compile --output-file requirements.txt requirements.in

lint:
flake8 --exclude=.tox --max-line-length 120 $(SOURCE_PATH)
flake8 --exclude=.tox --max-line-length 120 --ignore=E402 $(SOURCE_PATH)

test:
pytest --verbose --color=yes -s \
Expand All @@ -36,3 +39,6 @@ test:

doctest:
pytest --verbose --color=yes --doctest-modules $(SOURCE_PATH)

gunicorn:
gunicorn --bind 0.0.0.0:5000 wsgi
2 changes: 1 addition & 1 deletion entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ case ${1} in
exec rpi-rf_receive "$@"
;;
serve)
exec flask run --host=0.0.0.0
exec gunicorn --workers 4 --bind 0.0.0.0:5000 ${FLASK_MODULE}
;;
*)
exec "$@"
Expand Down
1 change: 1 addition & 0 deletions requirements.in
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
attrs
flask
flask-restplus
gunicorn
schema
rpi-rf
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ attrs==17.4.0
click==6.7 # via flask
flask-restplus==0.10.1
flask==0.12.2
gunicorn==19.9.0
itsdangerous==0.24 # via flask
jinja2==2.10 # via flask
jsonschema==2.6.0 # via flask-restplus
Expand Down
28 changes: 19 additions & 9 deletions rpi433rc/business/devices.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,10 @@ class DeviceDict(DeviceStore):
... 'device2': {"system_code": "00010", "device_code": "2"}
... }
>>> dut = DeviceDict(device_dict)
>>> sorted(dut.list(), key=lambda e: e.device_name)
[CodeDevice(device_name='device1', code_on=12345, code_off=23456), SystemDevice(device_name='device2', system_code='00010', device_code=2)]
>>> (sorted(dut.list(), key=lambda e: e.device_name) ==
... [CodeDevice(device_name='device1', code_on=12345, code_off=23456),
... SystemDevice(device_name='device2', system_code='00010', device_code=2)])
True
>>> dut.lookup('device1')
CodeDevice(device_name='device1', code_on=12345, code_off=23456)
Expand All @@ -154,8 +156,10 @@ class DeviceDict(DeviceStore):
>>> with open(fn, 'w') as fp:
... json.dump(device_dict, fp)
>>> dut = DeviceDict.from_json(fn)
>>> sorted(dut.list(), key=lambda e: e.device_name)
[CodeDevice(device_name='device1', code_on=12345, code_off=23456), SystemDevice(device_name='device2', system_code='00010', device_code=2)]
>>> (sorted(dut.list(), key=lambda e: e.device_name) ==
... [CodeDevice(device_name='device1', code_on=12345, code_off=23456),
... SystemDevice(device_name='device2', system_code='00010', device_code=2)])
True
"""
device_dict = attr.ib(validator=attr.validators.instance_of(dict))
devices = attr.ib(default=None, repr=False, cmp=False, hash=False, init=False)
Expand Down Expand Up @@ -192,7 +196,7 @@ def _init_device(device_name, props):
for dev in __ALL_DEVICES__:
try:
return dev.from_props(device_name, props)
except:
except (TypeError, ValueError):
# import traceback
# traceback.print_exc()
pass
Expand Down Expand Up @@ -332,11 +336,17 @@ class DeviceRegistry(DeviceStore, DeviceState):
>>> dstate = MemoryState() # Instantiate the device state
>>> dut = DeviceRegistry(dstore, dstate)
>>> sorted(dut.list(), key=lambda e: e.device.device_name)
[StatefulDevice(device=CodeDevice(device_name='device1', code_on=12345, code_off=23456), state=False), StatefulDevice(device=SystemDevice(device_name='device2', system_code='00010', device_code=2), state=False)]
>>> (sorted(dut.list(), key=lambda e: e.device.device_name) ==
... [StatefulDevice(device=CodeDevice(device_name='device1', code_on=12345, code_off=23456), state=False),
... StatefulDevice(device=SystemDevice(device_name='device2', system_code='00010', device_code=2)
... , state=False)])
True
>>> dut.lookup('device1'), dut.switch('device1', True), dut.lookup('device1')
(StatefulDevice(device=CodeDevice(device_name='device1', code_on=12345, code_off=23456), state=False), None, StatefulDevice(device=CodeDevice(device_name='device1', code_on=12345, code_off=23456), state=True))
>>> ((dut.lookup('device1'), dut.switch('device1', True), dut.lookup('device1')) ==
... (StatefulDevice(device=CodeDevice(device_name='device1', code_on=12345, code_off=23456), state=False),
... None,
... StatefulDevice(device=CodeDevice(device_name='device1', code_on=12345, code_off=23456), state=True)))
True
"""
device_store = attr.ib(validator=attr.validators.instance_of(DeviceStore))
Expand Down
3 changes: 0 additions & 3 deletions rpi433rc/business/util.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import contextlib
import logging

import os


class LogMixin(object):
"""
Expand Down
4 changes: 1 addition & 3 deletions run.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#!/usr/bin/env bash

export FLASK_APP=`pwd`/rpi433rc/app.py

flask run --host=0.0.0.0
gunicorn --workers 4 --bind 0.0.0.0:5000 rpi433rc.app:app

0 comments on commit 4c327c9

Please sign in to comment.