-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathserver.py
More file actions
217 lines (195 loc) · 8.08 KB
/
server.py
File metadata and controls
217 lines (195 loc) · 8.08 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# simple dev server: serves out/, runs generate.py on file changes, and notifies clients to reload
import http.server
import socketserver
import threading
import time
import os
import sys
import subprocess
import urllib.parse
PORT = 80
OUT_DIR = os.path.join(os.getcwd(), 'out')
class ThreadingHTTPServer(socketserver.ThreadingMixIn, http.server.HTTPServer):
daemon_threads = True
allow_reuse_address = True
clients = []
clients_lock = threading.Lock()
def inject(html):
# inject live-reload script into html
inject = "<script>" \
"let __liveserver = new EventSource('/__reload');" \
"__liveserver.onmessage=function(){location.reload();};" \
"window.addEventListener('beforeunload', function() { __liveserver.close(); });" \
"</script>"
if '</body>' in html.lower():
idx = html.lower().rfind('</body>')
return html[:idx] + inject + html[idx:]
else:
return html + inject
class DevHandler(http.server.SimpleHTTPRequestHandler):
def translate_path(self, path):
# Serve files from the out/ directory
path = path.split('?', 1)[0].split('#', 1)[0]
path = urllib.parse.unquote(path)
requested = path.lstrip('/')
full_path = os.path.join(OUT_DIR, requested)
return full_path
def do_GET(self):
if self.path.startswith('/__reload'):
self.send_response(200)
self.send_header('Content-Type', 'text/event-stream')
self.send_header('Cache-Control', 'no-cache')
self.send_header('Connection', 'keep-alive')
self.end_headers()
# register client
with clients_lock:
clients.append(self)
print('Client connected, total clients:', len(clients))
try:
# keep connection open
while True:
time.sleep(1)
except Exception:
print('Client connection error')
pass
finally:
with clients_lock:
if self in clients:
clients.remove(self)
print('Client disconnected, total clients:', len(clients))
return
# serve from out/, but inject reload script into html responses
# resolve filesystem path for requested resource
req_path = self.path.split('?', 1)[0].split('#', 1)[0]
fs_path = self.translate_path(req_path)
# if path is a directory, try index.html
if fs_path.endswith(os.path.sep) or os.path.isdir(fs_path):
# If the request URL omitted the trailing slash (e.g. GET /bpe2text),
# browsers resolve relative URLs incorrectly (they request /bpe2text.js).
# GitHub Pages redirects to the slash. Mirror that behavior here.
if os.path.isdir(fs_path):
if not req_path.endswith('/'):
# preserve query string if present
qs = ''
if '?' in self.path:
qs = self.path.split('?', 1)[1]
qs = '?' + qs
self.send_response(301)
self.send_header('Location', req_path + '/' + qs)
self.end_headers()
return
index_path = os.path.join(fs_path, 'index.html') if os.path.isdir(fs_path) else fs_path
if os.path.exists(index_path):
fs_path = index_path
# if requested path has no extension, try adding .html
if not os.path.exists(fs_path):
base, ext = os.path.splitext(fs_path)
if ext == '':
html_candidate = fs_path + '.html'
if os.path.exists(html_candidate) and os.path.isfile(html_candidate):
fs_path = html_candidate
if os.path.exists(fs_path) and os.path.isfile(fs_path):
# serve the file
try:
ctype = self.guess_type(fs_path)
if ctype == 'text/html':
with open(fs_path, 'rb') as f:
data = f.read()
try:
text = data.decode('utf-8', errors='ignore')
text = inject(text)
encoded = text.encode('utf-8')
self.send_response(200)
self.send_header('Content-Type', 'text/html; charset=utf-8')
self.send_header('Content-Length', str(len(encoded)))
self.end_headers()
self.wfile.write(encoded)
except Exception:
# fallback to binary stream
with open(fs_path, 'rb') as f:
self.send_response(200)
self.send_header('Content-Type', ctype)
fs = os.fstat(f.fileno())
self.send_header('Content-Length', str(fs.st_size))
self.end_headers()
self.copyfile(f, self.wfile)
else:
with open(fs_path, 'rb') as f:
self.send_response(200)
self.send_header('Content-Type', ctype)
fs = os.fstat(f.fileno())
self.send_header('Content-Length', str(fs.st_size))
self.end_headers()
self.copyfile(f, self.wfile)
except BrokenPipeError:
return
return
# file not found: try to serve out/404.html (like GitHub Pages)
notfound_path = os.path.join(OUT_DIR, '404.html')
if os.path.exists(notfound_path):
try:
with open(notfound_path, 'rb') as f:
data = f.read()
text = data.decode('utf-8', errors='ignore')
text = inject(text)
encoded = text.encode('utf-8')
self.send_response(404)
self.send_header('Content-Type', 'text/html; charset=utf-8')
self.send_header('Content-Length', str(len(encoded)))
self.end_headers()
self.wfile.write(encoded)
return
except BrokenPipeError:
return
# fallback to default 404
self.send_error(404, 'File not found')
def notify_clients():
dead = []
with clients_lock:
for h in list(clients):
try:
msg = 'data: reload\n\n'
h.wfile.write(msg.encode('utf-8'))
h.wfile.flush()
except Exception:
dead.append(h)
for d in dead:
if d in clients:
clients.remove(d)
def snapshot_tree(root):
state = {}
for dirpath, dirnames, filenames in os.walk(root):
for name in filenames:
if dirpath.startswith(OUT_DIR):
continue
if dirpath.startswith(os.path.join(os.getcwd(), '.git')):
continue
path = os.path.join(dirpath, name)
try:
state[path] = os.path.getmtime(path)
except OSError:
state[path] = None
return state
def watch_and_build(root, interval=1.0):
prev = snapshot_tree(root)
while True:
time.sleep(interval)
curr = snapshot_tree(root)
if curr != prev:
try:
subprocess.run([sys.executable, os.path.join(os.getcwd(), 'generate.py')], check=False)
except Exception as e:
print('Error running generate.py:', e)
notify_clients()
prev = curr
if __name__ == '__main__':
subprocess.run([sys.executable, os.path.join(os.getcwd(), 'generate.py')], check=False)
print('Serving', OUT_DIR, 'on port', PORT)
server = ThreadingHTTPServer(('', PORT), DevHandler)
watcher = threading.Thread(target=watch_and_build, args=(os.getcwd(),), daemon=True)
watcher.start()
try:
server.serve_forever()
except KeyboardInterrupt:
print('\nShutting down')
server.shutdown()