|
from flask import Flask, request, Response |
|
import os |
|
import requests |
|
|
|
app = Flask(__name__) |
|
|
|
|
|
API_KEY = os.getenv("TYPEGPT_API_KEY") |
|
FAKE_KEY = os.getenv("FAKE_KEY", "fake-key") |
|
BASE_URL = "https://api.typegpt.net" |
|
|
|
|
|
@app.route("/<path:endpoint>", methods=["POST"]) |
|
def proxy(endpoint): |
|
|
|
client_key = request.headers.get("Authorization") |
|
if client_key != f"Bearer {FAKE_KEY}": |
|
return Response("{\"error\": \"Unauthorized\"}", status=401, mimetype="application/json") |
|
|
|
|
|
headers = { |
|
"Authorization": f"Bearer {API_KEY}", |
|
"Content-Type": "application/json" |
|
} |
|
data = request.get_data() |
|
response = requests.post(f"{BASE_URL}/{endpoint}", headers=headers, data=data) |
|
|
|
|
|
return Response(response.content, status=response.status_code, headers=dict(response.headers)) |
|
|
|
if __name__ == "__main__": |
|
app.run(host="0.0.0.0", port=5000) |
|
|