private void LoadLambdaConfigurationFromEnvironmentVariables()

in Tools/LambdaTestTool-v2/src/Amazon.Lambda.TestTool/Services/ApiGatewayRouteConfigService.cs [37:116]


    private void LoadLambdaConfigurationFromEnvironmentVariables()
    {
        _logger.LogDebug("Retrieving all environment variables");
        var environmentVariables = _environmentManager.GetEnvironmentVariables();

        _logger.LogDebug("Looping over the retrieved environment variables");
        foreach (DictionaryEntry entry in environmentVariables)
        {
            var key = entry.Key.ToString();
            if (key is null)
                continue;
            _logger.LogDebug("Environment variables: {VariableName}", key);
            if (!(key.Equals(Constants.LambdaConfigEnvironmentVariablePrefix) ||
                key.StartsWith($"{Constants.LambdaConfigEnvironmentVariablePrefix}_")))
            {
                _logger.LogDebug("Skipping environment variable: {VariableName}", key);
                continue;
            }

            var jsonValue = entry.Value?.ToString()?.Trim();
            _logger.LogDebug("Environment variable value: {VariableValue}", jsonValue);
            if (string.IsNullOrEmpty(jsonValue))
                continue;

            try
            {
                if (jsonValue.StartsWith('['))
                {
                    _logger.LogDebug("Environment variable value starts with '['. Attempting to deserialize as a List.");
                    var configs = JsonSerializer.Deserialize<List<ApiGatewayRouteConfig>>(jsonValue);
                    if (configs != null)
                    {
                        foreach (var config in configs)
                        {
                            if (IsRouteConfigValid(config))
                            {
                                _routeConfigs.Add(config);
                                _logger.LogDebug("Environment variable deserialized and added to the existing configuration.");
                            }
                            else
                            {
                                _logger.LogError("The route config {Method} {Path} is not valid. It will be skipped.", config.HttpMethod, config.Path);
                            }
                        }
                        _logger.LogDebug("Environment variable deserialized and added to the existing configuration.");
                    }
                    else
                    {
                        _logger.LogError("Environment variable was not properly deserialized and will be skipped.");
                    }
                }
                else
                {
                    _logger.LogDebug("Environment variable value does not start with '['.");
                    var config = JsonSerializer.Deserialize<ApiGatewayRouteConfig>(jsonValue);
                    if (config != null)
                    {
                        if (IsRouteConfigValid(config))
                        {
                            _routeConfigs.Add(config);
                            _logger.LogDebug("Environment variable deserialized and added to the existing configuration.");
                        }
                        else
                        {
                            _logger.LogError("The route config {Method} {Path} is not valid. It will be skipped.", config.HttpMethod, config.Path);
                        }
                    }
                    else
                    {
                        _logger.LogError("Environment variable was not properly deserialized and will be skipped.");
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Error deserializing environment variable {key}: {ex.Message}");
                _logger.LogError("Error deserializing environment variable {Key}: {Message}", key, ex.Message);
            }
        }
    }