1
0
mirror of https://github.com/XFox111/MuiCharts.git synced 2026-04-22 06:51:05 +03:00

- Reworked Infrastructure injection

- Added LettuceEncrypt for HTTPS
- Fixed DataContext
- Moved connection string to appsettings.json
This commit is contained in:
2024-02-22 14:56:15 +00:00
parent be8cc7ded4
commit 39bc85c9d9
7 changed files with 38 additions and 23 deletions
@@ -9,6 +9,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="LettuceEncrypt" Version="1.3.0" />
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.2">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
+10 -2
View File
@@ -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();
@@ -4,5 +4,8 @@
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"ConnectionStrings": {
"DataContext": "Data Source=app.db"
}
}
+4 -1
View File
@@ -5,5 +5,8 @@
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
"AllowedHosts": "*",
"ConnectionStrings": {
"DataContext": "Data Source=/persistence/data.db"
}
}
@@ -19,20 +19,13 @@ public class DataContext : DbContext
/// </summary>
public DbSet<Track> Tracks { get; set; }
/// <inheritdoc/>
public DataContext() : base() {}
/// <inheritdoc/>
public DataContext(DbContextOptions<DataContext> options) : base(options) {}
/// <inheritdoc/>
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
/// <summary>
/// Initializes a new instance of <see cref="DataContext"/>.
/// </summary>
/// <param name="options">The options for this context.</param>
public DataContext(DbContextOptions<DataContext> options) : base(options)
{
optionsBuilder
.UseSqlite("Data Source=data.db")
.EnableSensitiveDataLogging(
Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") == "Development"
);
Database.Migrate();
}
/// <inheritdoc/>
@@ -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
/// </summary>
/// <param name="services">The <see cref="IServiceCollection"/> to add the services to.</param>
/// <returns>The modified <see cref="IServiceCollection"/>.</returns>
public static IServiceCollection AddInfrastructure(this IServiceCollection services)
public static void AddInfrastructure(this IHostApplicationBuilder builder)
{
services.AddDbContext<DataContext>();
services.AddScoped<IPointRepository, PointRepository>();
services.AddScoped<ITrackRepository, TrackRepository>();
builder.Services.AddDbContext<DataContext>(options =>
{
options
.UseSqlite(builder.Configuration.GetConnectionString(nameof(DataContext)))
.EnableSensitiveDataLogging(builder.Environment.IsDevelopment());
});
return services;
builder.Services.AddScoped<IPointRepository, PointRepository>();
builder.Services.AddScoped<ITrackRepository, TrackRepository>();
}
}
@@ -6,8 +6,8 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="8.0.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="8.0.1" />
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="8.0.0" />
</ItemGroup>
<ItemGroup>