60 lines
2.2 KiB
C#
60 lines
2.2 KiB
C#
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<AppSettings>(builder.Configuration);
|
|
|
|
// Äîáàâëÿåì ëîãèðîâàíèå
|
|
builder.Services.AddLogging(config =>
|
|
{
|
|
config.AddConsole();
|
|
config.AddDebug();
|
|
});
|
|
|
|
// Ðåãèñòðàöèÿ ProcessMonitor êàê Singleton è êàê IHostedService
|
|
builder.Services.AddSingleton<ProcessMonitor>();
|
|
builder.Services.AddHostedService(sp => sp.GetRequiredService<ProcessMonitor>());
|
|
|
|
// Ðåãèñòðàöèÿ WinApi êàê Singleton (èëè ìîæíî ñäåëàòü åãî ñòàòè÷åñêèì, êàê ñåé÷àñ)
|
|
// Åñëè WinApi ñòàíåò íåñòàòè÷åñêèì è áóäåò èìåòü çàâèñèìîñòè, ðåãèñòðèðóéòå åãî çäåñü:
|
|
// builder.Services.AddSingleton<WinApi>(); // Åñëè ýòî êëàññ, à íå ñòàòè÷åñêèé
|
|
|
|
var app = builder.Build();
|
|
|
|
// Ïðèâÿçûâàåì URL èç íàñòðîåê
|
|
var appSettings = app.Services.GetRequiredService<IOptions<AppSettings>>().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();
|