Skip to content

Commit

Permalink
Dump configuration in development mode
Browse files Browse the repository at this point in the history
  • Loading branch information
C0nquistadore committed Sep 15, 2022
1 parent 8872757 commit 1c59268
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 2 deletions.
101 changes: 101 additions & 0 deletions src/PhoneBox.Server/Configuration/ConfigurationSerializer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Text.RegularExpressions;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Configuration.Json;

namespace PhoneBox.Server
{
internal static class ConfigurationSerializer
{
public static string DumpConfiguration(IConfigurationRoot root)
{
JsonObject jsonObject = new JsonObject();
DumpConfiguration(jsonObject, parentNode: null, parentKey: null, root.GetChildren(), root);
string json = jsonObject.ToJsonString(new JsonSerializerOptions { WriteIndented = true });
return json;
}
private static void DumpConfiguration(JsonNode node, JsonNode? parentNode, string? parentKey, IEnumerable<IConfigurationSection> children, IConfigurationRoot root)
{
foreach (IConfigurationSection child in children)
{
if (child.Key == "")
{
// :ASPNETCORE_BROWSER_TOOLS
// This key is treated as a section because it begins with :
continue;
}

ConfigurationValueResolutionResult result = ResolveConfigurationValue(root, child.Path, out string? value);
JsonNode childNode = DumpConfiguration(result, child.Key, parentKey, value, node, parentNode);
DumpConfiguration(childNode, node, child.Key, child.GetChildren(), root);
}
}
private static JsonNode DumpConfiguration(ConfigurationValueResolutionResult result, string key, string? parentKey, string? value, JsonNode node, JsonNode? parentNode)
{
switch (result)
{
case ConfigurationValueResolutionResult.NotInterested:
break;

case ConfigurationValueResolutionResult.NotFound:
{
JsonObject childNode = new JsonObject();
node[key] = childNode;
return childNode;
}

case ConfigurationValueResolutionResult.Found:
{
if (Int32.TryParse(key, out int intValue))
{
if (parentNode![parentKey!] is not JsonArray array)
{
array = new JsonArray();
parentNode![parentKey!] = array;
}
array.Insert(intValue, value!);
}
else
{
node[key] = value!;
}

break;
}

default:
throw new ArgumentOutOfRangeException(nameof(result), result, null);
}

return node;
}

private static ConfigurationValueResolutionResult ResolveConfigurationValue(IConfigurationRoot root, string key, out string? value)
{
foreach (IConfigurationProvider configurationProvider in root.Providers.Reverse())
{
if (!configurationProvider.TryGet(key, out value))
continue;

bool isAppSettingsJsonProvider = configurationProvider is JsonConfigurationProvider jsonConfigurationProvider && Regex.IsMatch(jsonConfigurationProvider.Source.Path, @"^appsettings(.+)?\.json$");
return isAppSettingsJsonProvider ? ConfigurationValueResolutionResult.Found : ConfigurationValueResolutionResult.NotInterested;
}

value = null;
return ConfigurationValueResolutionResult.NotFound;
}

private enum ConfigurationValueResolutionResult
{
None,
NotInterested,
NotFound,
Found

}
}
}
7 changes: 6 additions & 1 deletion src/PhoneBox.Server/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ internal static class Program
private static async Task Main(string[] args)
{
WebApplicationBuilder builder = WebApplication.CreateBuilder(args);
builder.Configuration.AddJsonFile($"appsettings.{Environment.MachineName}.json", optional: true);
builder.Configuration.AddJsonFile($"appsettings.{Environment.MachineName}.json", optional: true, reloadOnChange: true);
bool isDevelopment = builder.Environment.IsDevelopment();

IConfigurationSection authorizationConfiguration = builder.Configuration.GetSection("Authorization");
Expand Down Expand Up @@ -89,6 +89,11 @@ private static async Task Main(string[] args)
app.MapHub<TelephonyHub>()
.RequireAuthorization("HubConsumer");

if (isDevelopment)
{
app.MapGet("/configuration", () => ConfigurationSerializer.DumpConfiguration(builder.Configuration));
}

TelephonyConnectorRegistrar.ConfigureProvider(app);

await app.RunAsync().ConfigureAwait(false);
Expand Down
2 changes: 1 addition & 1 deletion src/PhoneBox.Server/Properties/launchSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "TelephonyHook/+4917612312312/+49123456789",
"launchUrl": "Configuration",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
Expand Down

0 comments on commit 1c59268

Please sign in to comment.