Compare commits
13 Commits
compartmen
...
626ab99888
| Author | SHA1 | Date | |
|---|---|---|---|
| 626ab99888 | |||
| 9c8189bc33 | |||
| 956aa8b3b0 | |||
| 7d5b2a7aac | |||
| 5f089fb01f | |||
| af3cc81180 | |||
| e0189381f1 | |||
| 3a56e2426e | |||
| 0c3edd64f3 | |||
| 2d95fcb1a1 | |||
| 7c952c134a | |||
| 7721dfb669 | |||
| c1ed05a335 |
@@ -1,8 +1,8 @@
|
||||
using AS1024.GeoFeed.Core.Interfaces;
|
||||
using AS1024.GeoFeed.Core.Tools;
|
||||
using AS1024.GeoFeed.Models;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using AS1024.GeoFeed.Core.Tools;
|
||||
|
||||
namespace AS1024.GeoFeed.Core.GeoFeedSqliteLocalCache
|
||||
{
|
||||
@@ -58,7 +58,7 @@ namespace AS1024.GeoFeed.Core.GeoFeedSqliteLocalCache
|
||||
List<IPGeoFeed> cachedData = [];
|
||||
results.ForEach(cachedData.Add);
|
||||
|
||||
return cachedData.ToGeoFeedCsv();
|
||||
return cachedData.ToGeoFeedCsv(true, true);
|
||||
}
|
||||
|
||||
private async Task DBContextMigrate()
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
<OutputType>Library</OutputType>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\AS1024.GeoFeed.Core\AS1024.GeoFeed.Core.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
70
AS1024.GeoFeed.Core.WebLogic/GeoFeedRetrurn.cs
Normal file
70
AS1024.GeoFeed.Core.WebLogic/GeoFeedRetrurn.cs
Normal file
@@ -0,0 +1,70 @@
|
||||
using AS1024.GeoFeed.Core.Interfaces;
|
||||
using AS1024.GeoFeed.Core.Tools;
|
||||
using AS1024.GeoFeed.Models;
|
||||
using Microsoft.Extensions.Caching.Memory;
|
||||
using System.Text;
|
||||
|
||||
namespace AS1024.GeoFeed.Core.WebLogic
|
||||
{
|
||||
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.GetGeoFeedDataAsync();
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
12
AS1024.GeoFeed.Core.WebLogic/Properties/launchSettings.json
Normal file
12
AS1024.GeoFeed.Core.WebLogic/Properties/launchSettings.json
Normal file
@@ -0,0 +1,12 @@
|
||||
{
|
||||
"profiles": {
|
||||
"AS1024.GeoFeed.Core.WebLogic": {
|
||||
"commandName": "Project",
|
||||
"launchBrowser": true,
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
},
|
||||
"applicationUrl": "https://localhost:54455;http://localhost:54456"
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,7 @@
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
<OutputType>Library</OutputType>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
||||
@@ -39,10 +39,10 @@ namespace AS1024.GeoFeed.Core.CacheService
|
||||
{
|
||||
var scope = host.Services.CreateScope();
|
||||
|
||||
var persistentCacheProvider =
|
||||
var persistentCacheProvider =
|
||||
scope.ServiceProvider.GetRequiredService<IGeoFeedPersistentCacheProvider>();
|
||||
|
||||
var results = await feedProvider.GetGeoFeedData();
|
||||
var results = await feedProvider.GetGeoFeedDataAsync();
|
||||
await persistentCacheProvider.CacheGeoFeed(results);
|
||||
}
|
||||
catch (Exception)
|
||||
|
||||
@@ -1,13 +1,7 @@
|
||||
using AS1024.GeoFeed.Core.Interfaces;
|
||||
using AS1024.GeoFeed.Models;
|
||||
using AS1024.GeoFeed.Core.Tools;
|
||||
using AS1024.GeoFeed.Models;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace AS1024.GeoFeed.Core.CacheService
|
||||
|
||||
@@ -41,7 +41,7 @@ namespace AS1024.GeoFeed.Core.GeoFeedPreloader
|
||||
private async Task StartPreLoad()
|
||||
{
|
||||
logger.LogInformation("Preloading GeoFeed data in memory...");
|
||||
List<IPGeoFeed> feed = await provider.GetGeoFeedData();
|
||||
List<IPGeoFeed> feed = await provider.GetGeoFeedDataAsync();
|
||||
MemoryCacheEntryOptions cacheEntryOptions = new MemoryCacheEntryOptions().SetAbsoluteExpiration(TimeSpan.FromMinutes(45));
|
||||
memoryCache.Set(GeoFeedCacheKey, feed, cacheEntryOptions);
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@ namespace AS1024.GeoFeed.Core.GeoFeedProviders
|
||||
{
|
||||
public class NetBoxGeoFeedProvider : NetBoxGeoFeedProviderBase, IGeoFeedProvider
|
||||
{
|
||||
public NetBoxGeoFeedProvider(IConfiguration configuration, ILogger<NetBoxGeoFeedProvider> logger, IHttpClientFactory httpClientFactory)
|
||||
public NetBoxGeoFeedProvider(IConfiguration configuration, ILogger<NetBoxGeoFeedProvider> logger, IHttpClientFactory httpClientFactory)
|
||||
: base(configuration, logger, httpClientFactory)
|
||||
{
|
||||
}
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
using AS1024.GeoFeed.Core.Interfaces;
|
||||
using AS1024.GeoFeed.Models;
|
||||
using System.Text.Json;
|
||||
using System.Net.Http.Headers;
|
||||
using System.Net.Sockets;
|
||||
using System.Web;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using System.Net.Http.Headers;
|
||||
using System.Net.Sockets;
|
||||
using System.Text.Json;
|
||||
using System.Web;
|
||||
|
||||
namespace AS1024.GeoFeed.Core.GeoFeedProviders
|
||||
{
|
||||
@@ -31,7 +31,7 @@ namespace AS1024.GeoFeed.Core.GeoFeedProviders
|
||||
this.httpClientFactory = httpClientFactory;
|
||||
}
|
||||
|
||||
public async Task<List<IPGeoFeed>> GetGeoFeedData()
|
||||
public async Task<List<IPGeoFeed>> GetGeoFeedDataAsync()
|
||||
{
|
||||
List<IPGeoFeed> geoFeed = [];
|
||||
using HttpClient client = httpClientFactory.CreateClient();
|
||||
@@ -52,7 +52,7 @@ namespace AS1024.GeoFeed.Core.GeoFeedProviders
|
||||
{
|
||||
break;
|
||||
}
|
||||
string stringResult = await result.Content.ReadAsStringAsync();
|
||||
var stringResult = await result.Content.ReadAsStreamAsync();
|
||||
jsonData = DeserializeJsonData(stringResult);
|
||||
|
||||
if (jsonData?.Results == null || jsonData.Results.Count == 0)
|
||||
@@ -93,7 +93,7 @@ namespace AS1024.GeoFeed.Core.GeoFeedProviders
|
||||
return geoFeed;
|
||||
}
|
||||
|
||||
protected virtual NetboxData? DeserializeJsonData(string stringResult)
|
||||
protected virtual NetboxData? DeserializeJsonData(Stream stringResult)
|
||||
{
|
||||
return JsonSerializer.Deserialize<NetboxData>(stringResult, new JsonSerializerOptions
|
||||
{
|
||||
|
||||
@@ -1,9 +1,4 @@
|
||||
using AS1024.GeoFeed.Models;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace AS1024.GeoFeed.Core.Interfaces
|
||||
{
|
||||
|
||||
@@ -5,6 +5,6 @@ namespace AS1024.GeoFeed.Core.Interfaces
|
||||
public interface IGeoFeedProvider
|
||||
{
|
||||
public string GeoFeedProviderName { get; }
|
||||
public Task<List<IPGeoFeed>> GetGeoFeedData();
|
||||
public Task<List<IPGeoFeed>> GetGeoFeedDataAsync();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,10 +5,23 @@ namespace AS1024.GeoFeed.Core.Tools
|
||||
{
|
||||
public static class GeoFeedTools
|
||||
{
|
||||
public static string ToGeoFeedCsv(this List<IPGeoFeed> geoFeeds)
|
||||
/// <summary>
|
||||
/// Returns a CSV string for a given GeoFeed retreived from various sources
|
||||
/// </summary>
|
||||
/// <param name="geoFeeds">GeoFeed returned from the source of truth</param>
|
||||
/// <param name="timeStamp">If a timestamp should be appended at the header</param>
|
||||
/// <param name="isCached">If the result is cached</param>
|
||||
/// <returns></returns>
|
||||
public static string ToGeoFeedCsv(this List<IPGeoFeed> geoFeeds, bool timeStamp = false, bool isCached = false)
|
||||
{
|
||||
StringBuilder csvContent = new();
|
||||
|
||||
if (timeStamp)
|
||||
csvContent.AppendLine($"# GeoFeed generated on {DateTime.UtcNow:R}");
|
||||
|
||||
if (isCached)
|
||||
csvContent.AppendLine($"# Geofeed data is returned from local in memory cache");
|
||||
|
||||
foreach (IPGeoFeed feed in geoFeeds)
|
||||
{
|
||||
csvContent.AppendLine($"{feed.Prefix},{feed.GeolocCountry},{feed.GeolocRegion},{feed.GeolocCity},{feed.GeolocPostalCode}");
|
||||
|
||||
@@ -14,6 +14,7 @@
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\AS1024.GeoFeed.Core.WebLogic\AS1024.GeoFeed.Core.WebLogic.csproj" />
|
||||
<ProjectReference Include="..\AS1024.GeoFeed.Core\AS1024.GeoFeed.Core.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
|
||||
@@ -14,7 +14,7 @@ namespace AS1024.GeoFeed.MinimalAPI
|
||||
{
|
||||
}
|
||||
|
||||
protected override NetboxData? DeserializeJsonData(string stringResult)
|
||||
protected override NetboxData? DeserializeJsonData(Stream stringResult)
|
||||
{
|
||||
return JsonSerializer.Deserialize(stringResult, AppJsonSerializerContext.Default.NetboxData);
|
||||
}
|
||||
|
||||
@@ -1,15 +1,7 @@
|
||||
using AS1024.GeoFeed.Core.CacheService;
|
||||
using AS1024.GeoFeed.Core.GeoFeedProviders;
|
||||
using AS1024.GeoFeed.Core.GeoFeedPreloader;
|
||||
using AS1024.GeoFeed.Core.Interfaces;
|
||||
using AS1024.GeoFeed.Core.Tools;
|
||||
using AS1024.GeoFeed.Models;
|
||||
using Microsoft.AspNetCore.Http.HttpResults;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Extensions.Caching.Memory;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using System;
|
||||
using System.Text;
|
||||
using AS1024.GeoFeed.Core.WebLogic;
|
||||
|
||||
namespace AS1024.GeoFeed.MinimalAPI
|
||||
{
|
||||
@@ -21,73 +13,29 @@ namespace AS1024.GeoFeed.MinimalAPI
|
||||
var builder = WebApplication.CreateSlimBuilder(args);
|
||||
builder.Services.AddTransient<IGeoFeedProvider, NetboxAoTGeoFeedProvider>();
|
||||
builder.Services.AddHostedService<GeoFeedCacheService>();
|
||||
builder.Services.AddHostedService<PreLoadGeoFeed>();
|
||||
builder.Services.AddTransient<IGeoFeedPersistentCacheProvider, GeoFeedLocalFileCache>();
|
||||
builder.Services.AddScoped<GeoFeedReturn>();
|
||||
builder.Services.AddMemoryCache();
|
||||
builder.Services.AddLogging();
|
||||
builder.Services.AddHttpClient();
|
||||
builder.Services.ConfigureHttpJsonOptions(options => {
|
||||
builder.Services.ConfigureHttpJsonOptions(options =>
|
||||
{
|
||||
options.SerializerOptions.TypeInfoResolverChain.Insert(0, AppJsonSerializerContext.Default);
|
||||
});
|
||||
var app = builder.Build();
|
||||
|
||||
app.Map("/geofeed.csv", async (IGeoFeedProvider provider,
|
||||
ILogger<Program> logger,
|
||||
IGeoFeedPersistentCacheProvider cacheProvider,
|
||||
IMemoryCache memoryCache,
|
||||
IWebHostEnvironment environment) => {
|
||||
return await GeoFeedDataRunner(provider, logger, cacheProvider, memoryCache, environment);
|
||||
});
|
||||
app.Map("/geofeed.csv", async (GeoFeedReturn feedReturn) =>
|
||||
{
|
||||
return await feedReturn.GetGeoFeed();
|
||||
});
|
||||
|
||||
app.Map("/geofeed", async (IGeoFeedProvider provider,
|
||||
ILogger<Program> logger,
|
||||
IGeoFeedPersistentCacheProvider cacheProvider,
|
||||
IMemoryCache memoryCache,
|
||||
IWebHostEnvironment environment) => {
|
||||
return await GeoFeedDataRunner(provider, logger, cacheProvider, memoryCache, environment);
|
||||
});
|
||||
app.Map("/geofeed", async (GeoFeedReturn feedReturn) =>
|
||||
{
|
||||
return await feedReturn.GetGeoFeed();
|
||||
});
|
||||
|
||||
app.Run();
|
||||
}
|
||||
|
||||
protected static async Task<IResult> 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 Results.File(Encoding.UTF8.GetBytes(feed.ToGeoFeedCsv()),
|
||||
"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,11 +1,10 @@
|
||||
using System.Text.Json.Serialization;
|
||||
namespace AS1024.GeoFeed.Models
|
||||
namespace AS1024.GeoFeed.Models
|
||||
{
|
||||
public class NetboxData
|
||||
{
|
||||
public List<Result>? Results { get; set; }
|
||||
public string? Next { get; set; }
|
||||
public string? Previous { get; set; }
|
||||
public string? Previous { get; set; }
|
||||
}
|
||||
|
||||
public class Result
|
||||
@@ -41,7 +40,8 @@ namespace AS1024.GeoFeed.Models
|
||||
/// <summary>
|
||||
/// This class represents the IP GeoFeed Entry
|
||||
/// </summary>
|
||||
public class IPGeoFeed : CustomFields {
|
||||
public class IPGeoFeed : CustomFields
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents the IP Prefix for the associated GeoFeed entry
|
||||
/// </summary>
|
||||
|
||||
@@ -14,7 +14,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AS1024.GeoFeed.Core", "AS10
|
||||
EndProject
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AS1024.GeoFeed.MinimalAPI", "AS1024.GeoFeed.MinimalAPI\AS1024.GeoFeed.MinimalAPI.csproj", "{36F2958C-8D0E-463B-9BF3-D6E55E6FC0B8}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AS1024.GeoFeed.Core.SqliteGeoFeedCache", "AS1024.GeoFeed.Core.SqliteGeoFeedCache\AS1024.GeoFeed.Core.SqliteGeoFeedCache.csproj", "{3459BB31-FA7A-44D1-872D-C5338ACFBF80}"
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AS1024.GeoFeed.Core.SqliteGeoFeedCache", "AS1024.GeoFeed.Core.SqliteGeoFeedCache\AS1024.GeoFeed.Core.SqliteGeoFeedCache.csproj", "{3459BB31-FA7A-44D1-872D-C5338ACFBF80}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AS1024.GeoFeed.Core.WebLogic", "AS1024.GeoFeed.Core.WebLogic\AS1024.GeoFeed.Core.WebLogic.csproj", "{58BDCE89-FCC0-478F-BBDE-B89833712AAB}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
@@ -42,6 +44,10 @@ Global
|
||||
{3459BB31-FA7A-44D1-872D-C5338ACFBF80}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{3459BB31-FA7A-44D1-872D-C5338ACFBF80}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{3459BB31-FA7A-44D1-872D-C5338ACFBF80}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{58BDCE89-FCC0-478F-BBDE-B89833712AAB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{58BDCE89-FCC0-478F-BBDE-B89833712AAB}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{58BDCE89-FCC0-478F-BBDE-B89833712AAB}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{58BDCE89-FCC0-478F-BBDE-B89833712AAB}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
|
||||
@@ -23,6 +23,7 @@
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\AS1024.GeoFeed.Core.SqliteGeoFeedCache\AS1024.GeoFeed.Core.SqliteGeoFeedCache.csproj" />
|
||||
<ProjectReference Include="..\AS1024.GeoFeed.Core.WebLogic\AS1024.GeoFeed.Core.WebLogic.csproj" />
|
||||
<ProjectReference Include="..\AS1024.GeoFeed.Core\AS1024.GeoFeed.Core.csproj" />
|
||||
<ProjectReference Include="..\AS1024.GeoFeed.Models\AS1024.GeoFeed.Models.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
@@ -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.WebLogic;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace AS1024.GeoFeed.Controllers
|
||||
{
|
||||
@@ -13,75 +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()
|
||||
{
|
||||
try
|
||||
{
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
return ReturnFile(feed);
|
||||
} 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)
|
||||
{
|
||||
string csvContent = feed.ToGeoFeedCsv(); // 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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
using AS1024.GeoFeed.Core.Interfaces;
|
||||
using AS1024.GeoFeed.Core.CacheService;
|
||||
using AS1024.GeoFeed.Core.GeoFeedPreloader;
|
||||
using AS1024.GeoFeed.Core.GeoFeedProviders;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using AS1024.GeoFeed.Core.GeoFeedSqliteLocalCache;
|
||||
using AS1024.GeoFeed.Core.CacheService;
|
||||
using AS1024.GeoFeed.Core.Interfaces;
|
||||
using AS1024.GeoFeed.Core.WebLogic;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
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 =>
|
||||
{
|
||||
|
||||
14
docker/Caddyfile
Normal file
14
docker/Caddyfile
Normal file
@@ -0,0 +1,14 @@
|
||||
{$GEOFEEDDOMAIN}:443 {
|
||||
log {
|
||||
level INFO
|
||||
output file {$LOG_FILE} {
|
||||
roll_size 10MB
|
||||
roll_keep 10
|
||||
}
|
||||
}
|
||||
|
||||
# Use the ACME HTTP-01 challenge to get a cert for the configured domain.
|
||||
tls {$EMAIL}
|
||||
|
||||
reverse_proxy geofeed:8080
|
||||
}
|
||||
28
docker/docker-compose.yml
Normal file
28
docker/docker-compose.yml
Normal file
@@ -0,0 +1,28 @@
|
||||
version: '3.7'
|
||||
services:
|
||||
geofeed:
|
||||
# use the image tag aot-minimal for the AOT version for fastest startup performance
|
||||
image: git.startmywifi.com/as1024/geofeed:latest
|
||||
restart: always
|
||||
volumes:
|
||||
- './data:/data'
|
||||
environment:
|
||||
- ASPNETCORE_URLS=http://+:8080
|
||||
- ConnectionString__LocalFeedCache=Data Source=/data/geofeed-cache.db
|
||||
- APIKey=APIKeyHere
|
||||
- NetBoxHost=netboxhosthere
|
||||
caddy:
|
||||
image: caddy:2
|
||||
restart: always
|
||||
ports:
|
||||
- 80:80
|
||||
- 443:443
|
||||
- 443:443/udp
|
||||
volumes:
|
||||
- ./Caddyfile:/etc/caddy/Caddyfile:ro
|
||||
- ./caddy-config:/config
|
||||
- ./caddy-data:/data
|
||||
environment:
|
||||
GEOFEEDDOMAIN: "https://geofeed.exampleas.net"
|
||||
EMAIL: "noc@example.com" # The email address to use for ACME registration.
|
||||
LOG_FILE: "/data/access.log"
|
||||
Reference in New Issue
Block a user