make something clear

This commit is contained in:
pictures2333 2024-11-08 01:41:39 +08:00
parent ab47043281
commit af2d43e156
2 changed files with 21 additions and 24 deletions

43
app.py
View File

@ -1,18 +1,19 @@
from flask import Flask, session, request, redirect, Response from flask import Flask, request, Response
import os, requests, time import os, requests, time
from hashlib import sha256 from hashlib import sha256
from supaclient import supaClient, logger from supaclient import logger
from dotenv import load_dotenv from dotenv import load_dotenv
from supabase import Client from supabase import Client, create_client
load_dotenv() load_dotenv()
# supabase
URL = os.getenv("SUPABASE_IP") + "/" URL = os.getenv("SUPABASE_IP") + "/"
KEY = os.getenv("SUPABASE_KEY")
supabase = create_client(URL, KEY)
# app
app = Flask(__name__) app = Flask(__name__)
app.config["SECRET_KEY"] = os.urandom(64) app.config["SECRET_KEY"] = os.urandom(64)
app.shared_resource = supaClient()
# blueprints
# main # main
@app.route("/") @app.route("/")
@ -21,22 +22,17 @@ def index(): return "Hello, world!"
# reverse proxy # reverse proxy
@app.route('/r/<path:path>',methods=['GET', 'POST']) @app.route('/r/<path:path>',methods=['GET', 'POST'])
def proxy(path): def proxy(path):
if request.method=='GET': headers = request.headers
headers = request.headers
# process
if request.method == 'GET':
resp = requests.get(f'{URL}{path}', headers = headers) # forward 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': elif request.method=='POST':
# get items # get items
headers = request.headers
json_ctx = request.get_json() json_ctx = request.get_json()
# flags # flags
logger_args = [] logger_args = []
# edit request
# process
if path == "rest/v1/niming_posts": # niming post if path == "rest/v1/niming_posts": # niming post
# hash # hash
hash = sha256( (json_ctx["content"] + str(time.time())).encode() ).hexdigest() hash = sha256( (json_ctx["content"] + str(time.time())).encode() ).hexdigest()
@ -53,15 +49,16 @@ def proxy(path):
json_ctx["igid"] = igid json_ctx["igid"] = igid
# forward # forward
resp = requests.post(f'{URL}{path}',json=json_ctx, headers = headers) # forward resp = requests.post(f'{URL}{path}',json=json_ctx, headers = headers)
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)
# logger # logger
logger_res = logger(app.shared_resource.client, logger_args) logger_res = logger(supabase, logger_args)
return response # make response
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 # run
if __name__ == "__main__": if __name__ == "__main__":

View File

@ -15,7 +15,7 @@ class supaClient:
# logger # logger
def logger(dbclient: Client, args: list): def logger(dbclient: Client, args: list):
if args[0] == "newpost": if len(args) == 2 and args[0] == "newpost":
hash:str = args[1] hash:str = args[1]
pres = dbclient.table("niming_posts").select("id, hash").eq("hash", hash).execute() pres = dbclient.table("niming_posts").select("id, hash").eq("hash", hash).execute()
id = int(pres.data[0]["id"]) id = int(pres.data[0]["id"])