import http.server, threading, time, pathlib, json
root=pathlib.Path(__file__).resolve().parent
class Gateway(http.server.BaseHTTPRequestHandler):
 def log_message(self,*args): pass
 def do_POST(self):
  data=self.rfile.read(int(self.headers.get('Content-Length',0)))
  mode=(root/'mode').read_text().strip() if (root/'mode').exists() else 'limit'
  with (root/'gateway.log').open('a') as f: f.write(json.dumps({'at':time.time(),'path':self.path,'mode':mode})+'\n')
  if '/messages' in self.path and mode=='hang':
   while True: time.sleep(60)
  else:
   body=json.dumps({'type':'error','error':{'type':'rate_limit_error','message':'All credentials for model claude-fable-5-2 are cooling down (last error: rate limit exceeded) (reset after 44h4m3s)'}}).encode()
   self.send_response(429); self.send_header('Content-Type','application/json'); self.send_header('retry-after','158643'); self.send_header('Content-Length',str(len(body))); self.end_headers(); self.wfile.write(body)
http.server.ThreadingHTTPServer(('127.0.0.1',18778),Gateway).serve_forever()
