From 53d1a643b95b067438bbc48934069d761785ec91 Mon Sep 17 00:00:00 2001 From: Rory& Date: Sat, 20 Jan 2024 16:12:11 +0100 Subject: Add graph, add start/stop/kill/restart --- SystemdCtl/Controllers/UnitController.cs | 47 ++++++++++++++++++++++++++++++-- 1 file changed, 45 insertions(+), 2 deletions(-) (limited to 'SystemdCtl') diff --git a/SystemdCtl/Controllers/UnitController.cs b/SystemdCtl/Controllers/UnitController.cs index a128584..da1c534 100644 --- a/SystemdCtl/Controllers/UnitController.cs +++ b/SystemdCtl/Controllers/UnitController.cs @@ -1,3 +1,7 @@ +using System.Runtime.InteropServices; +using System.Text; +using ArcaneLibs.Extensions; +using ArcaneLibs.Extensions.Streams; using LibSystemdCli; using LibSystemdCli.Models; using Microsoft.AspNetCore.Mvc; @@ -17,15 +21,54 @@ public class UnitController : ControllerBase } } + // [HttpGet("unit/{serviceName}/logs")] + // public async IAsyncEnumerable GetUnitLogs(string serviceName, [FromQuery] int contextLines = 100) + // { + // await foreach (var log in SystemdExecutor.GetUnitLogs(serviceName, contextLines: contextLines)) + // { + // Console.WriteLine(log.Message); + // yield return log; + // await Response.Body.FlushAsync(); + // } + // } [HttpGet("unit/{serviceName}/logs")] - public async IAsyncEnumerable GetUnitLogs(string serviceName, [FromQuery] int contextLines = 100) + public async Task GetUnitLogs(string serviceName, [FromQuery] int contextLines = 100) { + Response.ContentType = "application/json"; + await Response.StartAsync(); + await Response.Body.WriteAsync("[\n"u8.ToArray()); await foreach (var log in SystemdExecutor.GetUnitLogs(serviceName, contextLines: contextLines)) { Console.WriteLine(log.Message); - yield return log; + var bytes = Encoding.UTF8.GetBytes($" {log.ToJson(indent: false)},\n"); + await Response.Body.WriteAsync(bytes); await Response.Body.FlushAsync(); } + await Response.Body.WriteAsync("]\n"u8.ToArray()); + await Response.Body.FlushAsync(); + await Response.CompleteAsync(); + } + + [HttpGet("unit/{serviceName}/dataStream")] + public async Task GetUnitDataStream(string serviceName, [FromQuery] int contextLines = 100) + { + Response.ContentType = "application/json"; + await Response.StartAsync(); + await Response.Body.WriteAsync("[\n"u8.ToArray()); + var oldData = await SystemdExecutor.GetUnitData(serviceName); + while(true) + { + var data = await SystemdExecutor.GetUnitData(serviceName); + data.SetOldData(oldData); + var bytes = Encoding.UTF8.GetBytes($" {data.ToJson(indent: false)},\n"); + await Response.Body.WriteAsync(bytes); + await Response.Body.FlushAsync(); + oldData = data; + await Task.Delay(1000); + } + await Response.Body.WriteAsync("]\n"u8.ToArray()); + await Response.Body.FlushAsync(); + await Response.CompleteAsync(); } [HttpGet("unit/{serviceName}/data")] -- cgit 1.4.1