import http.server
import json
import os
import urllib.request
import urllib.error
COZO_API = "http://localhost:9070"
PORT = 8080
SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
class Handler(http.server.BaseHTTPRequestHandler):
def do_GET(self):
if self.path == "/" or self.path == "/index.html":
self.send_response(200)
self.send_header("Content-Type", "text/html")
self.end_headers()
with open(os.path.join(SCRIPT_DIR, "index.html"), "rb") as f:
self.wfile.write(f.read())
else:
self.send_response(404)
self.end_headers()
def do_POST(self):
if self.path == "/api/query" or self.path == "/api/text-query":
try:
content_length = int(self.headers.get("Content-Length", 0))
body = self.rfile.read(content_length).decode("utf-8")
req = urllib.request.Request(
f"{COZO_API}/text-query",
data=body.encode("utf-8"),
headers={"Content-Type": "application/json"},
method="POST",
)
with urllib.request.urlopen(req, timeout=30) as resp:
data = resp.read()
self.send_response(200)
self.send_header("Content-Type", "application/json")
self.send_header("Access-Control-Allow-Origin", "*")
self.end_headers()
self.wfile.write(data)
except urllib.error.URLError as e:
self.send_response(502)
self.send_header("Content-Type", "application/json")
self.send_header("Access-Control-Allow-Origin", "*")
self.end_headers()
self.wfile.write(json.dumps({"error": str(e)}).encode())
else:
self.send_response(404)
self.end_headers()
def do_OPTIONS(self):
self.send_response(200)
self.send_header("Access-Control-Allow-Origin", "*")
self.send_header("Access-Control-Allow-Methods", "GET, POST, OPTIONS")
self.send_header("Access-Control-Allow-Headers", "Content-Type")
self.end_headers()
if __name__ == "__main__":
print(f"Serving graph viewer at http://localhost:{PORT}")
print(f"Proxying CozoDB API at {COZO_API}")
server = http.server.HTTPServer(("", PORT), Handler)
server.serve_forever()