niming_backend/app.py
pictures2333 3f2f7475f2 rev proxy
2024-11-07 16:55:35 +08:00

57 lines
1.9 KiB
Python

from flask import Flask, session, request, redirect, Response
import os, requests, time
from hashlib import sha256
from supaclient import supaClient
from dotenv import load_dotenv
load_dotenv()
URL = os.getenv("SUPABASE_IP") + "/"
app = Flask(__name__)
app.config["SECRET_KEY"] = os.urandom(64)
app.shared_resource = supaClient()
# blueprints
# main
@app.route("/")
def index(): return "Hello, world!"
# reverse proxy
@app.route('/r/<path:path>',methods=['GET', 'POST'])
def proxy(path):
if request.method=='GET':
headers = request.headers
resp = requests.get(f'{URL}{path}', headers = headers) # forward
excluded_headers = ['content-encoding', 'content-length', 'transfer-encoding', 'connection']
headers = [(name, value) for (name, value) in resp.raw.headers.items() if name.lower() not in excluded_headers]
response = Response(resp.content, resp.status_code, headers)
return response
elif request.method=='POST':
# get items
headers = request.headers
json_ctx = request.get_json()
# process
if path == "rest/v1/niming_posts": # niming post
# hash
hash = sha256( (json_ctx["content"] + str(time.time())).encode() ).hexdigest()
# ig posting
igid = None
# edit payload
json_ctx["hash"] = hash
json_ctx["igid"] = igid
# forward
resp = requests.post(f'{URL}{path}',json=json_ctx, headers = headers) # forward
excluded_headers = ['content-encoding', 'content-length', 'transfer-encoding', 'connection']
headers = [(name, value) for (name, value) in resp.raw.headers.items() if name.lower() not in excluded_headers]
response = Response(resp.content, resp.status_code, headers)
return response
# run
if __name__ == "__main__":
app.run(host = "0.0.0.0", port = 8000, debug = False)