35 lines
1.2 KiB
Python
35 lines
1.2 KiB
Python
#!/usr/bin/env python3
|
|
from flask import Flask, request, Response
|
|
import requests
|
|
|
|
app = Flask(__name__)
|
|
GROQ_BASE = "https://api.groq.com"
|
|
|
|
CORS = {
|
|
"Access-Control-Allow-Origin": "*",
|
|
"Access-Control-Allow-Methods": "GET, POST, OPTIONS",
|
|
"Access-Control-Allow-Headers": "Authorization, Content-Type",
|
|
}
|
|
|
|
@app.route("/<path:path>", methods=["OPTIONS"])
|
|
def options(path):
|
|
return Response(status=204, headers=CORS)
|
|
|
|
# Generic proxy for ALL Groq paths
|
|
@app.route("/<path:subpath>", methods=["GET", "POST", "PUT", "DELETE", "PATCH"])
|
|
def proxy(subpath):
|
|
url = f"{GROQ_BASE}/{subpath}"
|
|
hdrs = {}
|
|
if request.headers.get("Authorization"):
|
|
hdrs["Authorization"] = request.headers["Authorization"]
|
|
if request.headers.get("Content-Type"):
|
|
hdrs["Content-Type"] = request.headers["Content-Type"]
|
|
r = requests.request(request.method, url, headers=hdrs,
|
|
data=request.get_data(), timeout=60, stream=True)
|
|
out = dict(CORS)
|
|
out["Content-Type"] = r.headers.get("Content-Type", "application/json")
|
|
return Response(r.iter_content(8192), status=r.status_code, headers=out)
|
|
|
|
if __name__ == "__main__":
|
|
app.run(host="127.0.0.1", port=8765)
|