diff --git a/app.py b/app.py index 26fd98e..40aa6dc 100644 --- a/app.py +++ b/app.py @@ -1,18 +1,19 @@ -from flask import Flask, session, request, redirect, Response +from flask import Flask, request, Response import os, requests, time from hashlib import sha256 -from supaclient import supaClient, logger +from supaclient import logger from dotenv import load_dotenv -from supabase import Client +from supabase import Client, create_client load_dotenv() +# supabase URL = os.getenv("SUPABASE_IP") + "/" +KEY = os.getenv("SUPABASE_KEY") +supabase = create_client(URL, KEY) +# app app = Flask(__name__) app.config["SECRET_KEY"] = os.urandom(64) -app.shared_resource = supaClient() - -# blueprints # main @app.route("/") @@ -21,22 +22,17 @@ def index(): return "Hello, world!" # reverse proxy @app.route('/r/',methods=['GET', 'POST']) 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 - 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 + # edit request if path == "rest/v1/niming_posts": # niming post # hash hash = sha256( (json_ctx["content"] + str(time.time())).encode() ).hexdigest() @@ -53,15 +49,16 @@ def proxy(path): 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) + resp = requests.post(f'{URL}{path}',json=json_ctx, headers = headers) # logger - logger_res = logger(app.shared_resource.client, logger_args) - - return response + logger_res = logger(supabase, logger_args) + + # 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 if __name__ == "__main__": diff --git a/supaclient.py b/supaclient.py index 4d6c483..e10d880 100644 --- a/supaclient.py +++ b/supaclient.py @@ -15,7 +15,7 @@ class supaClient: # logger def logger(dbclient: Client, args: list): - if args[0] == "newpost": + if len(args) == 2 and args[0] == "newpost": hash:str = args[1] pres = dbclient.table("niming_posts").select("id, hash").eq("hash", hash).execute() id = int(pres.data[0]["id"])