rework: configuration service
- to have it be more dynamic
This commit is contained in:
@@ -9,12 +9,10 @@ namespace CaddyManager.Contracts.Configurations;
|
||||
public interface IConfigurationsService
|
||||
{
|
||||
/// <summary>
|
||||
/// Configurations for Caddy service
|
||||
/// Method extracting the configurations from the appsettings.json file or environment variables base on the
|
||||
/// type of the configuration class to determine the section name
|
||||
/// </summary>
|
||||
CaddyServiceConfigurations CaddyServiceConfigurations { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Configurations for Docker service
|
||||
/// </summary>
|
||||
DockerServiceConfiguration DockerServiceConfiguration { get; }
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <returns></returns>
|
||||
T Get<T>() where T : class;
|
||||
}
|
||||
@@ -13,7 +13,7 @@ public class CaddyService(IConfigurationsService configurationsService) : ICaddy
|
||||
/// </summary>
|
||||
private const string CaddyGlobalConfigName = "Caddyfile";
|
||||
|
||||
private CaddyServiceConfigurations Configurations => configurationsService.CaddyServiceConfigurations;
|
||||
private CaddyServiceConfigurations Configurations => configurationsService.Get<CaddyServiceConfigurations>();
|
||||
|
||||
/// <inheritdoc />
|
||||
public List<string> GetExistingCaddyConfigurations()
|
||||
|
||||
@@ -10,11 +10,16 @@ namespace CaddyManager.Services.Configurations;
|
||||
public class ConfigurationsService(IConfiguration configuration) : IConfigurationsService
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public CaddyServiceConfigurations CaddyServiceConfigurations =>
|
||||
configuration.GetSection(CaddyServiceConfigurations.Caddy).Get<CaddyServiceConfigurations>() ??
|
||||
new CaddyServiceConfigurations();
|
||||
public T Get<T>() where T : class
|
||||
{
|
||||
var section = typeof(T).Name;
|
||||
|
||||
public DockerServiceConfiguration DockerServiceConfiguration =>
|
||||
configuration.GetSection(DockerServiceConfiguration.Docker).Get<DockerServiceConfiguration>() ??
|
||||
new DockerServiceConfiguration();
|
||||
// Have the configuration section name be the section name without the "Configurations" suffix
|
||||
if (section.EndsWith("Configurations"))
|
||||
section = section[..^"Configurations".Length];
|
||||
else if (section.EndsWith("Configuration"))
|
||||
section = section[..^"Configuration".Length];
|
||||
|
||||
return configuration.GetSection(section).Get<T>() ?? Activator.CreateInstance<T>();
|
||||
}
|
||||
}
|
||||
@@ -9,7 +9,7 @@ namespace CaddyManager.Services.Docker;
|
||||
/// <inheritdoc />
|
||||
public class DockerService(IConfigurationsService configurationsService) : IDockerService
|
||||
{
|
||||
private DockerServiceConfiguration Configuration => configurationsService.DockerServiceConfiguration;
|
||||
private DockerServiceConfiguration Configuration => configurationsService.Get<DockerServiceConfiguration>();
|
||||
|
||||
/// <summary>
|
||||
/// Method to get the container id of the Caddy container by the name configured
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
"Microsoft.AspNetCore": "Warning"
|
||||
}
|
||||
},
|
||||
"Caddy": {
|
||||
"CaddyService": {
|
||||
"ConfigDir": "./caddy/config"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,10 +6,10 @@
|
||||
}
|
||||
},
|
||||
"AllowedHosts": "*",
|
||||
"Caddy": {
|
||||
"CaddyService": {
|
||||
"ConfigDir": "/root/compose/caddy/config"
|
||||
},
|
||||
"Docker": {
|
||||
"DockerService": {
|
||||
"CaddyContainerName": "caddy",
|
||||
"DockerHost": "unix:///var/run/docker.sock"
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user