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

45
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
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()
# flags
logger_args = []
# process # process
if request.method == 'GET':
resp = requests.get(f'{URL}{path}', headers = headers) # forward
elif request.method=='POST':
# get items
json_ctx = request.get_json()
# flags
logger_args = []
# edit request
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,14 +49,15 @@ 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)
# logger
logger_res = logger(supabase, logger_args)
# make response
excluded_headers = ['content-encoding', 'content-length', 'transfer-encoding', 'connection'] 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] 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) response = Response(resp.content, resp.status_code, headers)
# logger
logger_res = logger(app.shared_resource.client, logger_args)
return response return response
# run # run

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"])