Unify logic of generating the webside unlike a different version we had for MVC and Minimal API

This commit is contained in:
2024-01-18 15:57:01 -08:00
parent 2d95fcb1a1
commit 0c3edd64f3
5 changed files with 89 additions and 128 deletions

View File

@@ -1,9 +1,5 @@
using Microsoft.AspNetCore.Mvc;
using AS1024.GeoFeed.Core.Interfaces;
using Microsoft.Extensions.Caching.Memory;
using AS1024.GeoFeed.Models;
using System.Text;
using AS1024.GeoFeed.Core.Tools;
using AS1024.GeoFeed.Core.GeoFeedLogic;
namespace AS1024.GeoFeed.Controllers
{
@@ -13,77 +9,18 @@ namespace AS1024.GeoFeed.Controllers
public class GeofeedController : ControllerBase
{
private readonly IGeoFeedProvider builder;
private readonly IMemoryCache memoryCache;
private readonly IWebHostEnvironment environment;
private readonly ILogger<GeofeedController> logger;
private readonly IGeoFeedPersistentCacheProvider geoFeedPersistentCache;
private const string GeoFeedCacheKey = "GeoFeedData";
private readonly GeoFeedReturn feedReturn;
public GeofeedController(IGeoFeedProvider builder,
IMemoryCache memoryCache,
IWebHostEnvironment environment,
ILogger<GeofeedController> logger,
IGeoFeedPersistentCacheProvider geoFeedPersistentCache) {
this.logger = logger;
this.geoFeedPersistentCache = geoFeedPersistentCache;
this.builder = builder;
this.memoryCache = memoryCache;
this.environment = environment;
public GeofeedController(GeoFeedReturn feedReturn)
{
this.feedReturn = feedReturn;
}
[HttpGet]
[Route("")]
public async Task<IActionResult> Get()
public async Task<IResult> Get()
{
bool isCached = true;
try
{
if (!memoryCache.TryGetValue(GeoFeedCacheKey, out List<IPGeoFeed>? feed))
{
isCached = false;
feed = await builder.GetGeoFeedData();
if (environment.IsProduction())
{
MemoryCacheEntryOptions cacheEntryOptions = new MemoryCacheEntryOptions()
.SetSlidingExpiration(TimeSpan.FromMinutes(15));
memoryCache.Set(GeoFeedCacheKey, feed, cacheEntryOptions);
}
}
return ReturnFile(feed, isCached);
} catch (HttpRequestException ex)
{
logger.LogWarning($"Temporary failure of retrieving GeoData from upstream. {ex}");
var cachedData = geoFeedPersistentCache.GetGeoFeed();
return ReturnFile(cachedData);
}
catch (Exception ex)
{
logger.LogError($"Geofeed generation failed. Exception: {ex}");
return StatusCode(500);
}
}
[NonAction]
private IActionResult ReturnFile(List<IPGeoFeed>? feed, bool isCached = false)
{
string csvContent = feed.ToGeoFeedCsv(true, isCached); // Assuming ToGeoFeedCsv() returns a string in CSV format.
return ReturnFile(csvContent);
}
[NonAction]
private IActionResult ReturnFile(string csvContent)
{
byte[] contentBytes = Encoding.UTF8.GetBytes(csvContent);
string contentType = "text/csv";
return new FileContentResult(contentBytes, contentType)
{
FileDownloadName = "geofeed.csv"
};
return await feedReturn.GetGeoFeed();
}
}
}

View File

@@ -4,6 +4,7 @@ using AS1024.GeoFeed.Core.GeoFeedProviders;
using Microsoft.EntityFrameworkCore;
using AS1024.GeoFeed.Core.GeoFeedSqliteLocalCache;
using AS1024.GeoFeed.Core.CacheService;
using AS1024.GeoFeed.Core.GeoFeedLogic;
namespace AS1024.GeoFeed
{
@@ -15,6 +16,7 @@ namespace AS1024.GeoFeed
builder.Services.AddHostedService<PreLoadGeoFeed>();
builder.Services.AddTransient<IGeoFeedProvider, NetBoxGeoFeedProvider>();
builder.Services.AddScoped<GeoFeedReturn>();
builder.Services.AddDbContext<GeoFeedCacheDbContext>(
options =>
{