private static Task JavascriptAsync(HttpContext ctx, NFigSettingsWithStore <TSettings, TTier, TDataCenter> settingsWithStore) { if (ctx.Request.Method != "GET") { return(NotFoundAsync(ctx)); } return(JavascriptAsync(ctx, NFigUI.SettingsPanelScript)); }
/// <summary> /// Attempts to locate a <typeparamref name="TSettings"/> implementation that was registered /// by the <see cref="NFigApplicationBuilderExtensions.UseNFig{TSettings,TTier,TDataCenter}"/> method. /// </summary> /// <typeparam name="TSettings"></typeparam> /// <typeparam name="TTier"></typeparam> /// <typeparam name="TDataCenter"></typeparam> /// <param name="store"></param> /// <returns></returns> public static bool TryGet <TSettings, TTier, TDataCenter>(out NFigSettingsWithStore <TSettings, TTier, TDataCenter> store) where TSettings : class, INFigSettings <TTier, TDataCenter>, new() where TTier : struct, Enum where TDataCenter : struct, Enum { if (_knownStores.TryGetValue(typeof(TSettings), out var untypedStore)) { store = (NFigSettingsWithStore <TSettings, TTier, TDataCenter>)untypedStore; return(true); } store = default; return(false); }
private static Task JsonAsync(HttpContext ctx, NFigSettingsWithStore <TSettings, TTier, TDataCenter> settingsWithStore) { if (ctx.Request.Method != "GET") { return(NotFoundAsync(ctx)); } var json = settingsWithStore.Store.GetSettingsJson( settingsWithStore.Settings.ApplicationName, settingsWithStore.Settings.Tier, settingsWithStore.Settings.DataCenter, _dataCenterValues ); return(JsonAsync(ctx, json)); }
private static async Task ClearOverrideAsync(HttpContext ctx, NFigSettingsWithStore <TSettings, TTier, TDataCenter> settingsWithStore) { if (ctx.Request.Method != "POST") { await NotFoundAsync(ctx); return; } var settingData = await ReadJsonAsync <SettingData>(ctx.Request); if (string.IsNullOrEmpty(settingData.SettingName)) { await BadRequestAsync(ctx, "Invalid setting name specified"); return; } if (!Enum.IsDefined(typeof(TDataCenter), settingData.DataCenter)) { await BadRequestAsync(ctx, "Invalid data center specified"); return; } settingsWithStore.Store.ClearOverride( settingsWithStore.Settings.ApplicationName, settingData.SettingName, settingsWithStore.Settings.Tier, settingData.DataCenter ); var json = settingsWithStore.Store.GetSettingJson( settingsWithStore.Settings.ApplicationName, settingData.SettingName, settingsWithStore.Settings.Tier, settingsWithStore.Settings.DataCenter, _dataCenterValues ); await JsonAsync(ctx, json); }
private static Task IndexAsync(HttpContext ctx, NFigSettingsWithStore <TSettings, TTier, TDataCenter> settingsWithStore) { if (ctx.Request.Method != "GET") { return(NotFoundAsync(ctx)); } var options = ctx.RequestServices.GetRequiredService <IOptions <NFigOptions <TTier, TDataCenter> > >(); var tierColors = options.Value.TierColors.ToDictionary( x => x.Key.ToString(), x => $"#{x.Value.R:x2}{x.Value.B:x2}{x.Value.G:x2}" ); var html = _htmlTemplate .Replace("{{ApplicationName}}", settingsWithStore.Settings.ApplicationName) .Replace("{{Tier}}", settingsWithStore.Settings.Tier.ToString()) .Replace("{{DataCenter}}", settingsWithStore.Settings.DataCenter.ToString()) .Replace("{{Prefix}}", ctx.Request.Path.Value) .Replace("{{TierColor}}", tierColors[settingsWithStore.Settings.Tier.ToString()]) .Replace("{{TierColors}}", JsonSerializer.Serialize(tierColors)); return(HtmlAsync(ctx, html)); }
private static async Task SetOverrideAsync(HttpContext ctx, NFigSettingsWithStore <TSettings, TTier, TDataCenter> settingsWithStore) { if (ctx.Request.Method != "POST") { await NotFoundAsync(ctx); return; } var settingData = await ReadJsonAsync <SettingData>(ctx.Request); if (string.IsNullOrEmpty(settingData.SettingName)) { await BadRequestAsync(ctx, "Invalid setting name specified"); return; } if (!Enum.IsDefined(typeof(TDataCenter), settingData.DataCenter)) { await BadRequestAsync(ctx, "Invalid data center specified"); return; } var settingInfo = settingsWithStore.Store.GetSettingInfo( settingsWithStore.Settings.ApplicationName, settingData.SettingName ); if (!settingInfo.CanSetOverrideFor(settingsWithStore.Settings.Tier, settingData.DataCenter)) { await NotImplementedAsync(ctx, $"Setting {settingData.SettingName} does not allow overrides for Data Center {settingData.DataCenter}"); return; } if (!settingsWithStore.Store.IsValidStringForSetting(settingData.SettingName, settingData.Value)) { await ConflictAsync(ctx, $"\"{settingData.Value}\" is an invalid value for setting {settingData.SettingName}"); return; } settingsWithStore.Store.SetOverride( settingsWithStore.Settings.ApplicationName, settingData.SettingName, settingData.Value, settingsWithStore.Settings.Tier, settingData.DataCenter ); var json = settingsWithStore.Store.GetSettingJson( settingsWithStore.Settings.ApplicationName, settingData.SettingName, settingsWithStore.Settings.Tier, settingsWithStore.Settings.DataCenter, _dataCenterValues ); await JsonAsync(ctx, json); }