117 lines
3.4 KiB
C#
117 lines
3.4 KiB
C#
using System.Security.Cryptography;
|
|
using API.Middlewares;
|
|
using Infrastructure;
|
|
using Infrastructure.Data;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.OpenApi.Models;
|
|
using System.Security.Cryptography.X509Certificates;
|
|
using Microsoft.AspNetCore.Server.Kestrel.Https;
|
|
|
|
var builder = WebApplication.CreateBuilder(args);
|
|
|
|
builder.Logging.ClearProviders();
|
|
builder.Logging.AddConsole();
|
|
builder.Logging.AddDebug();
|
|
|
|
|
|
// Specify the paths to your certificate and key files
|
|
var certPath = Path.Combine(Directory.GetCurrentDirectory(), "cert.pem");
|
|
var keyPath = Path.Combine(Directory.GetCurrentDirectory(), "key.pem");
|
|
|
|
// Load the certificate and key
|
|
var certificate = new X509Certificate2(certPath);
|
|
var privateKeyText = File.ReadAllText(keyPath);
|
|
var rsaPrivateKey = RSA.Create();
|
|
rsaPrivateKey.ImportFromPem(privateKeyText.ToCharArray());
|
|
|
|
// Combine the certificate and private key into one X509Certificate2 object
|
|
var certificateWithKey = certificate.CopyWithPrivateKey(rsaPrivateKey);
|
|
|
|
builder.WebHost.ConfigureKestrel(serverOptions =>
|
|
{
|
|
serverOptions.ListenAnyIP(5000); // Set HTTP port
|
|
serverOptions.ListenAnyIP(5001, listenOptions => // Set HTTPS port
|
|
{
|
|
// Apply the certificate with the private key to HTTPS
|
|
listenOptions.UseHttps(new HttpsConnectionAdapterOptions
|
|
{
|
|
ServerCertificate = certificateWithKey,
|
|
ClientCertificateMode = ClientCertificateMode.NoCertificate
|
|
});
|
|
});
|
|
});;
|
|
|
|
builder.Services.AddCors(options =>
|
|
{
|
|
options.AddPolicy("AllowAll",
|
|
builder =>
|
|
{
|
|
builder.AllowAnyOrigin()
|
|
.AllowAnyMethod()
|
|
.AllowAnyHeader()
|
|
.WithExposedHeaders("*"); // This exposes all headers
|
|
});
|
|
});
|
|
|
|
builder.Services.AddControllers();
|
|
builder.Services.AddInfrastructureServices(builder.Configuration);
|
|
|
|
builder.Services.AddEndpointsApiExplorer();
|
|
builder.Services.AddSwaggerGen(c =>
|
|
{
|
|
c.AddSecurityDefinition("ApiKey", new OpenApiSecurityScheme
|
|
{
|
|
Description = "ApiKey must appear in header",
|
|
Type = SecuritySchemeType.ApiKey,
|
|
Name = "ApiKey",
|
|
In = ParameterLocation.Header,
|
|
Scheme = "ApiKeyScheme"
|
|
});
|
|
var key = new OpenApiSecurityScheme
|
|
{
|
|
Reference = new OpenApiReference
|
|
{
|
|
Type = ReferenceType.SecurityScheme,
|
|
Id = "ApiKey"
|
|
},
|
|
In = ParameterLocation.Header
|
|
};
|
|
var requirement = new OpenApiSecurityRequirement
|
|
{
|
|
{ key, new List<string>() }
|
|
};
|
|
c.AddSecurityRequirement(requirement);
|
|
});
|
|
|
|
var app = builder.Build();
|
|
|
|
if (app.Environment.IsDevelopment())
|
|
{
|
|
app.UseSwagger();
|
|
app.UseSwaggerUI();
|
|
}
|
|
|
|
app.UseCors("AllowAll");
|
|
app.UseHttpsRedirection();
|
|
app.MapControllers();
|
|
|
|
// Middlewares
|
|
app.UseMiddleware<ApiKeyMiddleware>();
|
|
app.UseMiddleware<BodyCheckMiddleware>();
|
|
app.UseMiddleware<JwtMiddleware>();
|
|
|
|
using (var scope = app.Services.CreateScope())
|
|
{
|
|
var services = scope.ServiceProvider;
|
|
var dbContext = services.GetRequiredService<HealthcareManagerDatabase>(); // Directly resolving your DbContext
|
|
EnsureDatabaseCreated(dbContext);
|
|
}
|
|
|
|
app.Run();
|
|
|
|
static void EnsureDatabaseCreated(HealthcareManagerDatabase dbContext)
|
|
{
|
|
// Checking for pending migrations is more efficient than applying migrations unconditionally
|
|
if (dbContext.Database.GetPendingMigrations().Any()) dbContext.Database.Migrate();
|
|
}
|