Skip to content

Commit

Permalink
chore: configure default api settings
Browse files Browse the repository at this point in the history
  • Loading branch information
eduardosbcabral committed Sep 26, 2023
1 parent dd9912d commit be1718b
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 10 deletions.
23 changes: 15 additions & 8 deletions src/Scaffolding/Api.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ApiSettings>();
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);
Expand Down
12 changes: 12 additions & 0 deletions src/Scaffolding/Models/DefaultApiSettings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace Scaffolding.Models
{
public class DefaultApiSettings
{
public string EnvironmentVariablesPrefix { get; private set; }

public DefaultApiSettings(string environmentVariablesPrefix)
{
EnvironmentVariablesPrefix = environmentVariablesPrefix;
}
}
}
5 changes: 4 additions & 1 deletion src/Scaffolding/Program.cs
Original file line number Diff line number Diff line change
@@ -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();

Expand Down
1 change: 0 additions & 1 deletion src/Scaffolding/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
"Domain": "Domain",
"JsonSerializer": "SnakeCase",
"Port": 8500,
"EnvironmentVariablesPrefix": "ScaffoldingApi_",
"Version": "v1",
"PathBase": "scaffolding",
"RequestKeyProperty": "RequestKey",
Expand Down

0 comments on commit be1718b

Please sign in to comment.