niming_backend/app.py

77 lines
2.6 KiB
Python
Raw 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
from utils import pgclass, setting_loader, logger, dbhelper
from utils.platform_consts import PLIST_ROOT
2024-11-13 03:23:11 +08:00
from blueprints.article import article
2024-11-14 13:03:00 +08:00
from blueprints.log import log
from blueprints.admin import admin
2024-11-13 03:23:11 +08:00
2024-11-19 21:22:01 +08:00
# env
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 = []
2024-11-19 23:29:01 +08:00
if JWT_KEY is None or len(JWT_KEY) == 0:
2024-11-19 21:22:01 +08:00
errmsg.append("Invalid JWT_KEY")
2024-11-19 23:29:01 +08:00
if PLATFORM_ROOT_PASSWORD is None or len(PLATFORM_ROOT_PASSWORD) == 0:
2024-11-19 21:22:01 +08:00
errmsg.append("Invalid PLATFORM_ROOT_PASSWORD")
2024-11-18 02:47:25 +08:00
if len(errmsg):
print(f"Env check failed: {errmsg}")
exit(0)
2024-11-19 21:22:01 +08:00
# Postgresql
engine = create_engine('postgresql+psycopg2://%s:%s@%s:%s/%s'%(PG_USER, PG_PASS, PG_HOST, PG_PORT, PG_NAME))
pgclass.Base.metadata.create_all(engine)
dbhelper.db(engine)
2024-11-18 02:47:25 +08:00
# 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-11-18 02:47:25 +08:00
print("Settings.json data type check failed: %s"%s)
exit(0)
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:
root = session.query(pgclass.SQLuser).filter(pgclass.SQLuser.user=="root").first()
if root is None: # 沒有root
session.add(pgclass.SQLuser(user="root",password=pwhash, permission=PLIST_ROOT))
elif (not checkpw(PLATFORM_ROOT_PASSWORD.encode("utf-8"), root.password.encode("utf-8"))) or root.permission != PLIST_ROOT:
session.delete(root)
session.add(pgclass.SQLuser(user="root",password=pwhash, permission=PLIST_ROOT))
session.commit()
2024-11-13 03:23:11 +08:00
# flask app
app = Flask(__name__)
app.config["SECRET_KEY"] = os.urandom(64)
# register blueprints
app.register_blueprint(article, url_prefix = "/article")
2024-11-14 13:03:00 +08:00
app.register_blueprint(log , url_prefix = "/log")
app.register_blueprint(admin , url_prefix = "/admin")
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)
2024-11-25 21:51:50 +08:00
# 檢查ctx跟content的混用(英文單字)