File size: 2,294 Bytes
231ed61
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""
Dyad-style live preview backend for Crane Builder.
Serves an iframeable preview page and accepts live edits via POST.
Run on the VM:  python3 scripts/preview_server.py
"""
import json
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer

BASE_HTML = """<!doctype html><html><head><meta charset="utf-8"><title>Crane Live Preview</title>
<style id="live-css"></style></head><body><div id="live-html"></div>
<script id="live-js"></script>
</body></html>"""

state = {"css":"", "js":"", "html":""}

class Handler(BaseHTTPRequestHandler):
    def cors(self):
        self.send_header("Access-Control-Allow-Origin","*")
        self.send_header("Access-Control-Allow-Headers","Content-Type")
        self.send_header("Access-Control-Allow-Methods","GET,POST,OPTIONS")

    def do_OPTIONS(self):
        self.send_response(204); self.cors(); self.end_headers()

    def do_GET(self):
        if self.path == "/" or self.path == "/preview":
            body = BASE_HTML.replace('<style id="live-css"></style>', f'<style id="live-css">{state["css"]}</style>')
            body = body.replace('<div id="live-html"></div>', f'<div id="live-html">{state["html"]}</div>')
            body = body.replace('<script id="live-js"></script>', f'<script id="live-js">{state["js"]}</script>')
            self.send_response(200); self.cors()
            self.send_header("Content-Type","text/html")
            self.end_headers(); self.wfile.write(body.encode()); return
        self.send_response(404); self.end_headers()

    def do_POST(self):
        if self.path != "/api/update":
            self.send_response(404); self.end_headers(); return
        length = int(self.headers.get("Content-Length",0))
        data = json.loads(self.rfile.read(length).decode())
        state["css"] = data.get("css","")
        state["js"]  = data.get("js","")
        state["html"]= data.get("html","")
        self.send_response(200); self.cors()
        self.send_header("Content-Type","application/json")
        self.end_headers(); self.wfile.write(b'{"ok":true}')

    def log_message(self,*args): pass

if __name__ == "__main__":
    import os
    port = int(os.environ.get("CRANE_PREVIEW_PORT","11438"))
    ThreadingHTTPServer(("127.0.0.1",port), Handler).serve_forever()