diff --git a/src/Scaffolding/Api.cs b/src/Scaffolding/Api.cs index 364a870..50841ed 100644 --- a/src/Scaffolding/Api.cs +++ b/src/Scaffolding/Api.cs @@ -20,26 +20,33 @@ namespace Scaffolding; public static class Api { - public static WebApplicationBuilder Initialize(string[] args = null, string customUrls = null, params Assembly[] executingAssemblies) + public static WebApplicationBuilder Initialize(DefaultApiSettings defaultApiSettings, string[] args = null, string customUrls = null, params Assembly[] executingAssemblies) { + if (defaultApiSettings == null) + { + throw new Exception("Default api settings is required."); + } + var builder = WebApplication.CreateBuilder(args); + builder.Configuration + .SetBasePath(Directory.GetCurrentDirectory()) + .AddJsonFile($"appsettings.json", optional: true, reloadOnChange: true) + .AddJsonFile($"appsettings.{EnvironmentUtility.GetCurrentEnvironment()}.json", optional: true, reloadOnChange: true) + .AddEnvironmentVariables(defaultApiSettings.EnvironmentVariablesPrefix); + var apiSettings = builder.Configuration.GetSection("ApiSettings").Get(); if (apiSettings == null) { throw new Exception("'ApiSettings' section in the appsettings.json is required."); } - Console.WriteLine("==== Api Settings ===="); + apiSettings.EnvironmentVariablesPrefix = defaultApiSettings.EnvironmentVariablesPrefix; + + Console.WriteLine("==== Api ===="); Console.WriteLine("Name: " + apiSettings.Name); Console.WriteLine("Path: " + apiSettings.GetFullPath()); Console.WriteLine("EnvironmentVariablesPrefix: " + apiSettings.EnvironmentVariablesPrefix); - - builder.Configuration - .SetBasePath(Directory.GetCurrentDirectory()) - .AddJsonFile($"appsettings.json", optional: true, reloadOnChange: true) - .AddJsonFile($"appsettings.{EnvironmentUtility.GetCurrentEnvironment()}.json", optional: true, reloadOnChange: true) - .AddEnvironmentVariables(apiSettings.EnvironmentVariablesPrefix); builder.WebHost.UseUrls(customUrls ?? $"http://*:{apiSettings.Port}"); builder.Services.AddSingleton(apiSettings); diff --git a/src/Scaffolding/Models/DefaultApiSettings.cs b/src/Scaffolding/Models/DefaultApiSettings.cs new file mode 100644 index 0000000..e40efdd --- /dev/null +++ b/src/Scaffolding/Models/DefaultApiSettings.cs @@ -0,0 +1,12 @@ +namespace Scaffolding.Models +{ + public class DefaultApiSettings + { + public string EnvironmentVariablesPrefix { get; private set; } + + public DefaultApiSettings(string environmentVariablesPrefix) + { + EnvironmentVariablesPrefix = environmentVariablesPrefix; + } + } +} diff --git a/src/Scaffolding/Program.cs b/src/Scaffolding/Program.cs index 55bd1b5..9efe7d3 100644 --- a/src/Scaffolding/Program.cs +++ b/src/Scaffolding/Program.cs @@ -1,7 +1,10 @@ using Scaffolding; using Scaffolding.Extensions.Healthcheck; +using Scaffolding.Models; -var builder = Api.Initialize(args); +var apiSettings = new DefaultApiSettings("ScaffoldingApi_"); + +var builder = Api.Initialize(apiSettings, args); builder.UseLogging(); diff --git a/src/Scaffolding/appsettings.json b/src/Scaffolding/appsettings.json index fd56ae9..491b643 100644 --- a/src/Scaffolding/appsettings.json +++ b/src/Scaffolding/appsettings.json @@ -4,7 +4,6 @@ "Domain": "Domain", "JsonSerializer": "SnakeCase", "Port": 8500, - "EnvironmentVariablesPrefix": "ScaffoldingApi_", "Version": "v1", "PathBase": "scaffolding", "RequestKeyProperty": "RequestKey",