Some minor housekeeping
This commit is contained in:
15
AS1024.GeoFeed.MinimalAPI/AppJsonSerializerContext.cs
Normal file
15
AS1024.GeoFeed.MinimalAPI/AppJsonSerializerContext.cs
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
using AS1024.GeoFeed.Models;
|
||||||
|
using System.Text.Json.Serialization;
|
||||||
|
|
||||||
|
namespace AS1024.GeoFeed.MinimalAPI
|
||||||
|
{
|
||||||
|
[JsonSerializable(typeof(NetboxData))]
|
||||||
|
[JsonSerializable(typeof(Result))]
|
||||||
|
[JsonSerializable(typeof(CustomFields))]
|
||||||
|
[JsonSourceGenerationOptions(PropertyNamingPolicy = JsonKnownNamingPolicy.SnakeCaseLower)]
|
||||||
|
internal partial class AppJsonSerializerContext : JsonSerializerContext
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -6,8 +6,9 @@ using AS1024.GeoFeed.Models;
|
|||||||
using Microsoft.AspNetCore.Http.HttpResults;
|
using Microsoft.AspNetCore.Http.HttpResults;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
using Microsoft.Extensions.Caching.Memory;
|
using Microsoft.Extensions.Caching.Memory;
|
||||||
|
using Microsoft.Extensions.Hosting;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
using System;
|
using System;
|
||||||
using System.Text.Json.Serialization;
|
|
||||||
|
|
||||||
namespace AS1024.GeoFeed.MinimalAPI
|
namespace AS1024.GeoFeed.MinimalAPI
|
||||||
{
|
{
|
||||||
@@ -27,50 +28,59 @@ namespace AS1024.GeoFeed.MinimalAPI
|
|||||||
options.SerializerOptions.TypeInfoResolverChain.Insert(0, AppJsonSerializerContext.Default);
|
options.SerializerOptions.TypeInfoResolverChain.Insert(0, AppJsonSerializerContext.Default);
|
||||||
});
|
});
|
||||||
var app = builder.Build();
|
var app = builder.Build();
|
||||||
var geoFeed = app.Map("/geofeed.csv", async (IGeoFeedProvider provider,
|
|
||||||
|
app.Map("/geofeed.csv", async (IGeoFeedProvider provider,
|
||||||
ILogger<Program> logger,
|
ILogger<Program> logger,
|
||||||
IGeoFeedPersistentCacheProvider cacheProvider,
|
IGeoFeedPersistentCacheProvider cacheProvider,
|
||||||
IMemoryCache memoryCache,
|
IMemoryCache memoryCache,
|
||||||
IWebHostEnvironment environment) => {
|
IWebHostEnvironment environment) => {
|
||||||
try
|
return await GeoFeedDataRunner(provider, logger, cacheProvider, memoryCache, environment);
|
||||||
{
|
});
|
||||||
if (!memoryCache.TryGetValue("Geofeed", out List<IPGeoFeed>? feed))
|
|
||||||
{
|
|
||||||
feed = await provider.GetGeoFeedData();
|
|
||||||
if (environment.IsProduction())
|
|
||||||
{
|
|
||||||
MemoryCacheEntryOptions cacheEntryOptions = new MemoryCacheEntryOptions()
|
|
||||||
.SetSlidingExpiration(TimeSpan.FromMinutes(15));
|
|
||||||
memoryCache.Set("Geofeed", feed, cacheEntryOptions);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return feed.ToGeoFeedCsv();
|
app.Map("/geofeed", async (IGeoFeedProvider provider,
|
||||||
}
|
ILogger<Program> logger,
|
||||||
catch (HttpRequestException ex)
|
IGeoFeedPersistentCacheProvider cacheProvider,
|
||||||
{
|
IMemoryCache memoryCache,
|
||||||
logger.LogWarning($"Temporary failure of retrieving GeoData from upstream. {ex}");
|
IWebHostEnvironment environment) => {
|
||||||
string geoFeedData = cacheProvider.GetGeoFeed();
|
return await GeoFeedDataRunner(provider, logger, cacheProvider, memoryCache, environment);
|
||||||
return geoFeedData;
|
});
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
logger.LogError($"Error: {ex}");
|
|
||||||
}
|
|
||||||
|
|
||||||
return "";
|
|
||||||
});
|
|
||||||
app.Run();
|
app.Run();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected static async Task<string> GeoFeedDataRunner(IGeoFeedProvider provider,
|
||||||
|
ILogger<Program> logger,
|
||||||
|
IGeoFeedPersistentCacheProvider cacheProvider,
|
||||||
|
IMemoryCache memoryCache,
|
||||||
|
IWebHostEnvironment environment)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
if (!memoryCache.TryGetValue("Geofeed", out List<IPGeoFeed>? feed))
|
||||||
|
{
|
||||||
|
feed = await provider.GetGeoFeedData();
|
||||||
|
if (environment.IsProduction())
|
||||||
|
{
|
||||||
|
MemoryCacheEntryOptions cacheEntryOptions = new MemoryCacheEntryOptions()
|
||||||
|
.SetSlidingExpiration(TimeSpan.FromMinutes(15));
|
||||||
|
memoryCache.Set("Geofeed", feed, cacheEntryOptions);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return feed.ToGeoFeedCsv();
|
||||||
|
}
|
||||||
|
catch (HttpRequestException ex)
|
||||||
|
{
|
||||||
|
logger.LogWarning($"Temporary failure of retrieving GeoData from upstream. {ex}");
|
||||||
|
string geoFeedData = cacheProvider.GetGeoFeed();
|
||||||
|
return geoFeedData;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
logger.LogError($"Error: {ex}");
|
||||||
|
}
|
||||||
|
|
||||||
|
return "";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
[JsonSerializable(typeof(NetboxData))]
|
|
||||||
[JsonSerializable(typeof(Result))]
|
|
||||||
[JsonSerializable(typeof(CustomFields))]
|
|
||||||
[JsonSourceGenerationOptions(PropertyNamingPolicy = JsonKnownNamingPolicy.SnakeCaseLower)]
|
|
||||||
internal partial class AppJsonSerializerContext : JsonSerializerContext
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user