-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
111 lines (89 loc) · 3.32 KB
/
Program.cs
File metadata and controls
111 lines (89 loc) · 3.32 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
using AeroAssist.Data;
using AeroAssist.Services;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
var builder = WebApplication.CreateBuilder(args);
var config = builder.Configuration;
var myAllowSpecificOrigins = "_myAllowSpecificOrigins";
// Add services from AeroAssist.Services below
builder.Services.AddScoped<TicketService.ITicketService, TicketService>();
// Add DbContext
var connectionString = builder.Configuration.GetConnectionString("DefaultConnection") ?? throw new InvalidOperationException("Connection string 'DefaultConnection' not found.");
builder.Services.AddDbContext<AeroAssistContext>(options =>
options.UseSqlServer(connectionString));
builder.Services.AddDatabaseDeveloperPageExceptionFilter();
// CORS policy - configurable origins with fallback to localhost
var corsOrigins = config.GetSection("Cors:Origins").Get<string[]>()
?? new[] { "https://localhost:7223", "https://localhost:5001", "http://localhost:8080" };
builder.Services.AddCors(options =>
{
options.AddPolicy(name: myAllowSpecificOrigins,
policy =>
{
policy.WithOrigins(corsOrigins)
.AllowAnyHeader()
.AllowAnyMethod();
});
});
builder.Services.AddDefaultIdentity<IdentityUser>(options => options.SignIn.RequireConfirmedAccount = true)
.AddEntityFrameworkStores<AeroAssistContext>();
// Microsoft Account authentication (optional - only configure if credentials are provided)
var msClientId = config["Authentication:Microsoft:ClientId"];
var msClientSecret = config["Authentication:Microsoft:ClientSecret"];
if (!string.IsNullOrEmpty(msClientId) && !string.IsNullOrEmpty(msClientSecret))
{
builder.Services.AddAuthentication()
.AddMicrosoftAccount(microsoftOptions =>
{
microsoftOptions.ClientId = msClientId;
microsoftOptions.ClientSecret = msClientSecret;
});
}
builder.Services.AddControllers();
builder.Services.AddRazorPages();
builder.Services.AddHealthChecks();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
var app = builder.Build();
// Apply pending migrations automatically if configured (useful for containers)
if (config.GetValue<bool>("Database:AutoMigrate"))
{
using var scope = app.Services.CreateScope();
var db = scope.ServiceProvider.GetRequiredService<AeroAssistContext>();
db.Database.Migrate();
}
// Configure the HTTP request pipeline.
var enableSwagger = app.Environment.IsDevelopment() || config.GetValue<bool>("Swagger:Enabled");
if (enableSwagger)
{
app.UseSwagger();
app.UseSwaggerUI();
}
if (app.Environment.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Error");
// Only use HSTS and HTTPS redirection if not behind a reverse proxy
if (!config.GetValue<bool>("ReverseProxy:Enabled"))
{
app.UseHsts();
app.UseHttpsRedirection();
}
}
app.UseRouting();
app.UseCors(myAllowSpecificOrigins);
app.UseAuthentication();
app.UseAuthorization();
// Map health check endpoint
app.MapHealthChecks("/health");
// Map API controllers
app.MapControllers();
// Map Razor Pages
app.MapRazorPages();
// Map static files via wwwroot folder (e.g. images)
app.UseStaticFiles();
app.Run();