mirror of
https://github.com/Jetsparrow/jetherald.git
synced 2026-01-21 07:56:09 +03:00
Allow sending heartbeats through query parameters
This commit is contained in:
parent
7892bfffc0
commit
2a1e79fa01
@ -1,7 +1,12 @@
|
|||||||
using System;
|
using System;
|
||||||
|
using System.IO;
|
||||||
using System.Runtime.Serialization;
|
using System.Runtime.Serialization;
|
||||||
|
using System.Text;
|
||||||
|
using System.Text.Json;
|
||||||
|
using System.Text.Json.Serialization;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using Microsoft.AspNetCore.Mvc.ModelBinding;
|
||||||
|
|
||||||
namespace JetHerald.Controllers
|
namespace JetHerald.Controllers
|
||||||
{
|
{
|
||||||
@ -17,9 +22,41 @@ namespace JetHerald.Controllers
|
|||||||
Db = db;
|
Db = db;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// tfw when you need to manually parse body and query
|
||||||
[Route("api/heartbeat")]
|
[Route("api/heartbeat")]
|
||||||
[HttpPost]
|
[HttpPost]
|
||||||
public async Task<IActionResult> Heartbeat([FromBody] HeartbeatArgs args)
|
public async Task<IActionResult> Heartbeat()
|
||||||
|
{
|
||||||
|
var q = Request.Query;
|
||||||
|
if (q.ContainsKey("Topic")
|
||||||
|
&& q.ContainsKey("ExpiryTimeout")
|
||||||
|
&& q.ContainsKey("WriteToken"))
|
||||||
|
{
|
||||||
|
HeartbeatArgs args = new();
|
||||||
|
args.Topic = q["Topic"];
|
||||||
|
args.WriteToken = q["WriteToken"];
|
||||||
|
if (!int.TryParse(q["ExpiryTimeout"], out args.ExpiryTimeout))
|
||||||
|
{
|
||||||
|
return BadRequest();
|
||||||
|
}
|
||||||
|
return await DoHeartbeat(args);
|
||||||
|
}
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var args = await JsonSerializer.DeserializeAsync<HeartbeatArgs>(HttpContext.Request.Body, new JsonSerializerOptions()
|
||||||
|
{
|
||||||
|
IncludeFields = true
|
||||||
|
});
|
||||||
|
return await DoHeartbeat(args);
|
||||||
|
}
|
||||||
|
catch (JsonException)
|
||||||
|
{
|
||||||
|
return BadRequest();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private async Task<IActionResult> DoHeartbeat(HeartbeatArgs args)
|
||||||
{
|
{
|
||||||
var t = await Db.GetTopic(args.Topic);
|
var t = await Db.GetTopic(args.Topic);
|
||||||
if (t == null)
|
if (t == null)
|
||||||
@ -35,12 +72,11 @@ namespace JetHerald.Controllers
|
|||||||
return new OkResult();
|
return new OkResult();
|
||||||
}
|
}
|
||||||
|
|
||||||
[DataContract]
|
|
||||||
public class HeartbeatArgs
|
public class HeartbeatArgs
|
||||||
{
|
{
|
||||||
[DataMember] public string Topic { get; set; }
|
[JsonPropertyName("Topic")] public string Topic;
|
||||||
[DataMember] public int ExpiryTimeout { get; set; }
|
[JsonPropertyName("ExpiryTimeout")] public int ExpiryTimeout;
|
||||||
[DataMember] public string WriteToken { get; set; }
|
[JsonPropertyName("WriteToken")] public string WriteToken;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue
Block a user