Keeping logic within microservice boundaries
Deploy Promiscuity Auth API / deploy (push) Successful in 45s
Deploy Promiscuity Character API / deploy (push) Successful in 57s
Deploy Promiscuity Inventory API / deploy (push) Successful in 45s
Deploy Promiscuity Locations API / deploy (push) Successful in 58s
k8s smoke test / test (push) Successful in 8s
Deploy Promiscuity Auth API / deploy (push) Successful in 45s
Deploy Promiscuity Character API / deploy (push) Successful in 57s
Deploy Promiscuity Inventory API / deploy (push) Successful in 45s
Deploy Promiscuity Locations API / deploy (push) Successful in 58s
k8s smoke test / test (push) Successful in 8s
This commit is contained in:
@@ -1,8 +1,11 @@
|
||||
using CharacterApi.Models;
|
||||
using CharacterApi.Services;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using System.Security.Claims;
|
||||
using CharacterApi.Models;
|
||||
using CharacterApi.Services;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using System.Net.Http.Headers;
|
||||
using System.Security.Claims;
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace CharacterApi.Controllers;
|
||||
|
||||
@@ -11,11 +14,15 @@ namespace CharacterApi.Controllers;
|
||||
public class CharactersController : ControllerBase
|
||||
{
|
||||
private readonly CharacterStore _characters;
|
||||
private readonly IHttpClientFactory _httpClientFactory;
|
||||
private readonly IConfiguration _configuration;
|
||||
private readonly ILogger<CharactersController> _logger;
|
||||
|
||||
public CharactersController(CharacterStore characters, ILogger<CharactersController> logger)
|
||||
public CharactersController(CharacterStore characters, IHttpClientFactory httpClientFactory, IConfiguration configuration, ILogger<CharactersController> logger)
|
||||
{
|
||||
_characters = characters;
|
||||
_httpClientFactory = httpClientFactory;
|
||||
_configuration = configuration;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
@@ -91,17 +98,70 @@ public class CharactersController : ControllerBase
|
||||
userId
|
||||
);
|
||||
|
||||
var generation = await _characters.GetOrCreateVisibleLocationsAsync(character);
|
||||
var locations = generation.Locations;
|
||||
var locationsBaseUrl = (_configuration["Services:LocationsApiBaseUrl"] ?? "http://localhost:5002").TrimEnd('/');
|
||||
var internalApiKey = (_configuration["InternalApi:Key"] ?? _configuration["Jwt:Key"] ?? string.Empty).Trim();
|
||||
var body = JsonSerializer.Serialize(new
|
||||
{
|
||||
x = character.Coord.X,
|
||||
y = character.Coord.Y,
|
||||
radius = character.VisionRadius > 0 ? character.VisionRadius : 3
|
||||
});
|
||||
|
||||
_logger.LogInformation(
|
||||
"Visible locations resolved for character {CharacterId}: generated {GeneratedCount}, returned {ReturnedCount}",
|
||||
character.Id,
|
||||
generation.GeneratedCount,
|
||||
locations.Count
|
||||
);
|
||||
var client = _httpClientFactory.CreateClient();
|
||||
using var request = new HttpRequestMessage(HttpMethod.Post, $"{locationsBaseUrl}/api/locations/internal/visible-window");
|
||||
request.Content = new StringContent(body, Encoding.UTF8, "application/json");
|
||||
request.Headers.Add("X-Internal-Api-Key", internalApiKey);
|
||||
|
||||
return Ok(locations);
|
||||
HttpResponseMessage response;
|
||||
try
|
||||
{
|
||||
response = await client.SendAsync(request);
|
||||
}
|
||||
catch (HttpRequestException ex)
|
||||
{
|
||||
_logger.LogError(ex, "Failed to reach LocationsApi while resolving visible locations for character {CharacterId}", character.Id);
|
||||
return StatusCode(StatusCodes.Status502BadGateway, new
|
||||
{
|
||||
type = "https://httpstatuses.com/502",
|
||||
title = "Bad Gateway",
|
||||
status = 502,
|
||||
detail = $"Failed to reach LocationsApi at {locationsBaseUrl}.",
|
||||
traceId = HttpContext.TraceIdentifier
|
||||
});
|
||||
}
|
||||
|
||||
using (response)
|
||||
{
|
||||
var responseBody = await response.Content.ReadAsStringAsync();
|
||||
if (!response.IsSuccessStatusCode)
|
||||
return StatusCode((int)response.StatusCode, responseBody);
|
||||
|
||||
var generation = JsonSerializer.Deserialize<VisibleLocationWindowResponse>(
|
||||
responseBody,
|
||||
new JsonSerializerOptions { PropertyNameCaseInsensitive = true });
|
||||
if (generation is null)
|
||||
{
|
||||
return StatusCode(StatusCodes.Status502BadGateway, new
|
||||
{
|
||||
type = "https://httpstatuses.com/502",
|
||||
title = "Bad Gateway",
|
||||
status = 502,
|
||||
detail = "LocationsApi returned an unreadable visible locations payload.",
|
||||
traceId = HttpContext.TraceIdentifier
|
||||
});
|
||||
}
|
||||
|
||||
var locations = generation.Locations;
|
||||
|
||||
_logger.LogInformation(
|
||||
"Visible locations resolved for character {CharacterId}: generated {GeneratedCount}, returned {ReturnedCount}",
|
||||
character.Id,
|
||||
generation.GeneratedCount,
|
||||
locations.Count
|
||||
);
|
||||
|
||||
return Ok(locations);
|
||||
}
|
||||
}
|
||||
|
||||
[HttpPut("{id}/coord")]
|
||||
|
||||
Reference in New Issue
Block a user