Ask or search…
⌃K
Links

API + Perigee

Grab the code from the blueprint section for API hosting with Perigee

Admin controller example

In this example we create a controller that allows us to turn threads on and off.
  1. 1.
    Simply add a new empty "API Controller" to the Controllers folder.
  2. 2.
    Call it AdminController.cs
  3. 3.
    Paste the code below in to replace the content
using Microsoft.AspNetCore.Mvc;
using Perigee;
using Perigee.Helpers;
​
// Visit https://docs.perigee.software to learn more
// Visit https://perigee.software to purchase a license!
​
namespace PerigeeIntroAPI.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class AdminController : ControllerBase
{
public ThreadRegistry reg { get; }
​
public AdminController(ThreadRegistry tr)
{
this.reg = tr;
}
​
​
[HttpGet]
public IActionResult Taskcontrol([FromQuery] string task, [FromQuery] bool start)
{
//Does this thread exist?
if (reg.ContainsThreadByName(task))
{
if (start)
{
reg.StartIfNotRunning(task); //Start if not started
return Ok();
}
else
{
reg.QueueStop(task, true); //Stop and do not auto restart
return Ok();
}
}
​
return BadRequest();
}
​
[Route("cron")]
[HttpGet]
public IActionResult cron([FromQuery] string name, [FromQuery] string cron, [FromQuery] string result)
{
if (reg.ContainsThreadByName(name) || string.IsNullOrEmpty(cron) || string.IsNullOrEmpty(result)) return BadRequest();
​
reg.AddManagedThread(new ManagedThread(name, (ct, l) =>
{
l.LogInformation("result: {result}", result);
}, cron, reg.CTS, reg.GetLogger<AdminController>()));
​
return new JsonResult(new { Message = $"Will run a CRON thread named {name} {Cron.Parse(cron).ToString()}" });
}
​
[Route("delete")]
[HttpDelete]
public IActionResult remove([FromQuery] string name)
{
reg.RemoveThreadByName(name);
return Ok();
}
​
[Route("list")]
[HttpGet]
public IActionResult list()
{
return new JsonResult(new { Threads = reg.GetThreads().Select(f => new { Name = f.Name, Runtime = f.RunTime }).ToArray() });
}
​
[Route("reboot")]
[HttpGet]
public IActionResult reboot()
{
reg.RestartAllThreads();
return Ok();
}
​
[Route("exec")]
[HttpGet]
public IActionResult exec([FromQuery] string name)
{
reg.ExecuteCRONNow(name);
return Ok();
}
​
​
}
}
​
In this example we've created a simple endpoint we can call to turn anything on or off by simply checking the thread registry for the requested thread, then turning it on or off.
The port may be different - but you can see how easy it would be to turn something on or off!
[GET] https://localhost:5001/api/admin?task=ProcessAgent&start=true
Typically speaking this would be secured or unavailable outside the firewall. Don't forget to secure your endpoints!