1
0
mirror of https://github.com/ncblakely/GiantsTools synced 2024-11-22 14:15:37 +01:00
GiantsTools/Giants.WebApi/Controllers/ServersController.cs

92 lines
3.2 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using AutoMapper;
2020-08-16 11:03:10 +02:00
using Giants.DataContract.V1;
using Giants.Services;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
namespace Giants.Web.Controllers
{
[ApiController]
2020-10-12 23:42:44 +02:00
[ApiVersion("1.0")]
[Route("api/[controller]")]
public class ServersController : ControllerBase
{
private readonly IMapper mapper;
private readonly IHttpContextAccessor httpContextAccessor;
private readonly ILogger<ServersController> logger;
private readonly IServerRegistryService serverRegistryService;
public ServersController(
IMapper mapper,
IHttpContextAccessor httpContextAccessor,
ILogger<ServersController> logger,
IServerRegistryService serverRegistryService)
{
this.mapper = mapper;
this.httpContextAccessor = httpContextAccessor;
this.logger = logger;
this.serverRegistryService = serverRegistryService;
}
2020-08-10 07:11:10 +02:00
[HttpDelete]
public async Task DeleteServer()
{
string requestIpAddress = this.GetRequestIpAddress();
this.logger.LogInformation("Deleting server from {IPAddress}", requestIpAddress);
await this.serverRegistryService.DeleteServer(requestIpAddress);
}
[HttpGet]
2020-08-16 11:03:10 +02:00
public async Task<IEnumerable<ServerInfoWithHostAddress>> GetServers()
{
IEnumerable<Services.ServerInfo> serverInfo = await this.serverRegistryService.GetAllServers();
IMapper mapper = Services.Mapper.GetMapper();
2020-08-09 02:52:26 +02:00
var mappedServers = serverInfo
.Select(x =>
{
var serverInfo = mapper.Map<ServerInfoWithHostAddress>(x);
serverInfo.HostIpAddress = x.HostIpAddress;
return serverInfo;
})
.ToList();
2020-08-09 02:52:26 +02:00
string requestIpAddress = this.GetRequestIpAddress();
this.logger.LogInformation("Returning {Count} servers to {IPAddress}", mappedServers.Count, requestIpAddress);
2020-08-09 02:52:26 +02:00
return mappedServers;
}
[HttpPost]
2020-08-16 11:03:10 +02:00
public async Task AddServer([FromBody] DataContract.V1.ServerInfo serverInfo)
{
2020-08-09 02:52:26 +02:00
ArgumentUtility.CheckForNull(serverInfo, nameof(serverInfo));
string requestIpAddress = this.GetRequestIpAddress();
this.logger.LogInformation("Request to add server from {IPAddress}", requestIpAddress);
var serverInfoEntity = this.mapper.Map<Services.ServerInfo>(serverInfo);
serverInfoEntity.HostIpAddress = requestIpAddress.ToString();
serverInfoEntity.LastHeartbeat = DateTime.UtcNow;
2020-08-09 01:31:16 +02:00
await this.serverRegistryService.AddServer(serverInfoEntity);
2020-08-09 02:52:26 +02:00
this.logger.LogInformation("Added server successfully for {IPAddress}", requestIpAddress);
}
private string GetRequestIpAddress()
{
return this.httpContextAccessor.HttpContext.Connection.RemoteIpAddress.MapToIPv4().ToString();
}
}
}