-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserve_frontend.py
More file actions
30 lines (26 loc) · 1.27 KB
/
Copy pathserve_frontend.py
File metadata and controls
30 lines (26 loc) · 1.27 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
import os, sys, http.server, socketserver
PORT = int(os.environ.get("FRONTEND_PORT", "99"))
DIR = os.environ.get("FRONTEND_DIR", os.path.join(os.getcwd(), "frontend"))
class Handler(http.server.SimpleHTTPRequestHandler):
def __init__(self, *a, directory=None, **k):
super().__init__(*a, directory=DIR, **k)
def end_headers(self):
path = self.path.split("?",1)[0]
if path.endswith(".html") or path.endswith("/") or path == "/":
self.send_header("Cache-Control", "no-store, max-age=0")
elif path.endswith(".js") or path.endswith(".css") or "script." in path:
super().end_headers()
def log_message(self, fmt, *args):
sys.stdout.write("%s - - [%s] %s\n" % (self.client_address[0], self.log_date_time_string(), fmt%args)); sys.stdout.flush()
Handler.extensions_map.update({
".html": "text/html; charset=utf-8",
".htm" : "text/html; charset=utf-8",
".js" : "application/javascript; charset=utf-8",
".css" : "text/css; charset=utf-8",
"": "text/html; charset=utf-8",
})
if __name__ == "__main__":
with socketserver.TCPServer(("", PORT), Handler) as httpd:
print(f"Serving {DIR} at http://127.0.0.1:{PORT}")
try: httpd.serve_forever()
except KeyboardInterrupt: pass