Skip to content

Commit

Permalink
added spa serve script
Browse files Browse the repository at this point in the history
  • Loading branch information
jesseflorig committed Mar 23, 2023
1 parent 7aa2647 commit 8e10b39
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions dashboard/serve.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/usr/bin/env python

# Inspired by https://gist.github.com/jtangelder/e445e9a7f5e31c220be6
# Python3 http.server for Single Page Application

import urllib.parse
import http.server
import socketserver
import re
from pathlib import Path

HOST = ('0.0.0.0', 80)
pattern = re.compile('.png|.jpg|.jpeg|.js|.css|.ico|.gif|.svg', re.IGNORECASE)

class Handler(http.server.SimpleHTTPRequestHandler):
def do_GET(self):
url_parts = urllib.parse.urlparse(self.path)
request_file_path = Path(url_parts.path.strip("/"))

ext = request_file_path.suffix
if not request_file_path.is_file() and not pattern.match(ext):
self.path = 'index.html'

return http.server.SimpleHTTPRequestHandler.do_GET(self)

httpd = socketserver.TCPServer(HOST, Handler)
httpd.serve_forever()

0 comments on commit 8e10b39

Please sign in to comment.