From deedb61d415e1b62ee840511ad2fda5d936a1502 Mon Sep 17 00:00:00 2001 From: spiral Date: Mon, 21 Jun 2021 09:19:47 -0400 Subject: [PATCH] Add /v1/meta endpoint (shows shard state) --- .../Controllers/v1/MetaController.cs | 67 +++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 PluralKit.API/Controllers/v1/MetaController.cs diff --git a/PluralKit.API/Controllers/v1/MetaController.cs b/PluralKit.API/Controllers/v1/MetaController.cs new file mode 100644 index 00000000..4dd2a52e --- /dev/null +++ b/PluralKit.API/Controllers/v1/MetaController.cs @@ -0,0 +1,67 @@ +using System.Collections.Generic; +using System.Threading.Tasks; + +using System.Linq; + +using Microsoft.AspNetCore.Mvc; + +using Newtonsoft.Json.Linq; + +using PluralKit.Core; + +namespace PluralKit.API +{ + [ApiController] + [ApiVersion("1.0")] + [Route( "v{version:apiVersion}" )] + public class MetaController : ControllerBase + { + private readonly IDatabase _db; + private readonly ModelRepository _repo; + public MetaController(IDatabase db, ModelRepository repo) + { + _db = db; + _repo = repo; + } + + [HttpGet("meta")] + public async Task> GetMeta() + { + await using var conn = await _db.Obtain(); + var shards = await _repo.GetShards(conn); + + var o = new JObject(); + o.Add("shards", shards.ToJSON()); + + return Ok(o); + } + } + + public static class MetaJsonExt + { + public static JArray ToJSON(this IEnumerable shards) + { + var o = new JArray(); + + foreach (var shard in shards) + { + var s = new JObject(); + s.Add("id", shard.Id); + + if (shard.Status == PKShardInfo.ShardStatus.Down) + s.Add("status", "down"); + else + s.Add("status", "up"); + + s.Add("ping", shard.Ping); + s.Add("last_heartbeat", shard.LastHeartbeat.ToString()); + s.Add("last_connection", shard.LastConnection.ToString()); + + o.Add(s); + } + + return o; + } + + } +} \ No newline at end of file