Unify logic of generating the webside unlike a different version we had for MVC and Minimal API
This commit is contained in:
parent
2d95fcb1a1
commit
0c3edd64f3
|
|
@ -1,9 +1,10 @@
|
||||||
<Project Sdk="Microsoft.NET.Sdk">
|
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<TargetFramework>net8.0</TargetFramework>
|
<TargetFramework>net8.0</TargetFramework>
|
||||||
<ImplicitUsings>enable</ImplicitUsings>
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
<Nullable>enable</Nullable>
|
<Nullable>enable</Nullable>
|
||||||
|
<OutputType>Library</OutputType>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,70 @@
|
||||||
|
using System.Text;
|
||||||
|
using AS1024.GeoFeed.Core.Interfaces;
|
||||||
|
using AS1024.GeoFeed.Core.Tools;
|
||||||
|
using AS1024.GeoFeed.Models;
|
||||||
|
using Microsoft.Extensions.Caching.Memory;
|
||||||
|
|
||||||
|
namespace AS1024.GeoFeed.Core.GeoFeedLogic
|
||||||
|
{
|
||||||
|
public class GeoFeedReturn
|
||||||
|
{
|
||||||
|
private const string GeoFeedCacheKey = "GeoFeedData";
|
||||||
|
private readonly IGeoFeedProvider provider;
|
||||||
|
private readonly ILogger<GeoFeedReturn> logger;
|
||||||
|
private readonly IGeoFeedPersistentCacheProvider cacheProvider;
|
||||||
|
private readonly IMemoryCache memoryCache;
|
||||||
|
private readonly IWebHostEnvironment environment;
|
||||||
|
|
||||||
|
public GeoFeedReturn(IGeoFeedProvider provider,
|
||||||
|
ILogger<GeoFeedReturn> logger,
|
||||||
|
IGeoFeedPersistentCacheProvider cacheProvider,
|
||||||
|
IMemoryCache memoryCache,
|
||||||
|
IWebHostEnvironment environment)
|
||||||
|
{
|
||||||
|
this.provider = provider;
|
||||||
|
this.logger = logger;
|
||||||
|
this.cacheProvider = cacheProvider;
|
||||||
|
this.memoryCache = memoryCache;
|
||||||
|
this.environment = environment;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<IResult> GetGeoFeed()
|
||||||
|
{
|
||||||
|
bool isCached = true;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
if (!memoryCache.TryGetValue(GeoFeedCacheKey, out List<IPGeoFeed>? feed))
|
||||||
|
{
|
||||||
|
isCached = false;
|
||||||
|
feed = await provider.GetGeoFeedData();
|
||||||
|
if (environment.IsProduction())
|
||||||
|
{
|
||||||
|
MemoryCacheEntryOptions cacheEntryOptions = new MemoryCacheEntryOptions()
|
||||||
|
.SetSlidingExpiration(TimeSpan.FromMinutes(15));
|
||||||
|
memoryCache.Set(GeoFeedCacheKey, feed, cacheEntryOptions);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return Results.File(Encoding.UTF8.GetBytes(feed.ToGeoFeedCsv(true, isCached)),
|
||||||
|
"text/csv",
|
||||||
|
"geofeed.csv");
|
||||||
|
|
||||||
|
}
|
||||||
|
catch (HttpRequestException ex)
|
||||||
|
{
|
||||||
|
logger.LogWarning($"Temporary failure of retrieving GeoData from upstream. {ex}");
|
||||||
|
string geoFeedData = cacheProvider.GetGeoFeed();
|
||||||
|
|
||||||
|
return Results.File(Encoding.UTF8.GetBytes(geoFeedData),
|
||||||
|
"text/csv",
|
||||||
|
"geofeed.csv");
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
logger.LogError($"Error: {ex}");
|
||||||
|
}
|
||||||
|
|
||||||
|
return Results.NoContent();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,4 +1,5 @@
|
||||||
using AS1024.GeoFeed.Core.CacheService;
|
using AS1024.GeoFeed.Core.CacheService;
|
||||||
|
using AS1024.GeoFeed.Core.GeoFeedLogic;
|
||||||
using AS1024.GeoFeed.Core.GeoFeedPreloader;
|
using AS1024.GeoFeed.Core.GeoFeedPreloader;
|
||||||
using AS1024.GeoFeed.Core.Interfaces;
|
using AS1024.GeoFeed.Core.Interfaces;
|
||||||
using AS1024.GeoFeed.Core.Tools;
|
using AS1024.GeoFeed.Core.Tools;
|
||||||
|
|
@ -18,6 +19,7 @@ namespace AS1024.GeoFeed.MinimalAPI
|
||||||
builder.Services.AddHostedService<GeoFeedCacheService>();
|
builder.Services.AddHostedService<GeoFeedCacheService>();
|
||||||
builder.Services.AddHostedService<PreLoadGeoFeed>();
|
builder.Services.AddHostedService<PreLoadGeoFeed>();
|
||||||
builder.Services.AddTransient<IGeoFeedPersistentCacheProvider, GeoFeedLocalFileCache>();
|
builder.Services.AddTransient<IGeoFeedPersistentCacheProvider, GeoFeedLocalFileCache>();
|
||||||
|
builder.Services.AddScoped<GeoFeedReturn>();
|
||||||
builder.Services.AddMemoryCache();
|
builder.Services.AddMemoryCache();
|
||||||
builder.Services.AddLogging();
|
builder.Services.AddLogging();
|
||||||
builder.Services.AddHttpClient();
|
builder.Services.AddHttpClient();
|
||||||
|
|
@ -26,66 +28,15 @@ namespace AS1024.GeoFeed.MinimalAPI
|
||||||
});
|
});
|
||||||
var app = builder.Build();
|
var app = builder.Build();
|
||||||
|
|
||||||
app.Map("/geofeed.csv", async (IGeoFeedProvider provider,
|
app.Map("/geofeed.csv", async (GeoFeedReturn feedReturn) => {
|
||||||
ILogger<Program> logger,
|
return await feedReturn.GetGeoFeed();
|
||||||
IGeoFeedPersistentCacheProvider cacheProvider,
|
});
|
||||||
IMemoryCache memoryCache,
|
|
||||||
IWebHostEnvironment environment) => {
|
|
||||||
return await GeoFeedDataRunner(provider, logger, cacheProvider, memoryCache, environment);
|
|
||||||
});
|
|
||||||
|
|
||||||
app.Map("/geofeed", async (IGeoFeedProvider provider,
|
app.Map("/geofeed", async (GeoFeedReturn feedReturn) => {
|
||||||
ILogger<Program> logger,
|
return await feedReturn.GetGeoFeed();
|
||||||
IGeoFeedPersistentCacheProvider cacheProvider,
|
});
|
||||||
IMemoryCache memoryCache,
|
|
||||||
IWebHostEnvironment environment) => {
|
|
||||||
return await GeoFeedDataRunner(provider, logger, cacheProvider, memoryCache, environment);
|
|
||||||
});
|
|
||||||
|
|
||||||
app.Run();
|
app.Run();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected static async Task<IResult> GeoFeedDataRunner(IGeoFeedProvider provider,
|
|
||||||
ILogger<Program> logger,
|
|
||||||
IGeoFeedPersistentCacheProvider cacheProvider,
|
|
||||||
IMemoryCache memoryCache,
|
|
||||||
IWebHostEnvironment environment)
|
|
||||||
{
|
|
||||||
bool isCached = true;
|
|
||||||
try
|
|
||||||
{
|
|
||||||
if (!memoryCache.TryGetValue("GeoFeedData", out List<IPGeoFeed>? feed))
|
|
||||||
{
|
|
||||||
isCached = false;
|
|
||||||
feed = await provider.GetGeoFeedData();
|
|
||||||
if (environment.IsProduction())
|
|
||||||
{
|
|
||||||
MemoryCacheEntryOptions cacheEntryOptions = new MemoryCacheEntryOptions()
|
|
||||||
.SetSlidingExpiration(TimeSpan.FromMinutes(15));
|
|
||||||
memoryCache.Set("GeoFeedData", feed, cacheEntryOptions);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return Results.File(Encoding.UTF8.GetBytes(feed.ToGeoFeedCsv(true, isCached)),
|
|
||||||
"text/csv",
|
|
||||||
"geofeed.csv");
|
|
||||||
|
|
||||||
}
|
|
||||||
catch (HttpRequestException ex)
|
|
||||||
{
|
|
||||||
logger.LogWarning($"Temporary failure of retrieving GeoData from upstream. {ex}");
|
|
||||||
string geoFeedData = cacheProvider.GetGeoFeed();
|
|
||||||
|
|
||||||
return Results.File(Encoding.UTF8.GetBytes(geoFeedData),
|
|
||||||
"text/csv",
|
|
||||||
"geofeed.csv");
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
logger.LogError($"Error: {ex}");
|
|
||||||
}
|
|
||||||
|
|
||||||
return Results.NoContent();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,5 @@
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
using AS1024.GeoFeed.Core.Interfaces;
|
using AS1024.GeoFeed.Core.GeoFeedLogic;
|
||||||
using Microsoft.Extensions.Caching.Memory;
|
|
||||||
using AS1024.GeoFeed.Models;
|
|
||||||
using System.Text;
|
|
||||||
using AS1024.GeoFeed.Core.Tools;
|
|
||||||
|
|
||||||
namespace AS1024.GeoFeed.Controllers
|
namespace AS1024.GeoFeed.Controllers
|
||||||
{
|
{
|
||||||
|
|
@ -13,77 +9,18 @@ namespace AS1024.GeoFeed.Controllers
|
||||||
|
|
||||||
public class GeofeedController : ControllerBase
|
public class GeofeedController : ControllerBase
|
||||||
{
|
{
|
||||||
private readonly IGeoFeedProvider builder;
|
private readonly GeoFeedReturn feedReturn;
|
||||||
private readonly IMemoryCache memoryCache;
|
|
||||||
private readonly IWebHostEnvironment environment;
|
|
||||||
private readonly ILogger<GeofeedController> logger;
|
|
||||||
private readonly IGeoFeedPersistentCacheProvider geoFeedPersistentCache;
|
|
||||||
private const string GeoFeedCacheKey = "GeoFeedData";
|
|
||||||
|
|
||||||
public GeofeedController(IGeoFeedProvider builder,
|
public GeofeedController(GeoFeedReturn feedReturn)
|
||||||
IMemoryCache memoryCache,
|
{
|
||||||
IWebHostEnvironment environment,
|
this.feedReturn = feedReturn;
|
||||||
ILogger<GeofeedController> logger,
|
|
||||||
IGeoFeedPersistentCacheProvider geoFeedPersistentCache) {
|
|
||||||
this.logger = logger;
|
|
||||||
this.geoFeedPersistentCache = geoFeedPersistentCache;
|
|
||||||
this.builder = builder;
|
|
||||||
this.memoryCache = memoryCache;
|
|
||||||
this.environment = environment;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[HttpGet]
|
[HttpGet]
|
||||||
[Route("")]
|
[Route("")]
|
||||||
public async Task<IActionResult> Get()
|
public async Task<IResult> Get()
|
||||||
{
|
{
|
||||||
bool isCached = true;
|
return await feedReturn.GetGeoFeed();
|
||||||
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"
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,7 @@ using AS1024.GeoFeed.Core.GeoFeedProviders;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
using AS1024.GeoFeed.Core.GeoFeedSqliteLocalCache;
|
using AS1024.GeoFeed.Core.GeoFeedSqliteLocalCache;
|
||||||
using AS1024.GeoFeed.Core.CacheService;
|
using AS1024.GeoFeed.Core.CacheService;
|
||||||
|
using AS1024.GeoFeed.Core.GeoFeedLogic;
|
||||||
|
|
||||||
namespace AS1024.GeoFeed
|
namespace AS1024.GeoFeed
|
||||||
{
|
{
|
||||||
|
|
@ -15,6 +16,7 @@ namespace AS1024.GeoFeed
|
||||||
|
|
||||||
builder.Services.AddHostedService<PreLoadGeoFeed>();
|
builder.Services.AddHostedService<PreLoadGeoFeed>();
|
||||||
builder.Services.AddTransient<IGeoFeedProvider, NetBoxGeoFeedProvider>();
|
builder.Services.AddTransient<IGeoFeedProvider, NetBoxGeoFeedProvider>();
|
||||||
|
builder.Services.AddScoped<GeoFeedReturn>();
|
||||||
builder.Services.AddDbContext<GeoFeedCacheDbContext>(
|
builder.Services.AddDbContext<GeoFeedCacheDbContext>(
|
||||||
options =>
|
options =>
|
||||||
{
|
{
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue