-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
serve.py
39 lines (31 loc) · 1.28 KB
/
serve.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
from tornado.web import RequestHandler
from tornado.httpclient import AsyncHTTPClient
from livereload import Server, shell
shell("make build-docs")()
class ProxyHandler(RequestHandler):
proxy_url = "https://shibuya.readthedocs.io/_/"
async def get(self, path: str) -> None:
query = self.request.query
url = self.proxy_url + path
if query:
url += '?' + query
client = AsyncHTTPClient()
response = await client.fetch(url)
self.set_status(response.code)
if response.body:
for header in response.headers:
if header.lower() == "content-length":
self.set_header(header, str(max(len(response.body), int(response.headers.get(header)))))
elif header.lower() != "transfer-encoding":
self.set_header(header, response.headers.get(header))
self.write(response.body)
self.finish()
class DebugServer(Server):
def get_web_handlers(self, script):
proxy = (r"/_/(.*)", ProxyHandler)
handlers = super().get_web_handlers(script)
return [proxy, *handlers]
app = DebugServer()
app.watch("src", shell("make build-docs"), delay=2)
app.watch("docs", shell("make build-docs"), delay=2)
app.serve(root="build/_html")