diff --git a/backend/config/config_manager.py b/backend/config/config_manager.py index 92f89b6bc..1c17ad854 100644 --- a/backend/config/config_manager.py +++ b/backend/config/config_manager.py @@ -178,9 +178,7 @@ class ConfigManager: # Also check if the config file is writable self._config_file_writable = os.access(self.config_file, os.W_OK) except FileNotFoundError: - log.critical( - "Config file not found! Any changes made to the configuration will not persist after the application restarts." - ) + self._create_missing_config_file() except PermissionError: log.warning( "Config file not writable! Any changes made to the configuration will not persist after the application restarts." @@ -190,6 +188,24 @@ class ConfigManager: self._parse_config() self._validate_config() + def _create_missing_config_file(self) -> None: + log.warning(f"Config file not found, creating an empty config at {hl(self.config_file, BLUE)}") + + try: + os.makedirs(os.path.dirname(self.config_file), exist_ok=True) + with open(self.config_file, "x"): + pass + + self._config_file_mounted = True + self._config_file_writable = os.access(self.config_file, os.W_OK) + except FileExistsError: + self._config_file_mounted = True + self._config_file_writable = os.access(self.config_file, os.W_OK) + except PermissionError: + log.critical( + "Config file not found and could not be created! Any changes made to the configuration will not persist after the application restarts." + ) + @staticmethod def get_db_engine() -> URL: """Builds the database connection string using environment variables diff --git a/backend/tests/config/test_config_loader.py b/backend/tests/config/test_config_loader.py index 37c29cfa4..faa9c8d10 100644 --- a/backend/tests/config/test_config_loader.py +++ b/backend/tests/config/test_config_loader.py @@ -113,3 +113,14 @@ def test_empty_config_loader(): assert loader.config.EJS_CONTROLS == {} assert loader.config.GAMELIST_MEDIA_THUMBNAIL == "box2d" assert loader.config.GAMELIST_MEDIA_IMAGE == "screenshot" + + +def test_missing_config_file_is_created(tmp_path): + config_file = tmp_path / "config" / "config.yml" + + loader = ConfigManager(str(config_file)) + + assert config_file.exists() + assert config_file.read_text() == "" + assert loader.config.CONFIG_FILE_MOUNTED + assert loader.config.CONFIG_FILE_WRITABLE