CoolFace
Apppublic

jongjing01/orion-secopsx

sourceHugging Faceupdated 3mo agoView on Hugging Face
0likes
proxy.py78 linesDownload Raw Back to root
1#!/usr/bin/env python32"""Proxy: serves /health and proxies /v1/* to gateway at 127.0.0.1:8642"""3import http.server4import json5import os6import urllib.request7 8PUBLIC_PORT = int(os.environ.get("PORT", "7860"))9GATEWAY_PORT = int(os.environ.get("API_SERVER_PORT", "8642"))10GATEWAY_HOST = "127.0.0.1"11 12class ProxyHandler(http.server.BaseHTTPRequestHandler):13    def do_GET(self):14        if self.path == "/health":15            self._json(200, {"status": "ok", "service": "orion-secopsx"})16        elif self.path.startswith("/v1/"):17            self._proxy()18        else:19            self._json(404, {"error": "not found"})20 21    def do_POST(self):22        if self.path.startswith("/v1/"):23            self._proxy()24        else:25            self._json(404, {"error": "not found"})26 27    def do_OPTIONS(self):28        self.send_response(204)29        self.send_header("Access-Control-Allow-Origin", "*")30        self.send_header("Access-Control-Allow-Methods", "GET, POST, OPTIONS")31        self.send_header("Access-Control-Allow-Headers", "Content-Type, Authorization")32        self.end_headers()33 34    def _proxy(self):35        body_bytes = None36        if self.command in ("POST", "PUT"):37            length = int(self.headers.get("Content-Length", 0))38            body_bytes = self.rfile.read(length) if length > 0 else None39 40        target = f"http://{GATEWAY_HOST}:{GATEWAY_PORT}{self.path}"41        try:42            req = urllib.request.Request(43                target,44                data=body_bytes,45                method=self.command,46                headers={k: v for k, v in self.headers.items() if k.lower() not in ("host", "transfer-encoding")},47            )48            resp = urllib.request.urlopen(req, timeout=120)49            self.send_response(resp.status)50            for k, v in resp.headers.items():51                if k.lower() not in ("transfer-encoding", "content-encoding", "content-length"):52                    self.send_header(k, v)53            self.send_header("Access-Control-Allow-Origin", "*")54            self.end_headers()55            chunk = resp.read(65536)56            while chunk:57                self.wfile.write(chunk)58                chunk = resp.read(65536)59        except urllib.error.HTTPError as e:60            self.send_response(e.code)61            self.send_header("Content-Type", "application/json")62            self.send_header("Access-Control-Allow-Origin", "*")63            self.end_headers()64            self.wfile.write(e.read())65        except Exception as e:66            self._json(502, {"error": f"gateway unreachable: {str(e)}"})67 68    def _json(self, status, data):69        self.send_response(status)70        self.send_header("Content-Type", "application/json")71        self.send_header("Access-Control-Allow-Origin", "*")72        self.end_headers()73        self.wfile.write(json.dumps(data).encode())74 75    def log_message(self, *a): pass76 77http.server.HTTPServer(("0.0.0.0", PUBLIC_PORT), ProxyHandler).serve_forever()78