mirror of
https://github.com/WGDashboard/WGDashboard-PRW.git
synced 2026-12-01 17:07:41 +00:00
feat: try to build out
This commit is contained in:
+121
-50
@@ -1,70 +1,141 @@
|
||||
#!/bin/env python3
|
||||
|
||||
import logging as log
|
||||
from logging.config import dictConfig
|
||||
|
||||
import flask
|
||||
import json
|
||||
import os
|
||||
import secrets
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
import flask
|
||||
from sqlalchemy import create_engine
|
||||
from sqlalchemy.engine import Engine
|
||||
from sqlalchemy.orm import Session, sessionmaker
|
||||
|
||||
from modules.config.config import config
|
||||
from modules.config.config_utils import config_utils
|
||||
from modules.config.legacy_keys import normalize_config_keys
|
||||
from modules.database.database import database
|
||||
|
||||
from modules.utilities.utilities import utilities as util
|
||||
from modules.utilities.logger import setup_logger
|
||||
|
||||
from modules.routes.routes_general import routes_general
|
||||
from modules.routes.routes_welcome import routes_welcome
|
||||
from modules.utilities.logger import setup_logger
|
||||
|
||||
if __name__ == '__main__':
|
||||
# Read the config file (ini)
|
||||
ok, config_data = config.read()
|
||||
if not ok:
|
||||
log.error("failed to read the config from disk")
|
||||
exit(1)
|
||||
found, config_server = config.filter(config_data, 'SERVER')
|
||||
|
||||
# Configure the loglevel of WGDashboard
|
||||
wanted_loglevel = config_server.get('log_level', 'DEBUG').upper()
|
||||
setup_logger(wanted_loglevel)
|
||||
def _read_persistent_secret(secret_path: Path) -> str:
|
||||
'''Read the persistent session key, creating it once when needed.'''
|
||||
secret_path.parent.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
# Get the database configuration from thee config
|
||||
found, config_database = config.filter(config_data, 'DATABASE')
|
||||
if not found:
|
||||
exit(1)
|
||||
try:
|
||||
descriptor = os.open(
|
||||
secret_path,
|
||||
os.O_CREAT | os.O_EXCL | os.O_WRONLY,
|
||||
0o600,
|
||||
)
|
||||
except FileExistsError:
|
||||
secret_key = secret_path.read_text(encoding="utf-8").strip()
|
||||
if not secret_key:
|
||||
raise RuntimeError(f"persistent secret key is empty: {secret_path}")
|
||||
return secret_key
|
||||
|
||||
# Make the engine and create the infrastructure
|
||||
ok, engine, session = database.create_session(config_database)
|
||||
ok = database.ensure_contents(engine)
|
||||
secret_key = secrets.token_urlsafe(64)
|
||||
with os.fdopen(descriptor, "w", encoding="utf-8") as secret_file:
|
||||
secret_file.write(secret_key)
|
||||
secret_file.flush()
|
||||
os.fsync(secret_file.fileno())
|
||||
|
||||
prefix = config_server.get('app_prefix', '')
|
||||
return secret_key
|
||||
|
||||
# Configure the Flask app
|
||||
app = flask.Flask("WGDashboard",
|
||||
static_url_path=prefix,
|
||||
template_folder=os.path.abspath("./static/dist/WGDashboardAdmin"),
|
||||
static_folder=os.path.abspath("./static/dist/WGDashboardAdmin")
|
||||
|
||||
def create_app(
|
||||
config_data: dict[str, Any] | None = None,
|
||||
*,
|
||||
database_url: str | None = None,
|
||||
database_engine: Engine | None = None,
|
||||
secret_key_path: str | Path | None = None,
|
||||
) -> flask.Flask:
|
||||
'''Create and configure the WGDashboard Flask application.'''
|
||||
config_file_path: str | None = None
|
||||
if config_data is None:
|
||||
config_loaded, loaded_config = config.read()
|
||||
if not config_loaded:
|
||||
raise RuntimeError("failed to read the WGDashboard configuration")
|
||||
config_data = loaded_config
|
||||
|
||||
config_path_found, config_file_path = config_utils.search_known_paths()
|
||||
if not config_path_found:
|
||||
raise RuntimeError("failed to locate the WGDashboard configuration")
|
||||
|
||||
normalized_config = normalize_config_keys(config_data)
|
||||
server_config_found, server_config = config.filter(normalized_config, "SERVER")
|
||||
if not server_config_found:
|
||||
raise RuntimeError("the SERVER configuration section is missing")
|
||||
|
||||
setup_logger(str(server_config.get("log_level", "DEBUG")).upper())
|
||||
|
||||
prefix_value = server_config.get("app_prefix", "")
|
||||
prefix = str(prefix_value).rstrip("/")
|
||||
if prefix == "/":
|
||||
prefix = ""
|
||||
|
||||
web_directory = Path(__file__).parent / "web" / "dist" / "WGDashboardAdmin"
|
||||
app = flask.Flask(
|
||||
"WGDashboard",
|
||||
static_url_path=f"{prefix}/web",
|
||||
template_folder=str(web_directory),
|
||||
static_folder=str(web_directory),
|
||||
)
|
||||
app.config["APP_PREFIX"] = prefix
|
||||
app.config["WGD_CONFIG"] = normalized_config
|
||||
app.config["SESSION_COOKIE_HTTPONLY"] = True
|
||||
app.config["SESSION_COOKIE_SAMESITE"] = "Lax"
|
||||
|
||||
if secret_key_path is None:
|
||||
if config_file_path is not None:
|
||||
secret_path = Path(config_file_path).resolve().parent / "secret_key"
|
||||
else:
|
||||
secret_path = Path.home() / ".config" / "wgdashboard" / "secret_key"
|
||||
else:
|
||||
secret_path = Path(secret_key_path)
|
||||
app.secret_key = _read_persistent_secret(secret_path)
|
||||
|
||||
database_config_found, database_config = config.filter(normalized_config, "DATABASE")
|
||||
if (
|
||||
not database_config_found
|
||||
and database_url is None
|
||||
and database_engine is None
|
||||
):
|
||||
raise RuntimeError("the DATABASE configuration section is missing")
|
||||
|
||||
if database_url is not None and database_engine is not None:
|
||||
raise ValueError("provide either database_url or database_engine, not both")
|
||||
|
||||
if database_engine is not None:
|
||||
engine = database_engine
|
||||
db_session = sessionmaker(bind=engine, class_=Session)()
|
||||
elif database_url is None:
|
||||
session_created, engine, db_session = database.create_session(database_config)
|
||||
if not session_created or engine is None or db_session is None:
|
||||
raise RuntimeError("failed to initialize the database")
|
||||
else:
|
||||
engine = create_engine(database_url)
|
||||
db_session = sessionmaker(bind=engine, class_=Session)()
|
||||
|
||||
if not database.ensure_contents(engine):
|
||||
engine.dispose()
|
||||
raise RuntimeError("failed to create or verify database tables")
|
||||
|
||||
app.extensions["wgd_engine"] = engine
|
||||
app.extensions["wgd_db_session"] = db_session
|
||||
app.config["WGD_LOCALE_PATH"] = str(Path(__file__).parent / "web" / "locales")
|
||||
app.register_blueprint(routes_general, url_prefix=prefix)
|
||||
app.register_blueprint(routes_welcome, url_prefix=prefix)
|
||||
return app
|
||||
|
||||
app.wgd_config = config_data
|
||||
app.locale_path = './static/locales/'
|
||||
|
||||
app.secret_key = secrets.token_urlsafe(64)
|
||||
app.config['SESSION_TYPE'] = 'filesystem'
|
||||
|
||||
app.engine = engine
|
||||
app.db_session = session
|
||||
|
||||
debug_enabled = config_server.get('debug_enabled', False)
|
||||
hostname = config_server.get('hostname', '0.0.0.0')
|
||||
port = config_server.get('port', '10086')
|
||||
|
||||
app.run(
|
||||
debug=debug_enabled,
|
||||
host=hostname,
|
||||
port=port,
|
||||
use_reloader=False
|
||||
)
|
||||
if __name__ == "__main__":
|
||||
application = create_app()
|
||||
server_settings = application.config["WGD_CONFIG"]["server"]
|
||||
application.run(
|
||||
debug=server_settings.get("debug_enabled", False),
|
||||
host=server_settings.get("hostname", "0.0.0.0"),
|
||||
port=int(server_settings.get("port", 10086)),
|
||||
use_reloader=False,
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user