diff --git a/backend/MuiCharts.Api/MuiCharts.Api.csproj b/backend/MuiCharts.Api/MuiCharts.Api.csproj index 63f2990..c8baf23 100644 --- a/backend/MuiCharts.Api/MuiCharts.Api.csproj +++ b/backend/MuiCharts.Api/MuiCharts.Api.csproj @@ -9,6 +9,7 @@ + runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/backend/MuiCharts.Api/Program.cs b/backend/MuiCharts.Api/Program.cs index d0d903a..7caeb6f 100644 --- a/backend/MuiCharts.Api/Program.cs +++ b/backend/MuiCharts.Api/Program.cs @@ -1,4 +1,5 @@ using System.Reflection; +using LettuceEncrypt; using MuiCharts.Infrastructure; WebApplicationBuilder builder = WebApplication.CreateBuilder(args); @@ -17,7 +18,8 @@ WebApplicationBuilder builder = WebApplication.CreateBuilder(args); { options.SuppressAsyncSuffixInActionNames = false; }); - builder.Services.AddInfrastructure(); + + builder.AddInfrastructure(); builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerGen(options => @@ -26,6 +28,10 @@ WebApplicationBuilder builder = WebApplication.CreateBuilder(args); string xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFileName); options.IncludeXmlComments(xmlPath); }); + + if (builder.Configuration.GetSection("LettuceEncrypt").Exists()) + builder.Services.AddLettuceEncrypt() + .PersistDataToDirectory(new DirectoryInfo("/persistence"), null); } WebApplication app = builder.Build(); @@ -35,7 +41,9 @@ WebApplication app = builder.Build(); app.UseSwagger(); app.UseSwaggerUI(); - // app.UseHttpsRedirection(); + if (app.Configuration.GetSection("LettuceEncrypt").Exists()) + app.UseHttpsRedirection(); + app.MapControllers(); app.Run(); diff --git a/backend/MuiCharts.Api/appsettings.Development.json b/backend/MuiCharts.Api/appsettings.Development.json index a919448..78e579c 100644 --- a/backend/MuiCharts.Api/appsettings.Development.json +++ b/backend/MuiCharts.Api/appsettings.Development.json @@ -4,5 +4,8 @@ "Default": "Information", "Microsoft.AspNetCore": "Warning" } + }, + "ConnectionStrings": { + "DataContext": "Data Source=app.db" } } diff --git a/backend/MuiCharts.Api/appsettings.json b/backend/MuiCharts.Api/appsettings.json index 2f36700..943f5d5 100644 --- a/backend/MuiCharts.Api/appsettings.json +++ b/backend/MuiCharts.Api/appsettings.json @@ -5,5 +5,8 @@ "Microsoft.AspNetCore": "Warning" } }, - "AllowedHosts": "*" + "AllowedHosts": "*", + "ConnectionStrings": { + "DataContext": "Data Source=/persistence/data.db" + } } diff --git a/backend/MuiCharts.Infrastructure/DataContext.cs b/backend/MuiCharts.Infrastructure/DataContext.cs index 168d9d3..c5b60a0 100644 --- a/backend/MuiCharts.Infrastructure/DataContext.cs +++ b/backend/MuiCharts.Infrastructure/DataContext.cs @@ -19,20 +19,13 @@ public class DataContext : DbContext /// public DbSet Tracks { get; set; } - /// - public DataContext() : base() {} - - /// - public DataContext(DbContextOptions options) : base(options) {} - - /// - protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + /// + /// Initializes a new instance of . + /// + /// The options for this context. + public DataContext(DbContextOptions options) : base(options) { - optionsBuilder - .UseSqlite("Data Source=data.db") - .EnableSensitiveDataLogging( - Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") == "Development" - ); + Database.Migrate(); } /// diff --git a/backend/MuiCharts.Infrastructure/InfrastructrureExtensions.cs b/backend/MuiCharts.Infrastructure/InfrastructrureExtensions.cs index 858917e..c82c37c 100644 --- a/backend/MuiCharts.Infrastructure/InfrastructrureExtensions.cs +++ b/backend/MuiCharts.Infrastructure/InfrastructrureExtensions.cs @@ -1,6 +1,9 @@ -using Microsoft.Extensions.DependencyInjection; +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.DependencyInjection; using MuiCharts.Domain.Repositories; using MuiCharts.Infrastructure.Repositories; +using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.Configuration; namespace MuiCharts.Infrastructure; @@ -14,12 +17,16 @@ public static class InfrastructrureExtensions /// /// The to add the services to. /// The modified . - public static IServiceCollection AddInfrastructure(this IServiceCollection services) + public static void AddInfrastructure(this IHostApplicationBuilder builder) { - services.AddDbContext(); - services.AddScoped(); - services.AddScoped(); + builder.Services.AddDbContext(options => + { + options + .UseSqlite(builder.Configuration.GetConnectionString(nameof(DataContext))) + .EnableSensitiveDataLogging(builder.Environment.IsDevelopment()); + }); - return services; + builder.Services.AddScoped(); + builder.Services.AddScoped(); } } diff --git a/backend/MuiCharts.Infrastructure/MuiCharts.Infrastructure.csproj b/backend/MuiCharts.Infrastructure/MuiCharts.Infrastructure.csproj index 6ed9754..f089afc 100644 --- a/backend/MuiCharts.Infrastructure/MuiCharts.Infrastructure.csproj +++ b/backend/MuiCharts.Infrastructure/MuiCharts.Infrastructure.csproj @@ -6,8 +6,8 @@ runtime; build; native; contentfiles; analyzers; buildtransitive all - +