2024-11-19 02:19:25 +08:00
|
|
|
from flask import Flask, jsonify
|
2024-11-13 03:23:11 +08:00
|
|
|
# from dotenv import load_dotenv
|
|
|
|
# load_dotenv()
|
2024-11-18 02:47:25 +08:00
|
|
|
import os, hashlib
|
2024-11-13 03:23:11 +08:00
|
|
|
from sqlalchemy import create_engine
|
2024-11-18 02:47:25 +08:00
|
|
|
from sqlalchemy.orm import sessionmaker
|
|
|
|
from utils.pgclass import Base, SQLuser
|
|
|
|
from utils.platform_consts import pList_root
|
|
|
|
from utils.setting_loader import loadset, typechecker
|
2024-11-19 02:19:25 +08:00
|
|
|
from utils import logger
|
|
|
|
from bcrypt import checkpw, gensalt, hashpw
|
2024-11-13 03:23:11 +08:00
|
|
|
# blueprints
|
|
|
|
from blueprints.article import article
|
2024-11-14 13:03:00 +08:00
|
|
|
from blueprints.log import log
|
2024-11-15 02:12:21 +08:00
|
|
|
from blueprints.admin import admin
|
2024-11-13 03:23:11 +08:00
|
|
|
|
|
|
|
# Global Variables
|
2024-11-18 02:47:25 +08:00
|
|
|
PG_HOST = os.getenv("PG_HOST", None)
|
|
|
|
PG_PORT = os.getenv("PG_PORT", None)
|
|
|
|
PG_NAME = os.getenv("PG_NAME", None)
|
|
|
|
PG_USER = os.getenv("PG_USER", None)
|
|
|
|
PG_PASS = os.getenv("PG_PASS", None)
|
|
|
|
JWT_KEY = os.getenv("JWT_KEY", None)
|
|
|
|
PLATFORM_ROOT_PASSWORD = os.getenv("PLATFORM_ROOT_PASSWORD", None)
|
|
|
|
|
|
|
|
# env checker
|
|
|
|
errmsg = []
|
|
|
|
if JWT_KEY is None or len(JWT_KEY) == 0: errmsg.append("Invalid JWT_KEY")
|
|
|
|
if PLATFORM_ROOT_PASSWORD is None or len(PLATFORM_ROOT_PASSWORD) == 0: errmsg.append("Invalid PLATFORM_ROOT_PASSWORD")
|
|
|
|
if len(errmsg):
|
|
|
|
print(f"Env check failed: {errmsg}")
|
|
|
|
exit(0)
|
|
|
|
|
|
|
|
# settings checker
|
|
|
|
settings = loadset()
|
|
|
|
for s in settings:
|
|
|
|
if not typechecker(s, settings.get(s)):
|
|
|
|
print("Settings.json data type check failed: %s"%s)
|
|
|
|
exit(0)
|
2024-11-13 03:23:11 +08:00
|
|
|
|
|
|
|
# Postgresql
|
|
|
|
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)
|
2024-11-18 02:47:25 +08:00
|
|
|
# root checker
|
2024-11-19 02:19:25 +08:00
|
|
|
pwhash = hashpw(PLATFORM_ROOT_PASSWORD.encode("utf-8"), gensalt()).decode("utf-8")
|
2024-11-18 02:47:25 +08:00
|
|
|
rootperm = pList_root
|
|
|
|
Session = sessionmaker(bind=engine)
|
|
|
|
session = Session()
|
|
|
|
root = session.query(SQLuser).filter(SQLuser.user=="root").first()
|
|
|
|
if (root is None):
|
|
|
|
session.add(SQLuser(user="root",password=pwhash, permission=rootperm))
|
2024-11-19 02:19:25 +08:00
|
|
|
elif ((not checkpw(PLATFORM_ROOT_PASSWORD.encode("utf-8"), root.password.encode("utf-8"))) or root.permission != rootperm):
|
2024-11-18 02:47:25 +08:00
|
|
|
session.delete(root)
|
|
|
|
session.add(SQLuser(user="root",password=pwhash, permission=rootperm))
|
|
|
|
session.commit()
|
|
|
|
session.close()
|
2024-11-13 03:23:11 +08:00
|
|
|
|
|
|
|
# shared class
|
|
|
|
class shared():
|
|
|
|
def __init__(self, engine):
|
|
|
|
self.engine = engine
|
|
|
|
sh = shared(engine)
|
|
|
|
|
|
|
|
# flask app
|
|
|
|
app = Flask(__name__)
|
|
|
|
app.config["SECRET_KEY"] = os.urandom(64)
|
|
|
|
app.shared_resource = sh
|
|
|
|
|
|
|
|
# register blueprints
|
|
|
|
app.register_blueprint(article, url_prefix = "/article")
|
2024-11-14 13:03:00 +08:00
|
|
|
app.register_blueprint(log , url_prefix = "/log")
|
2024-11-15 02:12:21 +08:00
|
|
|
app.register_blueprint(admin , url_prefix = "/admin")
|
2024-11-13 03:23:11 +08:00
|
|
|
|
2024-11-19 02:19:25 +08:00
|
|
|
# logger
|
|
|
|
logger.logger(engine, "server.start", "Server is running")
|
|
|
|
|
2024-11-13 03:23:11 +08:00
|
|
|
# index
|
|
|
|
@app.route("/", methods = ["GET", "POST"])
|
|
|
|
def index():
|
2024-11-13 21:20:21 +08:00
|
|
|
return "Hello! World!<br>Shirakami Fubuki: cutest fox!!!"
|
2024-11-13 03:23:11 +08:00
|
|
|
|
2024-11-19 02:19:25 +08:00
|
|
|
# global error handler
|
|
|
|
# @app.errorhandler(Exception)
|
|
|
|
# def handle_exception(e):
|
|
|
|
# return jsonify({"error": "Internal server error"}), 500
|
|
|
|
|
2024-11-13 03:23:11 +08:00
|
|
|
# app run
|
|
|
|
if __name__ == "__main__":
|
2024-11-12 21:12:23 +08:00
|
|
|
app.run(host="0.0.0.0", port=5000, debug=False)
|