Handle errors
This commit is contained in:
@@ -16,11 +16,14 @@ namespace AS1024.GeoFeed.Controllers
|
|||||||
private readonly IGeoFeedProvider builder;
|
private readonly IGeoFeedProvider builder;
|
||||||
private readonly IMemoryCache memoryCache;
|
private readonly IMemoryCache memoryCache;
|
||||||
private readonly IWebHostEnvironment environment;
|
private readonly IWebHostEnvironment environment;
|
||||||
|
private readonly ILogger<GeofeedController> logger;
|
||||||
private const string GeoFeedCacheKey = "GeoFeedData";
|
private const string GeoFeedCacheKey = "GeoFeedData";
|
||||||
|
|
||||||
public GeofeedController(IGeoFeedProvider builder,
|
public GeofeedController(IGeoFeedProvider builder,
|
||||||
IMemoryCache memoryCache,
|
IMemoryCache memoryCache,
|
||||||
IWebHostEnvironment environment) {
|
IWebHostEnvironment environment,
|
||||||
|
ILogger<GeofeedController> logger) {
|
||||||
|
this.logger = logger;
|
||||||
this.builder = builder;
|
this.builder = builder;
|
||||||
this.memoryCache = memoryCache;
|
this.memoryCache = memoryCache;
|
||||||
this.environment = environment;
|
this.environment = environment;
|
||||||
@@ -30,26 +33,32 @@ namespace AS1024.GeoFeed.Controllers
|
|||||||
[Route("")]
|
[Route("")]
|
||||||
public async Task<IActionResult> Get()
|
public async Task<IActionResult> Get()
|
||||||
{
|
{
|
||||||
if (!memoryCache.TryGetValue(GeoFeedCacheKey, out List<IPGeoFeed>? feed)
|
try
|
||||||
&& environment.IsProduction())
|
|
||||||
{
|
{
|
||||||
feed = await builder.GetGeoFeedData();
|
if (!memoryCache.TryGetValue(GeoFeedCacheKey, out List<IPGeoFeed>? feed))
|
||||||
MemoryCacheEntryOptions cacheEntryOptions = new MemoryCacheEntryOptions()
|
{
|
||||||
.SetSlidingExpiration(TimeSpan.FromMinutes(15));
|
feed = await builder.GetGeoFeedData();
|
||||||
memoryCache.Set(GeoFeedCacheKey, feed, cacheEntryOptions);
|
if (environment.IsProduction())
|
||||||
} else
|
{
|
||||||
|
MemoryCacheEntryOptions cacheEntryOptions = new MemoryCacheEntryOptions()
|
||||||
|
.SetSlidingExpiration(TimeSpan.FromMinutes(15));
|
||||||
|
memoryCache.Set(GeoFeedCacheKey, feed, cacheEntryOptions);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
string csvContent = feed.ToGeoFeedCsv(); // Assuming ToGeoFeedCsv() returns a string in CSV format.
|
||||||
|
byte[] contentBytes = Encoding.UTF8.GetBytes(csvContent);
|
||||||
|
string contentType = "text/csv";
|
||||||
|
|
||||||
|
return new FileContentResult(contentBytes, contentType)
|
||||||
|
{
|
||||||
|
FileDownloadName = "geofeed.csv"
|
||||||
|
};
|
||||||
|
} catch (Exception ex)
|
||||||
{
|
{
|
||||||
feed = await builder.GetGeoFeedData();
|
logger.LogError($"Geofeed generation failed. Exception: {ex}");
|
||||||
|
return StatusCode(500);
|
||||||
}
|
}
|
||||||
|
|
||||||
string csvContent = feed.ToGeoFeedCsv(); // Assuming ToGeoFeedCsv() returns a string in CSV format.
|
|
||||||
byte[] contentBytes = Encoding.UTF8.GetBytes(csvContent);
|
|
||||||
string contentType = "text/csv";
|
|
||||||
|
|
||||||
return new FileContentResult(contentBytes, contentType)
|
|
||||||
{
|
|
||||||
FileDownloadName = "geofeed.csv"
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user