-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
70 lines (53 loc) · 2.04 KB
/
Program.cs
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
61
62
63
64
65
66
67
68
69
70
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using SongDiary.Data;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllersWithViews();
builder.Services.AddDbContext<ApplicationDbContext>(opt =>
opt.UseNpgsql(builder.Configuration.GetConnectionString("DefaultConnection")));
// Add Identity services
builder.Services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>();
// .AddDefaultTokenProviders();
if (!builder.Environment.IsDevelopment())
{
// In production, load environment variables (for cloud-hosted configurations)
builder.Configuration.AddEnvironmentVariables(); // Add environment variables for production
}
// Seed service
builder.Services.AddScoped<SeedService>();
// Add HttpClientFactory
builder.Services.AddHttpClient(); // This allows for easy management of HttpClient instances
// Swagger setup
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
var app = builder.Build();
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
using (var serviceScope = app.Services.CreateScope())
{
var context = serviceScope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
context.Database.Migrate();
}
// Seed the database
using (var scope = app.Services.CreateScope())
{
var seedService = scope.ServiceProvider.GetRequiredService<SeedService>();
await seedService.SeedDataAsync(); // Ensure this is asynchronous
}
// app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
// Add authentication and authorization middleware
app.UseAuthentication(); // Ensure authentication middleware is added
app.UseAuthorization(); // Keep authorization if you need it for other purposes
// Add endpoint mapping after routing
app.MapControllerRoute(
name: "default",
pattern: "{controller}/{action=Index}/{id?}");
app.MapFallbackToFile("index.html");
await app.RunAsync(); // Ensure the application runs asynchronously