-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
60 lines (49 loc) · 2 KB
/
Copy pathProgram.cs
File metadata and controls
60 lines (49 loc) · 2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using Smart_Home_IoT_Device_Management_API.Extensions;
using Smart_Home_IoT_Device_Management_API.Infrastructure.Persistence;
var builder = WebApplication.CreateBuilder(args);
// --- Database ---
var connectionString = builder.Configuration.GetConnectionString("DefaultConnection");
builder.Services.AddDbContext<SmartHomeContext>(options => options.UseNpgsql(connectionString));
// --- Service Registrations ---
builder.Services.AddApplicationServices();
// --- Identity ---
builder.Services.AddIdentity<User, IdentityRole<Guid>>(options =>
{
options.Password.RequiredLength = 6;
options.Password.RequireDigit = false;
options.Password.RequireNonAlphanumeric = false;
options.Password.RequireUppercase = false;
options.Password.RequireLowercase = false;
})
.AddEntityFrameworkStores<SmartHomeContext>()
.AddDefaultTokenProviders();
// --- JWT, Auth, Swagger---
builder.Services.AddJwtAuthentication(builder.Configuration);
builder.Services.AddCustomAuthorization();
builder.Services.AddSwaggerWithJwt();
// --- Middleware, Logging ---
builder.Logging.AddConsole();
builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
var app = builder.Build();
// --- Seed ---
using (var scope = app.Services.CreateScope()) // The using ensures the scope and all its services are disposed properly after use.
{
var context = scope.ServiceProvider.GetRequiredService<SmartHomeContext>();// Retrieves an instance of DbContext (SmartHomeContext) from the DI container.
var seeder = scope.ServiceProvider.GetRequiredService<ISeedData>();
await seeder.InitializeAsync(context);
}
// --- Middleware pipeline ---
app.UseGlobalMiddlewares();
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.UseAuthentication();
app.UseAuthorization();
app.MapControllers();
app.Run();