import os from flask import Flask from bcrypt import checkpw, gensalt, hashpw from sqlalchemy import create_engine from utils import setting_loader, logger, dbhelper from utils.pgclass import Base, SQLuser from utils.platform_consts import PLIST_ROOT # 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 # env PG_HOST = os.getenv("PG_HOST", None).strip() JWT_KEY = os.getenv("JWT_KEY", None) PLATFORM_ROOT_PASSWORD = os.getenv("PLATFORM_ROOT_PASSWORD", None) # env checker errmsg = [] if not JWT_KEY: errmsg.append("Invalid JWT_KEY") if not PLATFORM_ROOT_PASSWORD: errmsg.append("Invalid PLATFORM_ROOT_PASSWORD") if errmsg: print(f"[X] Env check failed: {errmsg}") exit(0) # settings checker settings = setting_loader.loadset() for s in settings: if not setting_loader.typechecker(s, settings.get(s)): print("[X] Settings.json data type check failed: %s"%(s)) exit(0) # Postgresql print("[*] Connecting to Database") dbhelper.db = dbhelper.DB(create_engine(PG_HOST)) Base.metadata.create_all(dbhelper.db._engine) # root checker 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(SQLuser).filter(SQLuser.user=="root").first() if root is None: # no root user session.add(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: # password / permission error session.delete(root) session.add(SQLuser(user="root",password=pwhash, permission=PLIST_ROOT)) session.commit() # flask app app = Flask(__name__) app.config["SECRET_KEY"] = os.urandom(64) # register blueprints # 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") # logger logger.logger("server.start", "Server is running") # index @app.route("/", methods = ["GET", "POST"]) def index(): return "Hello! World!
Shirakami Fubuki: cutest fox!!!" # app run if __name__ == "__main__": app.run(host="0.0.0.0", port=5000, debug=False)