aboutsummaryrefslogtreecommitdiff
path: root/backend/server.py
blob: 48d6c3f8d62069630494ab88bdee4fe5135a80f9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#!/usr/bin/python3

import sys, os
from wsgiref import simple_server, util
import mimetypes
from tilo import application

import argparse
parser = argparse.ArgumentParser(description="""
Runs a basic dev server that serves a WSGI script and image files
""")
parser.parse_args()

# WSGI handler that uses 'application', but also serves image files
def wrappingApp(environ, start_response):
	urlPath = environ["PATH_INFO"]
	if urlPath.startswith("/data/"):
		# Run WSGI script
		return application(environ, start_response)
	elif urlPath.startswith("/tolData/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()