Skip to content

Commit 4e67f1a

Browse files
Merge pull request #757 from VWS-Python/dockerfiles
Add Dockerfiles to run the Flask application
2 parents 9774c16 + f34de90 commit 4e67f1a

File tree

13 files changed

+247
-19
lines changed

13 files changed

+247
-19
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ jobs:
6464
- test_update_target.py::TestWidth
6565
- test_update_target.py::TestInactiveProject
6666
- test_usage.py
67+
- test_docker.py
6768

6869
steps:
6970
# We share Vuforia credentials and therefore Vuforia databases across

dev-requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ autoflake==1.4
77
black==20.8b1
88
check-manifest==0.43
99
doc8==0.8.1
10+
docker==4.3.1
1011
dodgy==0.2.1 # Look for uploaded secrets
1112
flake8-commas==2.0.0 # Require silicon valley commas
1213
flake8-quotes==3.2.0 # Require single quotes

docs/source/differences-to-vws.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,3 +101,8 @@ These are:
101101
* ``TargetQuotaReached``
102102
* ``ProjectSuspended``
103103
* ``ProjectHasNoAPIAccess``
104+
105+
``Content-Length`` headers
106+
--------------------------
107+
108+
When the given ``Content-Length`` header does not match the length of the given data, the mock server (written with Flask) will not behave as the real Vuforia Web Services behaves.

setup.cfg

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ ignore =
1919
CONTRIBUTING.rst
2020
LICENSE
2121
Makefile
22+
src/mock_vws/_flask_server/dockerfiles/base/Dockerfile
23+
src/mock_vws/_flask_server/dockerfiles/storage/Dockerfile
24+
src/mock_vws/_flask_server/dockerfiles/vwq/Dockerfile
25+
src/mock_vws/_flask_server/dockerfiles/vws/Dockerfile
2226
ci
2327
ci/**
2428
codecov.yaml

setup.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,10 @@ def _get_dependencies(requirements_file: Path) -> List[str]:
3131
)
3232

3333
setup(
34-
use_scm_version=True,
34+
# We use a dictionary with a fallback version rather than "True"
35+
# like https://github.com/pypa/setuptools_scm/issues/77 so that we do not
36+
# error in Docker.
37+
use_scm_version={'fallback_version': 'FALLBACK_VERSION'},
3538
setup_requires=SETUP_REQUIRES,
3639
install_requires=INSTALL_REQUIRES,
3740
extras_require={'dev': DEV_REQUIRES},
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
FROM python:3.8-slim-buster
2+
COPY . /app
3+
WORKDIR /app
4+
RUN pip install .
5+
EXPOSE 5000
6+
ENTRYPOINT ["python"]
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
FROM vws-mock:base
2+
CMD ["src/mock_vws/_flask_server/storage.py"]
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
FROM vws-mock:base
2+
CMD ["src/mock_vws/_flask_server/vwq.py"]
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
FROM vws-mock:base
2+
CMD ["src/mock_vws/_flask_server/vws.py"]

src/mock_vws/_flask_server/vwq.py

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
https://library.vuforia.com/articles/Solution/How-To-Perform-an-Image-Recognition-Query
66
"""
77

8-
import copy
98
import email.utils
109
from http import HTTPStatus
1110
from typing import Final, Set
@@ -27,7 +26,7 @@
2726

2827
CLOUDRECO_FLASK_APP = Flask(import_name=__name__)
2928
CLOUDRECO_FLASK_APP.config['PROPAGATE_EXCEPTIONS'] = True
30-
STORAGE_BASE_URL: Final[str] = 'http://todo.com'
29+
STORAGE_BASE_URL: Final[str] = 'http://vws-mock-storage:5000'
3130

3231

3332
def get_all_databases() -> Set[VuforiaDatabase]:
@@ -42,21 +41,25 @@ def get_all_databases() -> Set[VuforiaDatabase]:
4241

4342

4443
@CLOUDRECO_FLASK_APP.before_request
45-
def validate_request() -> None:
44+
def set_terminate_wsgi_input() -> None:
4645
"""
47-
Run validators on the request.
46+
We set ``wsgi.input_terminated`` to ``True`` when going through
47+
``requests``, so that requests have the given ``Content-Length`` headers
48+
and the given data in ``request.headers`` and ``request.data``.
49+
50+
We set this to ``False`` when running an application as standalone.
51+
This is because when running the Flask application, if this is set,
52+
reading ``request.data`` hangs.
53+
54+
Therefore, when running the real Flask application, the behavior is not the
55+
same as the real Vuforia.
56+
This is documented as a difference in the documentation for this package.
4857
"""
49-
request.environ['wsgi.input_terminated'] = True
50-
input_stream_copy = copy.copy(request.input_stream)
51-
request_body = input_stream_copy.read()
52-
databases = get_all_databases()
53-
run_query_validators(
54-
request_headers=dict(request.headers),
55-
request_body=request_body,
56-
request_method=request.method,
57-
request_path=request.path,
58-
databases=databases,
58+
terminate_wsgi_input = CLOUDRECO_FLASK_APP.config.get(
59+
'TERMINATE_WSGI_INPUT',
60+
False,
5961
)
62+
request.environ['wsgi.input_terminated'] = terminate_wsgi_input
6063

6164

6265
class ResponseNoContentTypeAdded(Response):
@@ -94,9 +97,16 @@ def query() -> Response:
9497
"""
9598
query_processes_deletion_seconds = 0.2
9699
query_recognizes_deletion_seconds = 0.2
100+
97101
databases = get_all_databases()
98-
input_stream_copy = copy.copy(request.input_stream)
99-
request_body = input_stream_copy.read()
102+
request_body = request.stream.read()
103+
run_query_validators(
104+
request_headers=dict(request.headers),
105+
request_body=request_body,
106+
request_method=request.method,
107+
request_path=request.path,
108+
databases=databases,
109+
)
100110
date = email.utils.formatdate(None, localtime=False, usegmt=True)
101111

102112
try:

0 commit comments

Comments
 (0)