79 lines
2.5 KiB
C#
79 lines
2.5 KiB
C#
using ListmonkIntegration.Models;
|
|
using ListmonkIntegration.Services;
|
|
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.Extensions.Options;
|
|
|
|
var builder = WebApplication.CreateBuilder(args);
|
|
// Ãëîáàëüíûé îáðàáîò÷èê íåîáðàáîòàííûõ èñêëþ÷åíèé
|
|
AppDomain.CurrentDomain.UnhandledException += (sender, args) =>
|
|
{
|
|
var exception = args.ExceptionObject as Exception;
|
|
Console.WriteLine($"[CRITICAL] Unhandled exception: {exception?.Message}");
|
|
Console.WriteLine($"[CRITICAL] Stack trace: {exception?.StackTrace}");
|
|
if (exception?.InnerException != null)
|
|
{
|
|
Console.WriteLine($"[CRITICAL] Inner exception: {exception.InnerException.Message}");
|
|
}
|
|
};
|
|
|
|
// Äîáàâëÿåì ñåðâèñû Razor Pages
|
|
builder.Services.AddRazorPages();
|
|
|
|
// Íàñòðîéêà HttpClient
|
|
builder.Services.AddHttpClient();
|
|
|
|
// ×òåíèå íàñòðîåê èç appsettings.json
|
|
builder.Services.Configure<ListmonkSettings>(
|
|
builder.Configuration.GetSection("Listmonk"));
|
|
|
|
// Ðåãèñòðàöèÿ ñåðâèñà Listmonk
|
|
builder.Services.AddSingleton<IListmonkService>(sp =>
|
|
{
|
|
var httpClientFactory = sp.GetRequiredService<IHttpClientFactory>();
|
|
var settings = sp.GetRequiredService<IOptions<ListmonkSettings>>().Value;
|
|
var logger = sp.GetRequiredService<ILogger<ListmonkService>>();
|
|
var httpClient = httpClientFactory.CreateClient();
|
|
return new ListmonkService(httpClient, settings, logger);
|
|
});
|
|
|
|
var app = builder.Build();
|
|
|
|
// Íàñòðîéêà êîíâåéåðà HTTP-çàïðîñîâ
|
|
if (!app.Environment.IsDevelopment())
|
|
{
|
|
app.UseExceptionHandler("/Error");
|
|
app.UseHsts();
|
|
}
|
|
|
|
app.UseHttpsRedirection();
|
|
app.UseStaticFiles();
|
|
|
|
app.UseRouting();
|
|
|
|
app.UseAuthorization();
|
|
app.MapGet("/api/templates/{id}/preview", async (int id, IListmonkService service, ILogger<Program> logger) =>
|
|
{
|
|
logger.LogInformation($"API: Getting template {id} preview");
|
|
var body = await service.GetTemplateBodyAsync(id);
|
|
return Results.Json(new { body });
|
|
});
|
|
// Êàñòîìíûé ìàðøðóò äëÿ API àòðèáóòîâ
|
|
// API endpoint äëÿ ïîëó÷åíèÿ àòðèáóòîâ ñïèñêà
|
|
app.MapGet("/api/lists/{id}/attributes", async (int id, IListmonkService service, ILogger<Program> logger) =>
|
|
{
|
|
logger.LogInformation($"API: Getting attributes for list {id}");
|
|
try
|
|
{
|
|
var attributes = await service.GetListAttributesAsync(id);
|
|
logger.LogInformation($"API: Found {attributes.Count} attributes");
|
|
return Results.Json(attributes);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
logger.LogError(ex, "Error getting attributes");
|
|
return Results.Json(new List<string>(), statusCode: 500);
|
|
}
|
|
});
|
|
app.MapRazorPages();
|
|
|
|
app.Run(); |