Compare commits
3 Commits
8a18e71a6f
...
01ccc1ab91
| Author | SHA1 | Date |
|---|---|---|
|
|
01ccc1ab91 | |
|
|
14f8ea618a | |
|
|
04d00e0fe9 |
|
|
@ -10,7 +10,6 @@
|
|||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.19.5" />
|
||||
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
|
|
|||
|
|
@ -16,11 +16,14 @@ namespace AS1024.GeoFeed.Controllers
|
|||
private readonly IGeoFeedProvider builder;
|
||||
private readonly IMemoryCache memoryCache;
|
||||
private readonly IWebHostEnvironment environment;
|
||||
private readonly ILogger<GeofeedController> logger;
|
||||
private const string GeoFeedCacheKey = "GeoFeedData";
|
||||
|
||||
public GeofeedController(IGeoFeedProvider builder,
|
||||
IMemoryCache memoryCache,
|
||||
IWebHostEnvironment environment) {
|
||||
IWebHostEnvironment environment,
|
||||
ILogger<GeofeedController> logger) {
|
||||
this.logger = logger;
|
||||
this.builder = builder;
|
||||
this.memoryCache = memoryCache;
|
||||
this.environment = environment;
|
||||
|
|
@ -30,26 +33,32 @@ namespace AS1024.GeoFeed.Controllers
|
|||
[Route("")]
|
||||
public async Task<IActionResult> Get()
|
||||
{
|
||||
if (!memoryCache.TryGetValue(GeoFeedCacheKey, out List<IPGeoFeed>? feed)
|
||||
&& environment.IsProduction())
|
||||
try
|
||||
{
|
||||
feed = await builder.GetGeoFeedData();
|
||||
MemoryCacheEntryOptions cacheEntryOptions = new MemoryCacheEntryOptions()
|
||||
.SetSlidingExpiration(TimeSpan.FromMinutes(15));
|
||||
memoryCache.Set(GeoFeedCacheKey, feed, cacheEntryOptions);
|
||||
} else
|
||||
if (!memoryCache.TryGetValue(GeoFeedCacheKey, out List<IPGeoFeed>? feed))
|
||||
{
|
||||
feed = await builder.GetGeoFeedData();
|
||||
if (environment.IsProduction())
|
||||
{
|
||||
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"
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
using AS1024.GeoFeed.Interfaces;
|
||||
using AS1024.GeoFeed.Models;
|
||||
using Newtonsoft.Json;
|
||||
using System.Text.Json;
|
||||
using System.Net.Http.Headers;
|
||||
using System.Net.Sockets;
|
||||
using System.Web;
|
||||
|
|
@ -49,7 +49,9 @@ namespace AS1024.GeoFeed.GeoFeedBuilder
|
|||
if (result.IsSuccessStatusCode)
|
||||
{
|
||||
string stringResult = await result.Content.ReadAsStringAsync();
|
||||
jsonData = JsonConvert.DeserializeObject<NetboxData>(stringResult);
|
||||
jsonData = JsonSerializer.Deserialize<NetboxData>(stringResult, new JsonSerializerOptions {
|
||||
PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower
|
||||
});
|
||||
|
||||
if (jsonData?.Results == null || jsonData.Results.Count == 0)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -9,22 +9,26 @@ namespace AS1024.GeoFeed.GeoFeedBuilder
|
|||
private readonly ILogger<PreLoadGeoFeed> logger;
|
||||
private readonly IGeoFeedProvider provider;
|
||||
private readonly IMemoryCache memoryCache;
|
||||
private readonly IWebHostEnvironment environment;
|
||||
private const string GeoFeedCacheKey = "GeoFeedData";
|
||||
|
||||
public PreLoadGeoFeed(ILogger<PreLoadGeoFeed> logger,
|
||||
IGeoFeedProvider provider,
|
||||
IMemoryCache memoryCache)
|
||||
IMemoryCache memoryCache,
|
||||
IWebHostEnvironment environment)
|
||||
{
|
||||
this.logger = logger;
|
||||
this.provider = provider;
|
||||
this.memoryCache = memoryCache;
|
||||
this.environment = environment;
|
||||
}
|
||||
|
||||
async Task IHostedService.StartAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
try
|
||||
{
|
||||
await StartPreLoad();
|
||||
if (environment.IsProduction())
|
||||
await StartPreLoad();
|
||||
} catch (Exception ex)
|
||||
{
|
||||
logger.LogWarning($"Failed to preload, exception settings below:\n{ex}");
|
||||
|
|
|
|||
|
|
@ -1,5 +1,4 @@
|
|||
using Newtonsoft.Json;
|
||||
|
||||
using System.Text.Json.Serialization;
|
||||
namespace AS1024.GeoFeed.Models
|
||||
{
|
||||
public class NetboxData
|
||||
|
|
@ -12,21 +11,15 @@ namespace AS1024.GeoFeed.Models
|
|||
public class Result
|
||||
{
|
||||
public string? Prefix { get; set; }
|
||||
[JsonProperty("custom_fields")]
|
||||
public CustomFields? CustomFields { get; set; }
|
||||
}
|
||||
|
||||
public class CustomFields
|
||||
{
|
||||
[JsonProperty("geoloc_city")]
|
||||
public string? GeolocCity { get; set; }
|
||||
[JsonProperty("geoloc_country")]
|
||||
public string? GeolocCountry { get; set; }
|
||||
[JsonProperty("geoloc_has_location")]
|
||||
public bool? GeolocHasLocation { get; set; }
|
||||
[JsonProperty("geoloc_region")]
|
||||
public string? GeolocRegion { get; set; }
|
||||
[JsonProperty("geoloc_postal_code")]
|
||||
public string? GeolocPostalCode { get; set; }
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue