-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
109 lines (81 loc) · 4.03 KB
/
Program.cs
File metadata and controls
109 lines (81 loc) · 4.03 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
using BlogProject.Data;
using BlogProject.Helpers;
using BlogProject.Models;
using BlogProject.Services;
using BlogProject.View_Models;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Identity.UI.Services;
using Microsoft.EntityFrameworkCore;
using System.Configuration;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container
//var connectionString = configuration.GetConnectionString("DefaultConnection"); ?? throw new InvalidOperationException("Connection string 'DefaultConnection' not found.");
//var connectionString = configuration.GetSection("pgSettings")["pgConnection"];
var connectionString = ConnectionHelper.GetConnectionString(builder.Configuration);
builder.Services.AddDbContext<ApplicationDbContext>(options => options.UseNpgsql(connectionString));
builder.Services.AddDatabaseDeveloperPageExceptionFilter();
builder.Services.AddDefaultIdentity<BlogUser>(options => options.SignIn.RequireConfirmedAccount = true)
.AddDefaultUI() /* LEARN : Is this redundant? doesnt AddDefaultIdentity include the default UI?*/
.AddRoles<IdentityRole>()
.AddDefaultTokenProviders()
.AddEntityFrameworkStores<ApplicationDbContext>();
builder.Services.AddControllersWithViews();
builder.Services.AddRazorPages();
// Register Custom Services
builder.Services.AddScoped<IImageService, BasicImageService>();
builder.Services.AddScoped<ISlugService, BasicSlugService>();
builder.Services.AddScoped<BlogSearchService>();
builder.Services.AddScoped<DataService>();
// Register preconfigured instance of MailSettings and EmailService
builder.Services.AddScoped<EmailService>();
builder.Services.Configure<MailSettings>(builder.Configuration.GetSection("MailSettings"));
var app = builder.Build();
var scope = app.Services.CreateScope();
//await DataHelper.ManageDataAsync(scope.ServiceProvider);
//// LEARN : Explain this code. Some of it seems to be redundant from DataHelper.cs
/// TODO : DELETE this section
////using (var scope = app.Services.CreateScope())
////{
//// var services = scope.ServiceProvider;
//// var context = services.GetRequiredService<ApplicationDbContext>();
//// // Apply any pending migrations and create the database if it doesn't exist
//// await context.Database.MigrateAsync();
//// // Run additional data management tasks
//// await DataHelper.ManageDataAsync(scope.ServiceProvider);
////}
var dataService = scope.ServiceProvider.GetRequiredService<DataService>();
//// COMMIT 26327f4 : Jan 2, 2025
// Get access to registered DataService
//var dataService = app.Services.CreateScope()
// .ServiceProvider.GetRequiredService<DataService>();
// Run initialization ManageDataAsync()
await dataService.ManageDataAsync();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseMigrationsEndPoint();
}
else
{
app.UseExceptionHandler("/Home/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseStatusCodePagesWithReExecute("/Home/HandleError/{0}");
// BLOG : Commented out the HTTPS redirection because Railway handles HTTPS redirections externally, and the UseHttpRedirection was causing errors on Railway deployment. This is likely because I am using a custom domain with Railway, and Railway provides its own SSL termination.
// TODO : Shoud I put this in the if (app.Environment.IsDevelopment()) block above?
//app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
// TODO : Check if SlugRoute is configured correctly
// Create new MapControllerRoute to supersede default route using the Slug, sometimes refered to a "Vanity URL", which improves SEO
app.MapControllerRoute(
name: "SlugRoute",
pattern: "BlogPosts/UrlFriendly/{slug}",
defaults: new { controller = "Posts", action = "Details" });
app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
app.MapRazorPages();
app.Run();