diff --git a/src/backend/config/config.ini b/src/backend/config/config.ini deleted file mode 100644 index ea6646d..0000000 --- a/src/backend/config/config.ini +++ /dev/null @@ -1,18 +0,0 @@ -[frontend] -path = ../frontend -build = npm run build -build_path = ../public -show_output = true -motd = Memory: {{teal}}{{memory_used}} MB{{end}} / {{memory_total}} MB\nCPU: {{teal}}{{cpu_percent}}%%{{end}}\nUsers: {{teal}}{{active_users}}{{end}}\nHave a great day! - -[login] -disable_statuses = -widgets = header, version, motd, status, sysinfo - -[status] -show_output = true -disable_statuses = - -[security] -# Specify auth cookie expire time in seconds -auth_cookie_expire_time = 3600 diff --git a/src/backend/config/config.toml b/src/backend/config/config.toml new file mode 100644 index 0000000..5ae71de --- /dev/null +++ b/src/backend/config/config.toml @@ -0,0 +1,18 @@ +[frontend] +path = "../frontend" +build = "npm run build" +build_path = "../public" +show_output = true +motd = "Memory: {{teal}}{{memory_used}} MB{{end}} / {{memory_total}} MB\nCPU: {{teal}}{{cpu_percent}}%%{{end}}\nUsers: {{teal}}{{active_users}}{{end}}\nHave a great day!" + +[login] +disable_statuses = [] +widgets = ["header", "version", "motd", "status", "sysinfo"] + +[status] +show_output = true +disable_statuses = ["npm_doct", "version", "ncu", "npm_audit"] + +[security] +# Specify auth cookie expire time in seconds +auth_cookie_expire_time = 3600.0 diff --git a/src/backend/db/users.py b/src/backend/db/users.py index 89ef713..4b31599 100644 --- a/src/backend/db/users.py +++ b/src/backend/db/users.py @@ -19,9 +19,7 @@ def generate_uuid(self) -> str: return self._uuid def _generate_expire_time(self): - expire_time = float( - config.get('security').get('auth_cookie_expire_time') or 3600.0 - ) + expire_time = config.fetch().get('security').get('auth_cookie_expire_time') or 3600.0 self._expire_time = datetime.datetime.now() + datetime.timedelta( seconds=expire_time ) diff --git a/src/backend/server/build.py b/src/backend/server/build.py index b6d0a27..d065761 100644 --- a/src/backend/server/build.py +++ b/src/backend/server/build.py @@ -8,14 +8,14 @@ def _start_frontend_build() -> subprocess.Popen: - conf = config.get('frontend') + conf = config.fetch().get('frontend') return processes.add_subprocess( subprocess.Popen( conf.get('build').split(' '), cwd=conf.get('path'), stdout=( None - if conf.get('show_output').lower() == 'true' + if conf.get('show_output') else subprocess.DEVNULL ), shell=True, @@ -38,7 +38,7 @@ def build_frontend() -> int: def get_frontend_size() -> Tuple[float, str]: - root_directory = Path(config.get('frontend').get('build_path')) + root_directory = Path(config.fetch().get('frontend').get('build_path')) return _format_units( sum(f.stat().st_size for f in root_directory.glob('**/*') if f.is_file()) ) diff --git a/src/backend/server/main.py b/src/backend/server/main.py index 77c024b..81b145c 100644 --- a/src/backend/server/main.py +++ b/src/backend/server/main.py @@ -66,7 +66,7 @@ async def login_page() -> FileResponse: @app.get('/login/motd', response_class=PlainTextResponse) async def motd_text() -> PlainTextResponse: - return PlainTextResponse(motd.format_(config.get('frontend').get('motd'), database)) + return PlainTextResponse(motd.format_(config.fetch().get('frontend').get('motd'), database)) @app.post('/auth', response_class=PlainTextResponse) @@ -80,7 +80,7 @@ async def login(request: Request) -> PlainTextResponse: uuid = database.create_uuid(username) logging.info(f'Welcome user "{username}"!') - expire_time = float(config.get('security').get( + expire_time = float(config.fetch().get('security').get( 'auth_cookie_expire_time') or 3600.0) response.set_cookie( key='auth_cookie', diff --git a/src/backend/utils/config.py b/src/backend/utils/config.py index b64a1ca..ea1dc02 100644 --- a/src/backend/utils/config.py +++ b/src/backend/utils/config.py @@ -1,14 +1,32 @@ -from configparser import ConfigParser -from typing import Any, Optional +import tomllib +from typing import Any, Dict, Optional import logging -_config: ConfigParser = ConfigParser() + +class Wrapper: + def __init__(self) -> None: + self.config: Optional[Dict[Any, Any]] = None + + def set(self, config: Dict[Any, Any]) -> None: + self.config = config + + def get(self) -> Optional[Dict[Any, Any]]: + return self.config + + +_config: Wrapper = Wrapper() def load() -> None: - _config.read('config/config.ini') + with open("config/config.toml", "rb") as f: + _config.set(tomllib.load(f)) + f.close() logging.info('Config loaded') -def get(field: str) -> Optional[Any]: - return _config[field] +def fetch() -> Dict[Any, Any]: + dict_ = _config.get() + if dict_ is None: + logging.critical('Config used before being loaded') + return {} + return dict_ diff --git a/src/backend/utils/feature_build.py b/src/backend/utils/feature_build.py index a4f6626..9ded1e7 100644 --- a/src/backend/utils/feature_build.py +++ b/src/backend/utils/feature_build.py @@ -69,7 +69,7 @@ def _decode_feature_files(feature: str) -> Optional[List[str]]: if not filenames: logging.error(f'Non-existing feature: "{feature}"') return None - path = config.get('frontend').get('path') + path = config.fetch().get('frontend').get('path') return [f'{path}/{filename}' for filename in filenames] diff --git a/src/backend/utils/status.py b/src/backend/utils/status.py index 48c2b2a..755bae4 100644 --- a/src/backend/utils/status.py +++ b/src/backend/utils/status.py @@ -12,10 +12,10 @@ def get() -> Dict[str, str]: def update() -> None: - cwd = config.get('frontend').get('path') - stdout = None if config.get('status').get('show_output').lower() == 'true' else subprocess.DEVNULL - disabled = tuple(map(str.strip, config.get('status').get('disable_statuses').split(','))) - _login_disabled.extend(tuple(map(str.strip, config.get('login').get('disable_statuses').split(',')))) + cwd = config.fetch().get('frontend').get('path') + stdout = None if config.fetch().get('status').get('show_output') else subprocess.DEVNULL + disabled = tuple(map(str.strip, config.fetch().get('status').get('disable_statuses'))) + _login_disabled.extend(tuple(map(str.strip, config.fetch().get('login').get('disable_statuses')))) if 'npm_doct' not in disabled: _status['npm_doct'] = _run_npm_doctor(cwd, stdout)