aboutsummaryrefslogtreecommitdiff
path: root/backend/server.py
diff options
context:
space:
mode:
authorTerry Truong <terry06890@gmail.com>2022-10-05 19:52:12 +1100
committerTerry Truong <terry06890@gmail.com>2022-10-05 19:52:12 +1100
commitdf14a1112e28597483de86619dcbd57dc5b15db7 (patch)
treed16c438d6164606f2b6706f6c4d5009a9e18b159 /backend/server.py
parentb1d4c709cb2793745e61d85c337514b9c6c85603 (diff)
Add data serving scripts
Add histplorer.py, implementing the http query interface Add unit test Add server.py as a simple backend dev server Update documentation
Diffstat (limited to 'backend/server.py')
-rwxr-xr-xbackend/server.py39
1 files changed, 39 insertions, 0 deletions
diff --git a/backend/server.py b/backend/server.py
new file mode 100755
index 0000000..70e847b
--- /dev/null
+++ b/backend/server.py
@@ -0,0 +1,39 @@
+#!/usr/bin/python3
+
+"""
+Runs a basic dev server that serves a WSGI script and image files
+"""
+
+from typing import Iterable
+import os
+from wsgiref import simple_server, util
+import mimetypes
+from histplorer import application
+
+import argparse
+parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
+parser.parse_args()
+
+def wrappingApp(environ: dict[str, str], start_response) -> Iterable[bytes]:
+ """ WSGI handler that uses 'application', but also serves image files """
+ urlPath = environ['PATH_INFO']
+ if urlPath.startswith('/data/'):
+ # Run WSGI script
+ return application(environ, start_response)
+ elif urlPath.startswith('/hist_data/img/'):
+ # Serve image file
+ imgPath = os.path.join(os.getcwd(), urlPath[1:])
+ if os.path.exists(imgPath):
+ imgType = mimetypes.guess_type(imgPath)[0]
+ start_response('200 OK', [('Content-type', imgType)])
+ return util.FileWrapper(open(imgPath, 'rb'))
+ else:
+ start_response('404 Not Found', [('Content-type', 'text/plain')])
+ return [b'No image found']
+ else:
+ start_response('404 Not Found', [('Content-type', 'text/plain')])
+ return [b'Unrecognised path']
+# Start server
+with simple_server.make_server('', 8000, wrappingApp) as httpd:
+ print('Serving HTTP on port 8000...')
+ httpd.serve_forever()