dev box & FBK so cutegit add .

This commit is contained in:
pictures2333 2024-11-12 19:23:11 +00:00
parent f2fbba44fe
commit dc98b7491f
5 changed files with 173 additions and 173 deletions

4
.gitignore vendored
View File

@ -1,2 +1,2 @@
__pycache__ __pycache__
.env *.env

86
app.py
View File

@ -1,44 +1,44 @@
from flask import Flask from flask import Flask
from dotenv import load_dotenv # from dotenv import load_dotenv
load_dotenv() # load_dotenv()
import os import os
from sqlalchemy import create_engine from sqlalchemy import create_engine
from pgclass import Base, SQLarticle from pgclass import Base, SQLarticle
# blueprints # blueprints
from blueprints.article import article from blueprints.article import article
# Global Variables # Global Variables
PG_HOST = os.getenv("PG_HOST") PG_HOST = os.getenv("PG_HOST")
PG_PORT = os.getenv("PG_PORT") PG_PORT = os.getenv("PG_PORT")
PG_NAME = os.getenv("PG_NAME") PG_NAME = os.getenv("PG_NAME")
PG_USER = os.getenv("PG_USER") PG_USER = os.getenv("PG_USER")
PG_PASS = os.getenv("PG_PASS") PG_PASS = os.getenv("PG_PASS")
JWT_KEY = os.getenv("JWT_KEY") JWT_KEY = os.getenv("JWT_KEY")
# Postgresql # Postgresql
engine = create_engine('postgresql+psycopg2://%s:%s@%s:%s/%s'%(PG_USER, PG_PASS, PG_HOST, PG_PORT, PG_NAME)) engine = create_engine('postgresql+psycopg2://%s:%s@%s:%s/%s'%(PG_USER, PG_PASS, PG_HOST, PG_PORT, PG_NAME))
Base.metadata.create_all(engine) Base.metadata.create_all(engine)
# shared class # shared class
class shared(): class shared():
def __init__(self, engine): def __init__(self, engine):
self.engine = engine self.engine = engine
self.SQLarticle = SQLarticle self.SQLarticle = SQLarticle
sh = shared(engine) sh = shared(engine)
# flask app # flask app
app = Flask(__name__) app = Flask(__name__)
app.config["SECRET_KEY"] = os.urandom(64) app.config["SECRET_KEY"] = os.urandom(64)
app.shared_resource = sh app.shared_resource = sh
# register blueprints # register blueprints
app.register_blueprint(article, url_prefix = "/article") app.register_blueprint(article, url_prefix = "/article")
# index # index
@app.route("/", methods = ["GET", "POST"]) @app.route("/", methods = ["GET", "POST"])
def index(): def index():
return "Hello, World!" return "Hello, World!"
# app run # app run
if __name__ == "__main__": if __name__ == "__main__":
app.run(host="0.0.0.0", port=5000, debug=False) app.run(host="0.0.0.0", port=5000, debug=False)

View File

@ -1,110 +1,110 @@
from flask import Blueprint, current_app, request, jsonify from flask import Blueprint, current_app, request, jsonify
import hashlib import hashlib
import time import time
from sqlalchemy.orm import sessionmaker from sqlalchemy.orm import sessionmaker
from sqlalchemy import desc from sqlalchemy import desc
""" """
TODO: TODO:
- Image & Video - Image & Video
- IG post - IG post
- LOG - LOG
""" """
article = Blueprint('article', __name__) article = Blueprint('article', __name__)
# 匿名文列表 # 匿名文列表
@article.route('/list', methods = ["GET"]) @article.route('/list', methods = ["GET"])
def listing(): def listing():
# variables # variables
rst = int(request.args.get("start")) rst = int(request.args.get("start"))
count = int(request.args.get("count")) count = int(request.args.get("count"))
# db # db
db = current_app.shared_resource.engine db = current_app.shared_resource.engine
Session = sessionmaker(bind=db) Session = sessionmaker(bind=db)
session = Session() session = Session()
# get ctx # get ctx
table = current_app.shared_resource.SQLarticle table = current_app.shared_resource.SQLarticle
res = session.query(table.id, table.ctx, table.igid, table.created_at, table.mark).order_by(desc(table.id)).filter(table.mark == 'visible').offset(rst).limit(count).all() res = session.query(table.id, table.ctx, table.igid, table.created_at, table.mark).order_by(desc(table.id)).filter(table.mark == 'visible').offset(rst).limit(count).all()
# mapping # mapping
res = [ {"id":r[0], "ctx":r[1], "igid":r[2], "created_at":r[3], "mark":r[4]} for r in res ] res = [ {"id":r[0], "ctx":r[1], "igid":r[2], "created_at":r[3], "mark":r[4]} for r in res ]
return jsonify(res) return jsonify(res)
# 獲取指定文章 # 獲取指定文章
@article.route("/get/<int:id>", methods = ["GET"]) @article.route("/get/<int:id>", methods = ["GET"])
def getarticle(id:int): def getarticle(id:int):
db = current_app.shared_resource.engine db = current_app.shared_resource.engine
Session = sessionmaker(bind=db) Session = sessionmaker(bind=db)
session = Session() session = Session()
# get ctx # get ctx
table = current_app.shared_resource.SQLarticle table = current_app.shared_resource.SQLarticle
res = session.query(table.id, table.ctx, table.igid, table.created_at, table.mark).filter(table.id == id).filter(table.mark == 'visible').all() res = session.query(table.id, table.ctx, table.igid, table.created_at, table.mark).filter(table.id == id).filter(table.mark == 'visible').all()
# mapping # mapping
res = [ {"id":r[0], "ctx":r[1], "igid":r[2], "created_at":r[3], "mark":r[4]} for r in res ] res = [ {"id":r[0], "ctx":r[1], "igid":r[2], "created_at":r[3], "mark":r[4]} for r in res ]
return jsonify(res) return jsonify(res)
# 上傳文章 # 上傳文章
@article.route("/post", methods = ["POST"]) @article.route("/post", methods = ["POST"])
def posting(): def posting():
db = current_app.shared_resource.engine db = current_app.shared_resource.engine
Session = sessionmaker(bind=db) Session = sessionmaker(bind=db)
session = Session() session = Session()
# content # content
ctx = str(request.json["ctx"]) ctx = str(request.json["ctx"])
if ctx is None: return "No brain no content" if ctx is None: return "No brain no content"
# hash # hash
seed = ctx + str(time.time()) seed = ctx + str(time.time())
hash = hashlib.sha256(seed.encode()).hexdigest() hash = hashlib.sha256(seed.encode()).hexdigest()
# file processing # file processing
# ig posting # ig posting
igid = None igid = None
# mark # mark
mark = "visible" mark = "visible"
# pg commit # pg commit
table = current_app.shared_resource.SQLarticle table = current_app.shared_resource.SQLarticle
data = table(hash = hash, ctx = ctx, igid = igid, mark = mark) data = table(hash = hash, ctx = ctx, igid = igid, mark = mark)
session.add(data) session.add(data)
session.commit() session.commit()
# pg getdata # pg getdata
res = session.query(table.id, table.ctx, table.igid, table.created_at, table.mark).filter(table.hash == hash).all() res = session.query(table.id, table.ctx, table.igid, table.created_at, table.mark).filter(table.hash == hash).all()
res = [ {"id":r[0], "ctx":r[1], "igid":r[2], "created_at":r[3], "mark":r[4]} for r in res ] res = [ {"id":r[0], "ctx":r[1], "igid":r[2], "created_at":r[3], "mark":r[4]} for r in res ]
session.close() session.close()
return jsonify(res) return jsonify(res)
# 只有發文者可以看到的獲取指定文章 # 只有發文者可以看到的獲取指定文章
# 只有發文者可以做到的刪除文章 # 只有發文者可以做到的刪除文章
@article.route("/own/<sha256>", methods = ["GET", "DELETE"]) @article.route("/own/<sha256>", methods = ["GET", "DELETE"])
def owner_getarticle(sha256:str): def owner_getarticle(sha256:str):
db = current_app.shared_resource.engine db = current_app.shared_resource.engine
Session = sessionmaker(bind=db) Session = sessionmaker(bind=db)
session = Session() session = Session()
table = current_app.shared_resource.SQLarticle table = current_app.shared_resource.SQLarticle
# 獲取指定文章 # 獲取指定文章
if request.method == "GET": if request.method == "GET":
res = session.query(table.id, table.ctx, table.igid, table.created_at, table.mark, table.hash).filter(table.hash == sha256).all() res = session.query(table.id, table.ctx, table.igid, table.created_at, table.mark, table.hash).filter(table.hash == sha256).all()
res = [ {"id":r[0], "ctx":r[1], "igid":r[2], "created_at":r[3], "mark":r[4], "hash":r[5]} for r in res ] res = [ {"id":r[0], "ctx":r[1], "igid":r[2], "created_at":r[3], "mark":r[4], "hash":r[5]} for r in res ]
return jsonify(res) return jsonify(res)
# 刪除指定文章 # 刪除指定文章
elif request.method == "DELETE": elif request.method == "DELETE":
res = session.query(table).filter(table.hash == sha256).first() res = session.query(table).filter(table.hash == sha256).first()
session.delete(res) session.delete(res)
session.commit() session.commit()
return "OK", 200 return "OK", 200
session.close() session.close()

View File

@ -1,17 +1,17 @@
from sqlalchemy import Column, Integer, String, TIMESTAMP, func from sqlalchemy import Column, Integer, String, TIMESTAMP, func
from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base() Base = declarative_base()
class SQLarticle(Base): class SQLarticle(Base):
__tablename__ = 'posts' __tablename__ = 'posts'
id = Column(Integer, primary_key=True) id = Column(Integer, primary_key=True)
created_at = Column(TIMESTAMP(timezone=True), server_default=func.now()) created_at = Column(TIMESTAMP(timezone=True), server_default=func.now())
hash = Column(String) hash = Column(String)
ctx = Column(String) ctx = Column(String)
igid = Column(String) igid = Column(String)
mark = Column(String) mark = Column(String)
def __repr__(self): def __repr__(self):
return f"<article(id={self.id}, hash={self.hash}, ctx={self.ctx}, igid={self.igid}, mark={self.mark}, created_at={self.created_at})>" return f"<article(id={self.id}, hash={self.hash}, ctx={self.ctx}, igid={self.igid}, mark={self.mark}, created_at={self.created_at})>"

View File

@ -1,4 +1,4 @@
sqlalchemy sqlalchemy
flask flask
pyjwt pyjwt
psycopg2 psycopg2