namespace DigitalData.Core.API; /// /// Provides extension methods for adding middleware to the application's request pipeline. /// public static class DIExtensions { /// /// Adds the to the application's request pipeline to include /// Content Security Policy (CSP) headers in the HTTP response. /// /// The application builder. /// /// The CSP policy string with placeholders. The first format parameter {0} will be replaced /// by the nonce value. /// /// The application builder with the CSP middleware added. public static IApplicationBuilder UseCSPMiddleware(this IApplicationBuilder app, string policy) => app.UseMiddleware(policy); /// /// Checks if the DiP (Development in Production) mode is enabled for the WebApplicationBuilder. /// /// The WebApplicationBuilder instance. /// True if DiP mode is enabled; otherwise, false. public static bool IsDiP(this WebApplicationBuilder builder) => builder.Configuration.GetValue("DiPMode"); /// /// Checks if the DiP (Development in Production) mode is enabled for the WebApplication. /// /// The WebApplication instance. /// True if DiP mode is enabled; otherwise, false. public static bool IsDiP(this WebApplication app) => app.Configuration.GetValue("DiPMode"); /// /// Checks if the environment is Development or DiP (Development in Production) mode is enabled for the WebApplicationBuilder. /// /// The WebApplicationBuilder instance. /// True if the environment is Development or DiP mode is enabled; otherwise, false. public static bool IsDevOrDiP(this WebApplicationBuilder builder) => builder.Environment.IsDevelopment() || builder.IsDiP(); /// /// Checks if the environment is Development or DiP (Development in Production) mode is enabled for the WebApplication. /// /// The WebApplication instance. /// True if the environment is Development or DiP mode is enabled; otherwise, false. public static bool IsDevOrDiP(this WebApplication app) => app.Environment.IsDevelopment() || app.IsDiP(); /// /// Configures the services with options from the specified section of the appsettings.json file. /// /// The options class type. Must be a reference type. /// The WebApplicationBuilder instance. /// The WebApplicationBuilder instance for chaining. /// Thrown if the section is not found in the configuration. public static WebApplicationBuilder ConfigureBySection(this WebApplicationBuilder builder) where T : class { var section = builder.Configuration.GetSection(typeof(T).Name); if (!section.Exists()) throw new InvalidOperationException($"Section '{typeof(T).Name}' not found in appsettings."); builder.Services.Configure(section); return builder; } }