From 0db92beda8b8839252e954a5cb433ab4b5539826 Mon Sep 17 00:00:00 2001 From: Ian Gulliver Date: Thu, 5 May 2016 00:15:45 +0000 Subject: [PATCH] Serve static files --- server/server.py | 23 ++++++++++++++++++++++- server/static/root.html | 1 + 2 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 server/static/root.html diff --git a/server/server.py b/server/server.py index bcdec43..77900c6 100755 --- a/server/server.py +++ b/server/server.py @@ -137,12 +137,15 @@ class INotifyHandler(pyinotify.ProcessEvent): class HTTPRequestHandler(object): _MIME_TYPES = { + '.html': 'text/html', '.iso': 'application/octet-stream', '.json': 'application/json', } _BLOCK_SIZE = 2 ** 16 def __init__(self, image_path, image_types, websockets): + self._static_path = os.path.join(os.path.dirname(sys.argv[0]), 'static') + self._image_path = image_path self._image_types = image_types @@ -154,9 +157,16 @@ class HTTPRequestHandler(object): def __call__(self, env, start_response): path = env['PATH_INFO'] + + if path == '/': + path = '/static/root.html' + if path.startswith('/image/'): image_type, image_name = path[7:].split('/', 1) return self._ServeImageFile(start_response, image_type, image_name) + elif path.startswith('/static/'): + file_name = path[8:] + return self._ServeStaticFile(start_response, file_name) elif path == '/ws/slave': return self._slave_ws_handler(env, start_response) elif path == '/ws/master': @@ -191,7 +201,18 @@ class HTTPRequestHandler(object): start_response('404 Not found') return [] - return + def _ServeStaticFile(self, start_response, file_name): + file_name = os.path.basename(file_name) + assert not file_name.startswith('.') + + file_path = os.path.join(self._static_path, file_name) + try: + with open(file_path, 'rb') as fh: + start_response('200 OK', [('Content-Type', self._MIMEType(file_name))]) + return [fh.read()] + except FileNotFoundError: + start_response('404 Not found') + return [] class Server(object): diff --git a/server/static/root.html b/server/static/root.html new file mode 100644 index 0000000..9daeafb --- /dev/null +++ b/server/static/root.html @@ -0,0 +1 @@ +test