diff options
author | Rory& <root@rory.gay> | 2024-01-20 16:12:11 +0100 |
---|---|---|
committer | Rory& <root@rory.gay> | 2024-01-20 16:12:11 +0100 |
commit | 53d1a643b95b067438bbc48934069d761785ec91 (patch) | |
tree | 877f479e04d71950006a41c1670674bcd08bf0a1 /SystemdCtl/Controllers | |
parent | Add actions and unit data (diff) | |
download | SystemdCtl-53d1a643b95b067438bbc48934069d761785ec91.tar.xz |
Diffstat (limited to 'SystemdCtl/Controllers')
-rw-r--r-- | SystemdCtl/Controllers/UnitController.cs | 47 |
1 files changed, 45 insertions, 2 deletions
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<SystemdJournalLogItem> 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<SystemdJournalLogItem> 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")] |