2026-03-01 02:13:26 +00:00
|
|
|
using IdentityModel;
|
2026-06-10 00:23:17 +01:00
|
|
|
using Microsoft.AspNetCore.Authorization;
|
2026-03-01 02:13:26 +00:00
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
2026-06-22 01:44:24 +01:00
|
|
|
using Microsoft.Extensions.DependencyInjection.Extensions;
|
|
|
|
|
using Yavsc;
|
2026-03-01 02:13:26 +00:00
|
|
|
using Yavsc.Interface;
|
2026-06-22 01:44:24 +01:00
|
|
|
using Yavsc.Interfaces;
|
2026-03-01 02:13:26 +00:00
|
|
|
using Yavsc.Models;
|
|
|
|
|
using Yavsc.Services;
|
2026-03-09 02:07:09 +00:00
|
|
|
using Yavsc.Server.Helpers;
|
2026-06-19 23:09:43 +01:00
|
|
|
using Microsoft.AspNetCore.Identity;
|
2026-06-10 16:59:23 +01:00
|
|
|
namespace Yavsc.Blogs;
|
2026-03-01 02:13:26 +00:00
|
|
|
|
|
|
|
|
internal class Program
|
|
|
|
|
{
|
|
|
|
|
private static async Task Main(string[] args)
|
2026-06-04 22:04:20 +01:00
|
|
|
{
|
|
|
|
|
Console.Title = "Yavsc.Blogs";
|
2026-03-09 02:07:09 +00:00
|
|
|
|
2026-06-04 22:04:20 +01:00
|
|
|
var builder = WebApplication.CreateBuilder(args);
|
2026-06-15 00:11:48 +01:00
|
|
|
builder.AddConfiguration("blogs");
|
2026-03-09 02:07:09 +00:00
|
|
|
|
2026-06-19 13:15:21 +01:00
|
|
|
var services = builder.Services;
|
2026-03-09 02:07:09 +00:00
|
|
|
|
2026-06-19 23:09:43 +01:00
|
|
|
// MvcBuilder
|
|
|
|
|
builder.Services
|
2026-06-04 22:04:20 +01:00
|
|
|
.AddAuthorization(options =>
|
|
|
|
|
{
|
2026-06-06 21:30:41 +01:00
|
|
|
options.AddPolicy("BlogScope", policy =>
|
2026-06-04 22:04:20 +01:00
|
|
|
{
|
|
|
|
|
policy
|
2026-03-09 02:07:09 +00:00
|
|
|
.RequireAuthenticatedUser()
|
2026-06-25 00:08:25 +01:00
|
|
|
.RequireClaim(JwtClaimTypes.Scope, new string[] { "blogs" });
|
2026-06-04 22:04:20 +01:00
|
|
|
});
|
|
|
|
|
})
|
2026-06-19 13:15:21 +01:00
|
|
|
.AddYavscCors(builder.Configuration)
|
2026-06-04 22:04:20 +01:00
|
|
|
.AddControllers();
|
2026-07-10 00:27:18 +01:00
|
|
|
String authority = builder.Configuration.GetValue<string>("Site:Authority");
|
2026-07-12 01:17:52 +01:00
|
|
|
|
2026-07-10 00:27:18 +01:00
|
|
|
if (string.IsNullOrEmpty(authority))
|
|
|
|
|
{
|
|
|
|
|
throw new Exception("Site:Authority is not configured in appsettings.json");
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-12 06:45:00 +01:00
|
|
|
// AuthenticationBuilder
|
|
|
|
|
services.AddAuthentication("Bearer")
|
|
|
|
|
.AddYavscJwtBearer(builder.Configuration);
|
2026-07-12 01:17:52 +01:00
|
|
|
|
2026-06-04 22:04:20 +01:00
|
|
|
|
2026-06-19 23:09:43 +01:00
|
|
|
// DbContextBuilder
|
2026-06-04 22:04:20 +01:00
|
|
|
services.AddDbContext<ApplicationDbContext>(options =>
|
2026-06-20 18:43:01 +01:00
|
|
|
options.UseNpgsql(builder.Configuration.GetConnectionString(
|
2026-08-20 20:50:52 +01:00
|
|
|
Yavsc.Constants.YavscConnectionStringName)));
|
2026-06-04 22:04:20 +01:00
|
|
|
|
2026-06-19 23:09:43 +01:00
|
|
|
// other services
|
|
|
|
|
services
|
|
|
|
|
.AddTransient<ITrueEmailSender, MailSender>()
|
|
|
|
|
.AddTransient<IEmailSender<ApplicationUser>, MailSender>()
|
2026-06-22 01:44:24 +01:00
|
|
|
.TryAddSingleton<ISmtpClientFactory, SmtpClientFactory>();
|
|
|
|
|
services
|
2026-06-19 23:09:43 +01:00
|
|
|
.AddTransient<IBillingService, BillingService>()
|
|
|
|
|
.AddTransient<ICalendarManager, CalendarManager>()
|
|
|
|
|
.AddTransient<IFileSystemAuthManager, FileSystemAuthManager>()
|
|
|
|
|
.AddTransient<BlogSpotService>()
|
|
|
|
|
.AddScoped<IAuthorizationHandler, PermissionHandler>()
|
|
|
|
|
.AddLocalization(options =>
|
2026-06-04 22:04:20 +01:00
|
|
|
{
|
|
|
|
|
options.ResourcesPath = "Resources";
|
2026-06-19 23:09:43 +01:00
|
|
|
})
|
|
|
|
|
.AddDistributedMemoryCache()
|
|
|
|
|
.AddSession(options =>
|
2026-06-04 22:04:20 +01:00
|
|
|
{
|
|
|
|
|
options.IdleTimeout = TimeSpan.FromMinutes(30);
|
|
|
|
|
options.Cookie.HttpOnly = true;
|
|
|
|
|
options.Cookie.IsEssential = false;
|
2026-06-19 23:09:43 +01:00
|
|
|
}).Configure<RequestLocalizationOptions>(options =>
|
2026-06-04 22:04:20 +01:00
|
|
|
{
|
|
|
|
|
var supportedCultures = new[] { "fr", "en", "pt" };
|
|
|
|
|
options.SetDefaultCulture(supportedCultures[0])
|
|
|
|
|
.AddSupportedCultures(supportedCultures)
|
|
|
|
|
.AddSupportedUICultures(supportedCultures);
|
|
|
|
|
});
|
2026-06-19 23:09:43 +01:00
|
|
|
|
|
|
|
|
// App startup
|
2026-06-04 22:04:20 +01:00
|
|
|
using (var app = builder.Build())
|
|
|
|
|
{
|
|
|
|
|
if (app.Environment.IsDevelopment())
|
|
|
|
|
app.UseDeveloperExceptionPage();
|
|
|
|
|
app
|
|
|
|
|
.UseRouting()
|
|
|
|
|
.UseAuthentication()
|
|
|
|
|
.UseAuthorization()
|
|
|
|
|
.UseCors("default")
|
|
|
|
|
;
|
2026-07-10 00:27:18 +01:00
|
|
|
app.Services.GetRequiredService<ILoggerFactory>().CreateLogger("Program")
|
2026-07-12 01:17:52 +01:00
|
|
|
.LogInformation($"Yavsc.Blogs started, Authority is '{authority}''");
|
2026-07-07 21:48:02 +01:00
|
|
|
app.MapControllers();
|
2026-07-10 00:27:18 +01:00
|
|
|
app.MapIdentityApi<ApplicationUser>().RequireAuthorization("BlogScope")
|
|
|
|
|
.WithHttpLogging(Microsoft.AspNetCore.HttpLogging.HttpLoggingFields.All)
|
|
|
|
|
.WithTags("Identity")
|
|
|
|
|
.WithDescription("Identity API for Yavsc.Blogs")
|
|
|
|
|
.WithMetadata(new ApiExplorerSettingsAttribute { GroupName = "Identity" });
|
2026-06-19 23:09:43 +01:00
|
|
|
|
2026-06-22 01:44:24 +01:00
|
|
|
app.UseSession();
|
2026-06-04 22:04:20 +01:00
|
|
|
await app.RunAsync();
|
|
|
|
|
}
|
2026-03-01 02:13:26 +00:00
|
|
|
}
|
2026-03-09 02:07:09 +00:00
|
|
|
|
2026-03-01 02:13:26 +00:00
|
|
|
}
|