diff --git a/back/Domain/ApplicationDbContext.cs b/back/Domain/ApplicationDbContext.cs new file mode 100644 index 0000000..c2961e7 --- /dev/null +++ b/back/Domain/ApplicationDbContext.cs @@ -0,0 +1,12 @@ +using Microsoft.EntityFrameworkCore; + +namespace back.Domain; + +public class ApplicationDbContext : DbContext +{ + public ApplicationDbContext(DbContextOptions options) : base(options) + { + Database.EnsureCreated(); + Database.Migrate(); + } +} \ No newline at end of file diff --git a/back/Domain/IEntity.cs b/back/Domain/IEntity.cs new file mode 100644 index 0000000..7b8164e --- /dev/null +++ b/back/Domain/IEntity.cs @@ -0,0 +1,14 @@ +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; + +namespace back.Domain; + +public interface IEntity +{ + [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)] + string Id + { + get; + set; + } +} \ No newline at end of file diff --git a/back/Program.cs b/back/Program.cs index 8dbf2df..c2cc34f 100644 --- a/back/Program.cs +++ b/back/Program.cs @@ -1,4 +1,8 @@ +using Autofac.Extensions.DependencyInjection; +using back.Domain; using MCVIngenieros.Healthchecks; +using MediatR.Extensions.FluentValidation.AspNetCore; +using Microsoft.EntityFrameworkCore; using OpenTelemetry.Logs; using OpenTelemetry.Metrics; using OpenTelemetry.Resources; @@ -43,7 +47,15 @@ public class Program var builder = WebApplication.CreateBuilder(args); builder.Configuration.AddConfiguration(configuration); builder.Host.UseSerilog(); - // Add services to the container. + builder.Host.UseServiceProviderFactory(new AutofacServiceProviderFactory()); + + builder.Services.AddMediatR(cfg => + { + cfg.RegisterServicesFromAssembly(typeof(Program).Assembly); + }); + + builder.Services.AddFluentValidation([typeof(Program).Assembly]); + builder.Services.AddHealthChecksSupport().DiscoverHealthChecks(); builder.Services.AddLogging(); builder.Logging.AddOpenTelemetry(options => @@ -63,7 +75,32 @@ public class Program .AddAspNetCoreInstrumentation() .AddConsoleExporter()); + builder.Services.AddDataProtection(); + builder.Services.AddCors(options => + { + options.AddDefaultPolicy(policy => + { + policy + .WithOrigins(builder.Configuration.GetSection("AllowedHosts").Get() ?? []) + .SetIsOriginAllowedToAllowWildcardSubdomains() + .SetPreflightMaxAge(TimeSpan.FromMinutes(10)) + .AllowCredentials() + .AllowAnyHeader() + .AllowAnyMethod(); + }); + }); + builder.Services.AddHttpContextAccessor(); + builder.Services.AddAntiforgery(options => + { + options.HeaderName = "X-XSRF-TOKEN"; + }); + + builder.Services.AddDbContext(opts => + { + var connectionString = builder.Configuration.GetConnectionString("DefaultConnection"); + opts.UseSqlite(connectionString); + }); builder.Services.AddControllers(options => { @@ -88,7 +125,6 @@ public class Program app.UseAuthorization(); - app.MapControllers(); app.Run(); diff --git a/back/appsettings.Development.json b/back/appsettings.Development.json index 0c208ae..10f68b8 100644 --- a/back/appsettings.Development.json +++ b/back/appsettings.Development.json @@ -4,5 +4,6 @@ "Default": "Information", "Microsoft.AspNetCore": "Warning" } - } + }, + "AllowedHosts": "*" } diff --git a/back/appsettings.Production.json b/back/appsettings.Production.json index 0c208ae..3b46298 100644 --- a/back/appsettings.Production.json +++ b/back/appsettings.Production.json @@ -4,5 +4,6 @@ "Default": "Information", "Microsoft.AspNetCore": "Warning" } - } + }, + "AllowedHosts": "mmorales.photo" } diff --git a/back/appsettings.json b/back/appsettings.json index 10f68b8..0db3279 100644 --- a/back/appsettings.json +++ b/back/appsettings.json @@ -1,9 +1,3 @@ { - "Logging": { - "LogLevel": { - "Default": "Information", - "Microsoft.AspNetCore": "Warning" - } - }, - "AllowedHosts": "*" + } diff --git a/back/back.csproj b/back/back.csproj index bad8936..7ea4bfc 100644 --- a/back/back.csproj +++ b/back/back.csproj @@ -7,12 +7,35 @@ + + + + + + + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + @@ -41,7 +64,6 @@ -