Files
FrontOffice/src/FrontOffice.Main/Program.cs

91 lines
2.6 KiB
C#

using FluentValidation;
using FrontOffice.Main.Utilities;
using System.Net;
using FrontOffice.Main.Utilities.Pdf;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddRazorPages();
builder.Services.AddServerSideBlazor();
#region AddCommonServices
builder.Services.AddCommonServices();
builder.Services.AddSingleton<MainService>();
#endregion
#region AddGrpcServices
builder.Services.AddGrpcServices(builder.Configuration);
#endregion
#region FluentValidation
ValidatorOptions.Global.LanguageManager = new CustomFluentValidationLanguageManager();
#endregion
var appSettings = builder.Configuration.Get<AppSettings>();
if (!string.IsNullOrWhiteSpace(appSettings?.DownloadUrl))
{
UrlUtility.DownloadUrl = appSettings.DownloadUrl;
}
else
{
UrlUtility.DownloadUrl = string.Empty; // fallback to empty
}
builder.Services.Configure<EncryptionSettings>(builder.Configuration.GetSection("EncryptionSettings"));
builder.Services.AddSingleton<MobileNumberEncryptor>();
var webApp = builder.Build();
// Configure the HTTP request pipeline.
if (!webApp.Environment.IsDevelopment())
{
webApp.UseExceptionHandler("/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
webApp.UseHsts();
}
webApp.UseHttpsRedirection();
webApp.UseStaticFiles();
webApp.UseRouting();
webApp.MapBlazorHub();
webApp.MapFallbackToPage("/_Host");
webApp.MapGet("/contract/generate",async (string html, string? fileName, IChromiumPdfService pdfService) =>
{
var decoded = WebUtility.UrlDecode(html);
var safeName = string.IsNullOrWhiteSpace(fileName) ? "contract" : fileName.Trim();
var bytes =await pdfService.GeneratePdfAsync(decoded, safeName);
return Results.File(bytes, "application/pdf", safeName + ".pdf");
});
// Support large HTML via POST (body) to avoid URL length limits
webApp.MapPost("/contract/generate", async (ContractPdfRequest req, IChromiumPdfService pdfService) =>
{
if (string.IsNullOrWhiteSpace(req.Html)) return Results.BadRequest("Empty html");
var safeName = string.IsNullOrWhiteSpace(req.FileName) ? "contract" : req.FileName.Trim();
var bytes = await pdfService.GeneratePdfAsync(req.Html, safeName);
return Results.File(bytes, "application/pdf", safeName + ".pdf");
});
webApp.Run();
public sealed record ContractPdfRequest(string Html, string? FileName);
public class AppSettings
{
public required string DownloadUrl { get; set; }
}
public class EncryptionSettings
{
public required string Key { get; set; }
public required string IV { get; set; }
}