File size: 1,250 Bytes
d1caa7d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Serve the demo's files, with cache headers so a Space update reaches visitors on a normal reload.

python -m http.server sends no Cache-Control, so browsers may keep an old index.html heuristically and pair it with
a newer worker.js or tree.json. Here the page, scripts and data are revalidated on every load (a cheap 304 when
unchanged), and only files whose names change with their content (the bot bundle) are cached for good.
"""
import os
from http.server import SimpleHTTPRequestHandler, ThreadingHTTPServer


class Handler(SimpleHTTPRequestHandler):
    def end_headers(self):
        path = self.path.split("?", 1)[0]
        if path.endswith(".zip"):        # pybot-<sha12>-<hash>.zip: renamed on every rebuild
            self.send_header("Cache-Control", "public, max-age=31536000, immutable")
        elif path.startswith("/fonts/"):
            self.send_header("Cache-Control", "public, max-age=86400")
        else:                            # index.html, worker.js, pybot_web.py, tree.json
            self.send_header("Cache-Control", "no-cache")
        super().end_headers()


if __name__ == "__main__":
    ThreadingHTTPServer((os.environ.get("HOST", "0.0.0.0"), int(os.environ.get("PORT", "7860"))), Handler).serve_forever()