niming_backend/app.py

96 lines
3.0 KiB
Python
Raw Permalink Normal View History

2024-11-19 21:22:01 +08:00
import os
2024-11-19 23:29:01 +08:00
from flask import Flask
2024-11-19 02:19:25 +08:00
from bcrypt import checkpw, gensalt, hashpw
2024-11-19 21:22:01 +08:00
from sqlalchemy import create_engine
2024-12-09 03:24:22 +08:00
from utils import setting_loader, logger, dbhelper
from utils.pgclass import Base, SQLuser
2024-11-19 21:22:01 +08:00
from utils.platform_consts import PLIST_ROOT
2024-12-23 02:03:55 +08:00
# import blueprints
# admin
from blueprints.admin.article import bl_admin_article
from blueprints.admin.ig import bl_admin_ig
from blueprints.admin.setting import bl_admin_setting
from blueprints.admin.user import bl_admin_user
# owner
from blueprints.owner.owner_article import bl_owner_article
# general
from blueprints.general.article import bl_article
from blueprints.general.comment import bl_comment
# misc
from blueprints.misc.log import bl_log
2024-11-13 03:23:11 +08:00
2024-11-19 21:22:01 +08:00
# env
2024-12-18 03:07:56 +08:00
PG_HOST = os.getenv("PG_HOST", None).strip()
2024-11-18 02:47:25 +08:00
JWT_KEY = os.getenv("JWT_KEY", None)
PLATFORM_ROOT_PASSWORD = os.getenv("PLATFORM_ROOT_PASSWORD", None)
# env checker
errmsg = []
2024-12-12 11:30:51 +08:00
if not JWT_KEY:
2024-11-19 21:22:01 +08:00
errmsg.append("Invalid JWT_KEY")
2024-12-12 11:30:51 +08:00
if not PLATFORM_ROOT_PASSWORD:
2024-11-19 21:22:01 +08:00
errmsg.append("Invalid PLATFORM_ROOT_PASSWORD")
2024-12-12 11:30:51 +08:00
if errmsg:
print(f"[X] Env check failed: {errmsg}")
2024-11-18 02:47:25 +08:00
exit(0)
# settings checker
2024-11-19 21:22:01 +08:00
settings = setting_loader.loadset()
2024-11-18 02:47:25 +08:00
for s in settings:
2024-11-19 21:22:01 +08:00
if not setting_loader.typechecker(s, settings.get(s)):
2024-12-12 11:30:51 +08:00
print("[X] Settings.json data type check failed: %s"%(s))
2024-11-18 02:47:25 +08:00
exit(0)
2024-12-12 11:30:51 +08:00
# Postgresql
print("[*] Connecting to Database")
2024-12-18 03:07:56 +08:00
dbhelper.db = dbhelper.DB(create_engine(PG_HOST))
2024-12-12 11:30:51 +08:00
Base.metadata.create_all(dbhelper.db._engine)
2024-11-13 03:23:11 +08:00
2024-11-18 02:47:25 +08:00
# root checker
2024-11-19 21:22:01 +08:00
pwhash = hashpw(PLATFORM_ROOT_PASSWORD.encode("utf-8"), gensalt()).decode("utf-8") # if needed, new password
with dbhelper.db.getsession() as session:
2024-12-09 03:24:22 +08:00
root = session.query(SQLuser).filter(SQLuser.user=="root").first()
2024-12-12 11:30:51 +08:00
if root is None:
# no root user
2024-12-09 03:24:22 +08:00
session.add(SQLuser(user="root",password=pwhash, permission=PLIST_ROOT))
2024-11-19 21:22:01 +08:00
elif (not checkpw(PLATFORM_ROOT_PASSWORD.encode("utf-8"), root.password.encode("utf-8"))) or root.permission != PLIST_ROOT:
2024-12-12 11:30:51 +08:00
# password / permission error
2024-11-19 21:22:01 +08:00
session.delete(root)
2024-12-09 03:24:22 +08:00
session.add(SQLuser(user="root",password=pwhash, permission=PLIST_ROOT))
2024-11-19 21:22:01 +08:00
session.commit()
2024-11-13 03:23:11 +08:00
# flask app
app = Flask(__name__)
app.config["SECRET_KEY"] = os.urandom(64)
# register blueprints
2024-12-23 02:03:55 +08:00
# admin
app.register_blueprint(bl_admin_ig, url_prefix="/admin/ig")
app.register_blueprint(bl_admin_setting, url_prefix="/admin/setting")
app.register_blueprint(bl_admin_user, url_prefix="/admin/user")
app.register_blueprint(bl_admin_article, url_prefix="/admin")
# owner
app.register_blueprint(bl_owner_article, url_prefix="/owner")
# general
app.register_blueprint(bl_article, url_prefix="/article")
app.register_blueprint(bl_comment, url_prefix="/comment")
# misc
app.register_blueprint(bl_log, url_prefix = "/log")
2024-11-13 03:23:11 +08:00
2024-11-19 02:19:25 +08:00
# logger
2024-11-19 21:22:01 +08:00
logger.logger("server.start", "Server is running")
2024-11-19 02:19:25 +08:00
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
# app run
if __name__ == "__main__":
2024-11-19 23:29:01 +08:00
app.run(host="0.0.0.0", port=5000, debug=False)