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-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
|
2024-12-17 01:08:35 +08:00
|
|
|
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-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")
|
|
|
|
dbhelper.db = dbhelper.DB(create_engine('postgresql+psycopg2://%s:%s@%s:%s/%s'%(PG_USER, PG_PASS, PG_HOST, PG_PORT, PG_NAME)))
|
|
|
|
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
|
|
|
|
app.register_blueprint(article, url_prefix = "/article")
|
2024-11-14 13:03:00 +08:00
|
|
|
app.register_blueprint(log , url_prefix = "/log")
|
2024-12-17 01:08:35 +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
|
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)
|