Rooni's picture
Update app.py
10f0fe4 verified
raw
history blame
1.28 kB
import os
import requests
from flask import Flask, request, Response
app = Flask(__name__)
OR_BASE_URL = "https://openrouter.ai/api"
@app.route("/", methods=["GET"])
def home():
return {"status": "ok", "message": "OpenRouter proxy is working :3"}
@app.route("/<path:endpoint>", methods=["GET", "POST", "PUT", "PATCH", "DELETE"])
def proxy(endpoint):
url = f"{OR_BASE_URL}/{endpoint}"
# Копируем все заголовки, кроме host и content-length
headers = {k: v for k, v in request.headers if k.lower() not in ["host", "content-length"]}
try:
resp = requests.request(
method=request.method,
url=url,
headers=headers,
params=request.args,
data=request.get_data(),
allow_redirects=False,
stream=True,
)
excluded_headers = ["content-encoding", "transfer-encoding", "connection"]
response_headers = [(k, v) for k, v in resp.raw.headers.items() if k.lower() not in excluded_headers]
return Response(resp.content, resp.status_code, response_headers)
except Exception as e:
return {"error": str(e)}, 500
if __name__ == "__main__":
app.run(host="0.0.0.0", port=int(os.environ.get("PORT", 7860)))