summary refs log tree commit diff
path: root/SystemdCtl
diff options
context:
space:
mode:
Diffstat (limited to 'SystemdCtl')
-rw-r--r--SystemdCtl/Controllers/UnitController.cs47
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")]