public void ConfigureServices(IServiceCollection services) { var appConfiguration = new ConfigurationBuilder() .AddEnvironmentVariables() .Build(); var homeDevConfiguration = new HomeDevicesConfiguration() { // Default service configuration: ServiceId = "homde-dev", LogLevel = "DEBUG", DatabaseServer = System.Environment.MachineName, DatabaseName = "homedev", DatabaseUsername = "******", DatabasePassword = "******" }; homeDevConfiguration.SetEnvironmentConfiguration(appConfiguration); homeDevConfiguration.LogConfiguration(); services.AddControllers(); services.AddHealthChecks().AddCheck <HealthCheck>("health-check"); services.AddDbContext <DevicesContext>(options => options.UseNpgsql(homeDevConfiguration.GetConnectionString())); services.AddTransient <IDataProvider, DataProvider>(); services.AddSwaggerGen(); }
public static void LogConfiguration(this HomeDevicesConfiguration config) { Log.Information("----------------------------------"); Log.Information("Started home-devices"); Log.Information("----------------------------------"); Log.Information($"[CFG] Service Id = {config.ServiceId ?? ""}"); Log.Information($"[CFG] Listening on = {config.ServiceListeningInterface ?? ""}"); Log.Information($"[CFG] Database server = {config.DatabaseServer ?? ""}"); Log.Information($"[CFG] Database name = {config.DatabaseName ?? ""}"); Log.Information($"[CFG] Database password = *******"); Log.Information($"[CFG] Database user name = {config.DatabaseUsername ?? ""}"); Log.Information($"[CFG] Log level = {config.LogLevel ?? ""}"); Log.Information("----------------------------------"); }
public static void SetEnvironmentConfiguration(this HomeDevicesConfiguration config, IConfigurationRoot rootConfig) { string serviceListeningInterface = rootConfig.GetSection("ASPNETCORE_URLS").Value; string serviceId = rootConfig.GetSection("SERVICE_ID").Value; string databaseServer = rootConfig.GetSection("DB_SERVER").Value; string databaseName = rootConfig.GetSection("DB_NAME").Value; string databaseUsername = rootConfig.GetSection("DB_USERNAME").Value; string databasePassword = rootConfig.GetSection("DB_PWD").Value; string logLevel = rootConfig.GetSection("LOG_LEVEL").Value; if (!string.IsNullOrEmpty(serviceId)) { config.ServiceId = serviceId; } if (!string.IsNullOrEmpty(serviceListeningInterface)) { config.ServiceListeningInterface = serviceListeningInterface; } if (!string.IsNullOrEmpty(databaseServer)) { config.DatabaseServer = databaseServer; } if (!string.IsNullOrEmpty(databaseName)) { config.DatabaseName = databaseName; } if (!string.IsNullOrEmpty(databaseUsername)) { config.DatabaseUsername = databaseUsername; } if (!string.IsNullOrEmpty(databasePassword)) { config.DatabasePassword = databasePassword; } if (!string.IsNullOrEmpty(logLevel)) { config.LogLevel = logLevel; } }