wabbajack/Wabbajack.BuildServer/Controllers/Heartbeat.cs

69 lines
1.7 KiB
C#
Raw Normal View History

2020-01-10 02:06:11 +00:00
using System;
using System.Collections.Generic;
2020-01-10 02:06:11 +00:00
using System.Linq;
using System.Text;
2020-01-10 02:06:11 +00:00
using System.Threading.Tasks;
2020-01-16 05:06:25 +00:00
using Microsoft.AspNetCore.Authorization;
2020-01-10 02:06:11 +00:00
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using MongoDB.Bson;
2020-01-10 02:06:11 +00:00
using Wabbajack.BuildServer.Models;
using Wabbajack.Common;
using Wabbajack.Common.StatusFeed;
2020-01-10 02:06:11 +00:00
namespace Wabbajack.BuildServer.Controllers
{
[Route("/heartbeat")]
public class Heartbeat : AControllerBase<Heartbeat>
{
static Heartbeat()
{
_startTime = DateTime.Now;
}
private static DateTime _startTime;
public Heartbeat(ILogger<Heartbeat> logger, DBContext db) : base(logger, db)
{
}
private const int MAX_LOG_SIZE = 128;
private static List<string> Log = new List<string>();
public static void AddToLog(IStatusMessage msg)
{
lock (Log)
{
Log.Add(msg.ToString());
if (Log.Count > MAX_LOG_SIZE)
Log.RemoveAt(0);
}
}
2020-01-10 02:06:11 +00:00
[HttpGet]
public async Task<TimeSpan> GetHeartbeat()
{
return DateTime.Now - _startTime;
}
2020-01-16 05:06:25 +00:00
[HttpGet("only-authenticated")]
[Authorize]
public IActionResult OnlyAuthenticated()
{
var message = $"Hello from {nameof(OnlyAuthenticated)}";
return new ObjectResult(message);
}
[HttpGet("logs")]
[Authorize]
public IActionResult GetLogs()
{
string[] lst;
lock (Log)
{
lst = Log.ToArray();
}
return Ok(string.Join("\n", lst));
}
2020-01-10 02:06:11 +00:00
}
}