import os from flask import Flask from bcrypt import checkpw, gensalt, hashpw from sqlalchemy import create_engine from utils import pgclass, setting_loader, logger, dbhelper from utils.platform_consts import PLIST_ROOT from blueprints.article import article from blueprints.log import log from blueprints.admin import admin # env 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) # 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) # settings checker settings = setting_loader.loadset() for s in settings: if not setting_loader.typechecker(s, settings.get(s)): print("Settings.json data type check failed: %s"%s) exit(0) # 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(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() # flask app app = Flask(__name__) app.config["SECRET_KEY"] = os.urandom(64) # register blueprints app.register_blueprint(article, url_prefix = "/article") app.register_blueprint(log , url_prefix = "/log") app.register_blueprint(admin , url_prefix = "/admin") # 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)