using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Options; using System.Diagnostics; using System.Text.Json; using WebmrAPI.Configuration; using WebmrAPI.Models; // Добавляем using для моделей using WebmrAPI.Services; var builder = WebApplication.CreateBuilder(args); // Загрузка конфигурации builder.Services.Configure(builder.Configuration); // Добавляем логирование builder.Services.AddLogging(config => { config.AddConsole(); config.AddDebug(); }); // Регистрация ProcessMonitor как Singleton и как IHostedService builder.Services.AddSingleton(); builder.Services.AddHostedService(sp => sp.GetRequiredService()); // Регистрация WinApi как Singleton (или можно сделать его статическим, как сейчас) // Если WinApi станет нестатическим и будет иметь зависимости, регистрируйте его здесь: // builder.Services.AddSingleton(); // Если это класс, а не статический var app = builder.Build(); // Привязываем URL из настроек var appSettings = app.Services.GetRequiredService>().Value; app.Urls.Add(appSettings.WebServer.Url); // GET /api/processes app.MapGet("/api/processes", (ProcessMonitor monitor, [FromQuery] bool pretty = false) => { return Results.Content(JsonSerializer.Serialize(monitor.Processes, new JsonSerializerOptions { WriteIndented = pretty }), "application/json"); }); // GET /api/processes/{pid}/memory_regions app.MapGet("/api/processes/{pid}/memory_regions", (int pid, ProcessMonitor monitor, [FromQuery] bool pretty = false) => { var regions = monitor.GetBufferedMemoryRegions(pid); if (regions == null) { return Results.NotFound($"Process with PID {pid} or its memory regions not found."); } return Results.Content(JsonSerializer.Serialize(regions, new JsonSerializerOptions { WriteIndented = pretty }), "application/json"); }); // GET /api/status/last_modified app.MapGet("/api/status/last_modified", (ProcessMonitor monitor, [FromQuery] bool pretty = false) => { var date = new { LastModified = monitor.LastModifiedTimestamp.ToString("o") }; return Results.Content(JsonSerializer.Serialize(date, new JsonSerializerOptions { WriteIndented = pretty }), "application/json"); }); app.Run();