using LibSystemdCli; using LibSystemdCli.Models; using Microsoft.AspNetCore.Mvc; namespace SystemdCtl.Controllers; [ApiController] [Route("/api/")] public class UnitController : ControllerBase { [HttpGet("listUnits")] public async IAsyncEnumerable GetUnits() { await foreach (var unit in SystemdExecutor.GetUnits()) { yield return unit; await Response.Body.FlushAsync(); } } [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}/data")] public Task GetUnitData(string serviceName) => SystemdExecutor.GetUnitData(serviceName); [HttpGet("unit/{serviceName}/start")] public Task StartUnit(string serviceName) => CommandExecutor.ExecuteCommand("systemctl", $"start {serviceName}"); [HttpGet("unit/{serviceName}/stop")] public Task StopUnit(string serviceName) => CommandExecutor.ExecuteCommand("systemctl", $"stop {serviceName}"); [HttpGet("unit/{serviceName}/restart")] public Task RestartUnit(string serviceName) => CommandExecutor.ExecuteCommand("systemctl", $"restart {serviceName}"); [HttpGet("unit/{serviceName}/reload")] public Task ReloadUnit(string serviceName) => CommandExecutor.ExecuteCommand("systemctl", $"reload {serviceName}"); [HttpGet("unit/{serviceName}/enable")] public Task EnableUnit(string serviceName) => CommandExecutor.ExecuteCommand("systemctl", $"enable {serviceName}"); [HttpGet("unit/{serviceName}/disable")] public Task DisableUnit(string serviceName) => CommandExecutor.ExecuteCommand("systemctl", $"disable {serviceName}"); }