Read-only mirror of the client's existing system as it stood before our work began. Taken from matthijsexpand@149.210.159.152 (TransIP VPS, Lelystad NL) with the client's written permission. Contents: - home-weddingcaketopper/ Next.js app, proxy.py, supabase schema - var-www/ static webroots for all three domains - etc-nginx/ vhost configs (previously unversioned, server-only) Excluded: node_modules (704M, regenerable), .next (17M, regenerable), and env file contents (present on local disk, gitignored — see README). Not our IP. Not covered by the proposal's assignment clause.
62 lines
2.6 KiB
Python
62 lines
2.6 KiB
Python
import http.server
|
|
import urllib.request
|
|
import json
|
|
|
|
env = {}
|
|
for line in open('/etc/weddingcaketopper.env').read().strip().split('\n'):
|
|
if '=' in line:
|
|
k, v = line.split('=', 1)
|
|
env[k.strip()] = v.strip()
|
|
|
|
ANTHROPIC_KEY = env.get('ANTHROPIC_API_KEY', '')
|
|
OPENAI_KEY = env.get('OPENAI_API_KEY', '')
|
|
|
|
class ProxyHandler(http.server.BaseHTTPRequestHandler):
|
|
def do_POST(self):
|
|
length = int(self.headers['Content-Length'])
|
|
body = self.rfile.read(length)
|
|
if self.path == '/api/claude':
|
|
url = 'https://api.anthropic.com/v1/messages'
|
|
headers = {'Content-Type':'application/json','x-api-key':ANTHROPIC_KEY,'anthropic-version':'2023-06-01'}
|
|
elif self.path == '/api/imagine':
|
|
url = 'https://api.openai.com/v1/images/generations'
|
|
headers = {'Content-Type':'application/json','Authorization':f'Bearer {OPENAI_KEY}'}
|
|
elif self.path == '/api/imagine-edit':
|
|
url = 'https://api.openai.com/v1/images/edits'
|
|
headers = {'Content-Type':self.headers.get('Content-Type',''),'Authorization':f'Bearer {OPENAI_KEY}'}
|
|
else:
|
|
self.send_response(404); self.end_headers(); return
|
|
try:
|
|
req = urllib.request.Request(url, data=body, headers=headers)
|
|
resp = urllib.request.urlopen(req, timeout=400)
|
|
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.HTTPError as e:
|
|
data = e.read()
|
|
self.send_response(e.code)
|
|
self.send_header('Content-Type','application/json')
|
|
self.send_header('Access-Control-Allow-Origin','*')
|
|
self.end_headers()
|
|
self.wfile.write(data)
|
|
except Exception as e:
|
|
self.send_response(500)
|
|
self.send_header('Content-Type','application/json')
|
|
self.send_header('Access-Control-Allow-Origin','*')
|
|
self.end_headers()
|
|
self.wfile.write(json.dumps({'error':{'message':str(e)}}).encode())
|
|
def do_OPTIONS(self):
|
|
self.send_response(200)
|
|
self.send_header('Access-Control-Allow-Origin','*')
|
|
self.send_header('Access-Control-Allow-Methods','POST')
|
|
self.send_header('Access-Control-Allow-Headers','Content-Type,Authorization')
|
|
self.end_headers()
|
|
def log_message(self, format, *args):
|
|
pass
|
|
|
|
httpd = http.server.HTTPServer(('localhost', 5000), ProxyHandler)
|
|
print('Proxy draait op poort 5000')
|
|
httpd.serve_forever()
|