|
|
|
|
|
|
|
|
|
|
|
import logging |
|
import json |
|
from flask import Flask, request, Response |
|
import requests |
|
|
|
app = Flask(__name__) |
|
|
|
|
|
app.logger.setLevel(logging.INFO) |
|
|
|
@app.route('/', defaults={'path': ''}) |
|
@app.route('/<path:path>', methods=['GET', 'POST', 'PUT', 'PATCH', 'DELETE']) |
|
def relay_request(path): |
|
|
|
base_url = 'https://username84-g.hf.space' |
|
|
|
|
|
relay_url = base_url + '/' + path |
|
|
|
|
|
headers_ = dict() |
|
|
|
|
|
if 'Content-Type' in request.headers: |
|
headers_['Content-Type'] = request.headers['Content-Type'] |
|
if 'x-api-key' in request.headers: |
|
headers_['x-api-key'] = request.headers['x-api-key'] |
|
|
|
|
|
|
|
|
|
if request.method == 'POST': |
|
response = requests.request( |
|
method='POST', |
|
url=relay_url, |
|
headers=headers_, |
|
json=json.loads(request.get_data()), |
|
allow_redirects=False |
|
) |
|
else: |
|
response = requests.request( |
|
method=request.method, |
|
url=relay_url, |
|
headers=headers_, |
|
allow_redirects=False |
|
) |
|
|
|
|
|
|
|
headers = [(name, value) for name, value in response.headers.items()] |
|
return Response(response.content, response.status_code, headers) |
|
|
|
if __name__ == '__main__': |
|
app.run(host='0.0.0.0', port=7860) |