-
Notifications
You must be signed in to change notification settings - Fork 0
/
simple_spa_server.py
39 lines (29 loc) · 946 Bytes
/
simple_spa_server.py
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
37
38
39
#!/usr/bin/env python
"""
Modification of `python -m SimpleHTTPServer` with a fallback to /index.html
on requests for non-existing files.
This is useful when serving a static single page application using the HTML5
history API.
"""
import os
import sys
import urlparse
import SimpleHTTPServer
import BaseHTTPServer
class Handler(SimpleHTTPServer.SimpleHTTPRequestHandler):
def do_GET(self):
urlparts = urlparse.urlparse(self.path)
request_file_path = 'dist/'+urlparts.path.strip('/')
if os.path.exists(request_file_path):
self.path = request_file_path
else:
self.path = 'dist/index.html'
return SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET(self)
host = '0.0.0.0'
try:
port = int(sys.argv[1])
except IndexError:
port = 3000
httpd = BaseHTTPServer.HTTPServer((host, port), Handler)
print 'Serving HTTP on %s port %d ...' % (host, port)
httpd.serve_forever()