rev proxy

This commit is contained in:
pictures2333 2024-11-07 16:55:35 +08:00
parent 7e3a130db1
commit 3f2f7475f2
4 changed files with 46 additions and 77 deletions

50
app.py
View File

@ -1,14 +1,56 @@
from flask import Flask, session, request, redirect
import os
from blueprints.article import article
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
app.register_blueprint(article, url_prefix = '/api/article')
# 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__":

View File

View File

@ -1,73 +0,0 @@
# /api/article
# 負責公開範圍內的資料上傳與存取
from flask import Blueprint, current_app, request
from supabase import Client
from hashlib import sha256
import time, math
article = Blueprint('article', __name__)
# listing - public
# /api/article/list?page=&count=
@article.route("/list", methods = ["GET"])
def list():
page = int(request.args.get("page"))
count = int(request.args.get("count"))
client: Client = current_app.shared_resource.client
response = client.table("niming_posts").select("id, content, igid, created_at").order("id", desc = True).range(start=page*count, end=(page+1)*count-1).execute()
return response.data
# fetching article - public
# /api/article/get?id=
@article.route("/get", methods = ["GET"])
def get():
id = str(int(request.args.get("id")))
client: Client = current_app.shared_resource.client
response = client.table("niming_posts").select("id, content, igid, created_at").eq("id", id).execute()
return response.data
# new post - public
@article.route("/post", methods = ["POST"])
def post():
# variables
ctx = request.form.get("ctx")
if ctx is None: return "No content"
else: ctx = str(ctx)
# future - images
# future - videos
# IG API Part
# hash
t = str(math.floor(time.time()*10000000))
hash = sha256( ( ctx + t ).encode() ).hexdigest()
# Supabase
client: Client = current_app.shared_resource.client
response = (client.table("niming_posts").insert({
"content": ctx,
"hash": hash
}).execute())
# log
return response.data
# 發文者專用 - 發文者有義務記住並保護好自己發的文章的sha256
# fetching article - article owner
@article.route("/owner/get", methods = ["GET"])
def owner_list():
hash = str(request.args.get("hash"))
client: Client = current_app.shared_resource.client
response = client.table("niming_posts").select("id, content, igid, created_at, hash").eq("hash", hash).execute()
return response.data
# delete article - article owner
# Coming Soon...

View File