Compare commits
34 Commits
a44b4f9421
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
| 5cad97c9ac | |||
| cc61b02959 | |||
| 0dfe68656d | |||
| 6544dc5a3d | |||
| 08e518c340 | |||
| e82d198ee7 | |||
| 4c6a9e6dec | |||
| feb1326cee | |||
| 626ab99888 | |||
| 9c8189bc33 | |||
| 956aa8b3b0 | |||
| 7d5b2a7aac | |||
| 5f089fb01f | |||
| af3cc81180 | |||
| e0189381f1 | |||
| 3a56e2426e | |||
| 0c3edd64f3 | |||
| 2d95fcb1a1 | |||
| 7c952c134a | |||
| 7721dfb669 | |||
| c1ed05a335 | |||
| 690a117ffd | |||
| 9dfbded5b8 | |||
| 7b7d422890 | |||
| 2d6083f530 | |||
| 0e56d778bc | |||
| 8523ddd60f | |||
| 83826cc930 | |||
| cc1717e4a3 | |||
| 48932ec2a5 | |||
| 42aacf497c | |||
| e8152186c1 | |||
| 43b34143a3 | |||
| 8ed021754c |
@@ -1,23 +0,0 @@
|
|||||||
<Project Sdk="Microsoft.NET.Sdk">
|
|
||||||
|
|
||||||
<PropertyGroup>
|
|
||||||
<TargetFramework>net8.0</TargetFramework>
|
|
||||||
<ImplicitUsings>enable</ImplicitUsings>
|
|
||||||
<Nullable>enable</Nullable>
|
|
||||||
</PropertyGroup>
|
|
||||||
|
|
||||||
<ItemGroup>
|
|
||||||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.0" />
|
|
||||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="8.0.0" />
|
|
||||||
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="8.0.0" />
|
|
||||||
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="8.0.0" />
|
|
||||||
<PackageReference Include="Microsoft.Extensions.Http" Version="8.0.0" />
|
|
||||||
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="8.0.0" />
|
|
||||||
</ItemGroup>
|
|
||||||
|
|
||||||
<ItemGroup>
|
|
||||||
<ProjectReference Include="..\AS1024.GeoFeed.Core\AS1024.GeoFeed.Core.csproj" />
|
|
||||||
<ProjectReference Include="..\AS1024.GeoFeed.Models\AS1024.GeoFeed.Models.csproj" />
|
|
||||||
</ItemGroup>
|
|
||||||
|
|
||||||
</Project>
|
|
||||||
@@ -1,15 +0,0 @@
|
|||||||
using Microsoft.EntityFrameworkCore;
|
|
||||||
|
|
||||||
namespace AS1024.GeoFeed.Core.GeoFeedSqliteLocalCache
|
|
||||||
{
|
|
||||||
public class GeoFeedCacheDbContext : DbContext
|
|
||||||
{
|
|
||||||
public GeoFeedCacheDbContext(DbContextOptions options)
|
|
||||||
: base(options)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
public virtual DbSet<GeoFeedCacheEntry> GeoFeedCacheEntries { get; set; }
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,12 +0,0 @@
|
|||||||
using AS1024.GeoFeed.Models;
|
|
||||||
using System.ComponentModel.DataAnnotations;
|
|
||||||
|
|
||||||
namespace AS1024.GeoFeed.Core.GeoFeedSqliteLocalCache
|
|
||||||
{
|
|
||||||
public class GeoFeedCacheEntry : IPGeoFeed
|
|
||||||
{
|
|
||||||
[Key]
|
|
||||||
public int Id { get; set; }
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,58 +0,0 @@
|
|||||||
using AS1024.GeoFeed.Core.Interfaces;
|
|
||||||
using Microsoft.EntityFrameworkCore;
|
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
|
||||||
using Microsoft.Extensions.Hosting;
|
|
||||||
using Microsoft.Extensions.Logging;
|
|
||||||
using System.Runtime.CompilerServices;
|
|
||||||
|
|
||||||
namespace AS1024.GeoFeed.Core.GeoFeedSqliteLocalCache
|
|
||||||
{
|
|
||||||
public class GeoFeedCacheService : IHostedService
|
|
||||||
{
|
|
||||||
private readonly ILogger<GeoFeedCacheService> logger;
|
|
||||||
private readonly IGeoFeedProvider feedProvider;
|
|
||||||
private readonly IHost host;
|
|
||||||
private readonly IGeoFeedPersistentCacheProvider persistentCacheProvider;
|
|
||||||
|
|
||||||
public GeoFeedCacheService(ILogger<GeoFeedCacheService> logger,
|
|
||||||
IGeoFeedProvider feedProvider,
|
|
||||||
IHost host,
|
|
||||||
IGeoFeedPersistentCacheProvider persistentCacheProvider)
|
|
||||||
{
|
|
||||||
this.logger = logger;
|
|
||||||
this.feedProvider = feedProvider;
|
|
||||||
this.host = host;
|
|
||||||
this.persistentCacheProvider = persistentCacheProvider;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Task StartAsync(CancellationToken cancellationToken)
|
|
||||||
{
|
|
||||||
_ = StartPerioidicSync(cancellationToken);
|
|
||||||
return Task.CompletedTask;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Task StopAsync(CancellationToken cancellationToken)
|
|
||||||
{
|
|
||||||
return Task.CompletedTask;
|
|
||||||
}
|
|
||||||
|
|
||||||
public async Task<bool> StartPerioidicSync(CancellationToken Token)
|
|
||||||
{
|
|
||||||
while (!Token.IsCancellationRequested)
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
var results = await feedProvider.GetGeoFeedData();
|
|
||||||
await persistentCacheProvider.CacheGeoFeed(results);
|
|
||||||
}
|
|
||||||
catch (Exception)
|
|
||||||
{
|
|
||||||
logger.LogWarning("On disk cache failed to run. Waiting on 30 minutes before retry...");
|
|
||||||
}
|
|
||||||
await Task.Delay(TimeSpan.FromMinutes(30));
|
|
||||||
}
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,17 +0,0 @@
|
|||||||
using Microsoft.EntityFrameworkCore;
|
|
||||||
using Microsoft.EntityFrameworkCore.Design;
|
|
||||||
|
|
||||||
namespace AS1024.GeoFeed.Core.GeoFeedSqliteLocalCache
|
|
||||||
|
|
||||||
{
|
|
||||||
public class GeoFeedDesignTimeMigration : IDesignTimeDbContextFactory<GeoFeedCacheDbContext>
|
|
||||||
{
|
|
||||||
public GeoFeedCacheDbContext CreateDbContext(string[] args)
|
|
||||||
{
|
|
||||||
var builder = new DbContextOptionsBuilder<GeoFeedCacheDbContext>();
|
|
||||||
builder.UseSqlite("Data Source=migratedb.db");
|
|
||||||
return new GeoFeedCacheDbContext(builder.Options);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,72 +0,0 @@
|
|||||||
using AS1024.GeoFeed.Core.Interfaces;
|
|
||||||
using AS1024.GeoFeed.Models;
|
|
||||||
using Microsoft.EntityFrameworkCore;
|
|
||||||
using Microsoft.Extensions.Hosting;
|
|
||||||
using AS1024.GeoFeed.Core.Tools;
|
|
||||||
|
|
||||||
namespace AS1024.GeoFeed.Core.GeoFeedSqliteLocalCache
|
|
||||||
{
|
|
||||||
public class GeoFeedSqliteCache : IGeoFeedPersistentCacheProvider
|
|
||||||
{
|
|
||||||
protected readonly GeoFeedCacheDbContext dbContext;
|
|
||||||
private readonly IGeoFeedProvider feedProvider;
|
|
||||||
|
|
||||||
public GeoFeedSqliteCache(GeoFeedCacheDbContext geoFeedCacheDb,
|
|
||||||
IHost host,
|
|
||||||
IGeoFeedProvider provider)
|
|
||||||
{
|
|
||||||
dbContext = geoFeedCacheDb;
|
|
||||||
feedProvider = provider;
|
|
||||||
}
|
|
||||||
|
|
||||||
public string ProviderName => "sqlite";
|
|
||||||
|
|
||||||
public async Task<bool> CacheGeoFeed(IList<IPGeoFeed> pGeoFeeds)
|
|
||||||
{
|
|
||||||
await DBContextMigrate();
|
|
||||||
List<GeoFeedCacheEntry> geoFeedCacheEntry = [];
|
|
||||||
|
|
||||||
var results = pGeoFeeds.ToList();
|
|
||||||
|
|
||||||
results.ForEach(x =>
|
|
||||||
{
|
|
||||||
geoFeedCacheEntry.Add(new()
|
|
||||||
{
|
|
||||||
Prefix = x.Prefix,
|
|
||||||
GeolocCity = x.GeolocCity,
|
|
||||||
GeolocCountry = x.GeolocCountry,
|
|
||||||
GeolocHasLocation = x.GeolocHasLocation,
|
|
||||||
GeolocPostalCode = x.GeolocPostalCode,
|
|
||||||
GeolocRegion = x.GeolocRegion
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
if (dbContext.GeoFeedCacheEntries.Any())
|
|
||||||
{
|
|
||||||
dbContext.GeoFeedCacheEntries.RemoveRange(dbContext.GeoFeedCacheEntries.ToArray());
|
|
||||||
}
|
|
||||||
await dbContext.AddRangeAsync(geoFeedCacheEntry);
|
|
||||||
await dbContext.SaveChangesAsync();
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
public string GetGeoFeed()
|
|
||||||
{
|
|
||||||
var results =
|
|
||||||
dbContext.GeoFeedCacheEntries.ToList();
|
|
||||||
List<IPGeoFeed> cachedData = [];
|
|
||||||
results.ForEach(cachedData.Add);
|
|
||||||
|
|
||||||
return cachedData.ToGeoFeedCsv();
|
|
||||||
}
|
|
||||||
|
|
||||||
private async Task DBContextMigrate()
|
|
||||||
{
|
|
||||||
if (dbContext.Database.GetPendingMigrations().Any())
|
|
||||||
{
|
|
||||||
await dbContext.Database.MigrateAsync();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,8 +1,8 @@
|
|||||||
using AS1024.GeoFeed.Core.Interfaces;
|
using AS1024.GeoFeed.Core.Interfaces;
|
||||||
|
using AS1024.GeoFeed.Core.Tools;
|
||||||
using AS1024.GeoFeed.Models;
|
using AS1024.GeoFeed.Models;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
using Microsoft.Extensions.Hosting;
|
using Microsoft.Extensions.Hosting;
|
||||||
using AS1024.GeoFeed.Core.Tools;
|
|
||||||
|
|
||||||
namespace AS1024.GeoFeed.Core.GeoFeedSqliteLocalCache
|
namespace AS1024.GeoFeed.Core.GeoFeedSqliteLocalCache
|
||||||
{
|
{
|
||||||
@@ -58,7 +58,7 @@ namespace AS1024.GeoFeed.Core.GeoFeedSqliteLocalCache
|
|||||||
List<IPGeoFeed> cachedData = [];
|
List<IPGeoFeed> cachedData = [];
|
||||||
results.ForEach(cachedData.Add);
|
results.ForEach(cachedData.Add);
|
||||||
|
|
||||||
return cachedData.ToGeoFeedCsv();
|
return cachedData.ToGeoFeedCsv(true, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task DBContextMigrate()
|
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>
|
||||||
72
AS1024.GeoFeed.Core.WebLogic/GeoFeedRetrurn.cs
Normal file
72
AS1024.GeoFeed.Core.WebLogic/GeoFeedRetrurn.cs
Normal file
@@ -0,0 +1,72 @@
|
|||||||
|
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 const string GeoFeedMimeTypeReturn = "text/csv";
|
||||||
|
private const string GeoFeedFileName = "geofeed.csv";
|
||||||
|
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)),
|
||||||
|
GeoFeedMimeTypeReturn,
|
||||||
|
GeoFeedFileName);
|
||||||
|
|
||||||
|
}
|
||||||
|
catch (HttpRequestException ex)
|
||||||
|
{
|
||||||
|
logger.LogWarning($"Temporary failure of retrieving GeoData from upstream. {ex}");
|
||||||
|
string geoFeedData = cacheProvider.GetGeoFeed();
|
||||||
|
|
||||||
|
return Results.File(Encoding.UTF8.GetBytes(geoFeedData),
|
||||||
|
GeoFeedMimeTypeReturn,
|
||||||
|
GeoFeedFileName);
|
||||||
|
}
|
||||||
|
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>
|
<TargetFramework>net8.0</TargetFramework>
|
||||||
<ImplicitUsings>enable</ImplicitUsings>
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
<Nullable>enable</Nullable>
|
<Nullable>enable</Nullable>
|
||||||
|
<OutputType>Library</OutputType>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
|||||||
@@ -42,7 +42,7 @@ namespace AS1024.GeoFeed.Core.CacheService
|
|||||||
var persistentCacheProvider =
|
var persistentCacheProvider =
|
||||||
scope.ServiceProvider.GetRequiredService<IGeoFeedPersistentCacheProvider>();
|
scope.ServiceProvider.GetRequiredService<IGeoFeedPersistentCacheProvider>();
|
||||||
|
|
||||||
var results = await feedProvider.GetGeoFeedData();
|
var results = await feedProvider.GetGeoFeedDataAsync();
|
||||||
await persistentCacheProvider.CacheGeoFeed(results);
|
await persistentCacheProvider.CacheGeoFeed(results);
|
||||||
}
|
}
|
||||||
catch (Exception)
|
catch (Exception)
|
||||||
|
|||||||
@@ -1,13 +1,7 @@
|
|||||||
using AS1024.GeoFeed.Core.Interfaces;
|
using AS1024.GeoFeed.Core.Interfaces;
|
||||||
using AS1024.GeoFeed.Models;
|
|
||||||
using AS1024.GeoFeed.Core.Tools;
|
using AS1024.GeoFeed.Core.Tools;
|
||||||
|
using AS1024.GeoFeed.Models;
|
||||||
using Microsoft.Extensions.Configuration;
|
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;
|
using Microsoft.Extensions.Logging;
|
||||||
|
|
||||||
namespace AS1024.GeoFeed.Core.CacheService
|
namespace AS1024.GeoFeed.Core.CacheService
|
||||||
|
|||||||
@@ -41,7 +41,7 @@ namespace AS1024.GeoFeed.Core.GeoFeedPreloader
|
|||||||
private async Task StartPreLoad()
|
private async Task StartPreLoad()
|
||||||
{
|
{
|
||||||
logger.LogInformation("Preloading GeoFeed data in memory...");
|
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));
|
MemoryCacheEntryOptions cacheEntryOptions = new MemoryCacheEntryOptions().SetAbsoluteExpiration(TimeSpan.FromMinutes(45));
|
||||||
memoryCache.Set(GeoFeedCacheKey, feed, cacheEntryOptions);
|
memoryCache.Set(GeoFeedCacheKey, feed, cacheEntryOptions);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,15 +1,15 @@
|
|||||||
using AS1024.GeoFeed.Core.Interfaces;
|
using AS1024.GeoFeed.Core.Interfaces;
|
||||||
using AS1024.GeoFeed.Models;
|
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.Configuration;
|
||||||
using Microsoft.Extensions.Logging;
|
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
|
namespace AS1024.GeoFeed.Core.GeoFeedProviders
|
||||||
{
|
{
|
||||||
public class NetBoxGeoFeedProviderBase : IGeoFeedProvider
|
public abstract class NetBoxGeoFeedProviderBase : IGeoFeedProvider
|
||||||
{
|
{
|
||||||
protected readonly IConfiguration configuration;
|
protected readonly IConfiguration configuration;
|
||||||
protected readonly ILogger<NetBoxGeoFeedProvider> logger;
|
protected readonly ILogger<NetBoxGeoFeedProvider> logger;
|
||||||
@@ -31,7 +31,7 @@ namespace AS1024.GeoFeed.Core.GeoFeedProviders
|
|||||||
this.httpClientFactory = httpClientFactory;
|
this.httpClientFactory = httpClientFactory;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<List<IPGeoFeed>> GetGeoFeedData()
|
public async Task<List<IPGeoFeed>> GetGeoFeedDataAsync()
|
||||||
{
|
{
|
||||||
List<IPGeoFeed> geoFeed = [];
|
List<IPGeoFeed> geoFeed = [];
|
||||||
using HttpClient client = httpClientFactory.CreateClient();
|
using HttpClient client = httpClientFactory.CreateClient();
|
||||||
@@ -52,7 +52,7 @@ namespace AS1024.GeoFeed.Core.GeoFeedProviders
|
|||||||
{
|
{
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
string stringResult = await result.Content.ReadAsStringAsync();
|
var stringResult = await result.Content.ReadAsStreamAsync();
|
||||||
jsonData = DeserializeJsonData(stringResult);
|
jsonData = DeserializeJsonData(stringResult);
|
||||||
|
|
||||||
if (jsonData?.Results == null || jsonData.Results.Count == 0)
|
if (jsonData?.Results == null || jsonData.Results.Count == 0)
|
||||||
@@ -93,7 +93,7 @@ namespace AS1024.GeoFeed.Core.GeoFeedProviders
|
|||||||
return geoFeed;
|
return geoFeed;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected virtual NetboxData? DeserializeJsonData(string stringResult)
|
protected virtual NetboxData? DeserializeJsonData(Stream stringResult)
|
||||||
{
|
{
|
||||||
return JsonSerializer.Deserialize<NetboxData>(stringResult, new JsonSerializerOptions
|
return JsonSerializer.Deserialize<NetboxData>(stringResult, new JsonSerializerOptions
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -1,9 +1,4 @@
|
|||||||
using AS1024.GeoFeed.Models;
|
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
|
namespace AS1024.GeoFeed.Core.Interfaces
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -5,6 +5,6 @@ namespace AS1024.GeoFeed.Core.Interfaces
|
|||||||
public interface IGeoFeedProvider
|
public interface IGeoFeedProvider
|
||||||
{
|
{
|
||||||
public string GeoFeedProviderName { get; }
|
public string GeoFeedProviderName { get; }
|
||||||
public Task<List<IPGeoFeed>> GetGeoFeedData();
|
public Task<List<IPGeoFeed>> GetGeoFeedDataAsync();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,13 +5,32 @@ namespace AS1024.GeoFeed.Core.Tools
|
|||||||
{
|
{
|
||||||
public static class GeoFeedTools
|
public static class GeoFeedTools
|
||||||
{
|
{
|
||||||
public static string ToGeoFeedCsv(this List<IPGeoFeed> geoFeeds)
|
/// <summary>
|
||||||
|
/// Returns a CSV string for a given GeoFeed retrieved 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>CSV formatted string of GeoFeed data.</returns>
|
||||||
|
public static string ToGeoFeedCsv(this List<IPGeoFeed> geoFeeds, bool timeStamp = false, bool isCached = false)
|
||||||
{
|
{
|
||||||
|
if (geoFeeds == null) throw new ArgumentNullException(nameof(geoFeeds));
|
||||||
|
|
||||||
StringBuilder csvContent = new();
|
StringBuilder csvContent = new();
|
||||||
|
|
||||||
|
// Append timestamp header if required
|
||||||
|
if (timeStamp)
|
||||||
|
csvContent.AppendFormat("# GeoFeed generated on {0:R}\n", DateTime.UtcNow);
|
||||||
|
|
||||||
|
// Append cache status if required
|
||||||
|
if (isCached)
|
||||||
|
csvContent.AppendLine("# Geofeed data is returned from local in memory cache");
|
||||||
|
|
||||||
|
// Iterate over each GeoFeed entry to append its details to the CSV content
|
||||||
foreach (IPGeoFeed feed in geoFeeds)
|
foreach (IPGeoFeed feed in geoFeeds)
|
||||||
{
|
{
|
||||||
csvContent.AppendLine($"{feed.Prefix},{feed.GeolocCountry},{feed.GeolocRegion},{feed.GeolocCity},{feed.GeolocPostalCode}");
|
// Using AppendFormat for a cleaner and more readable approach to constructing CSV lines
|
||||||
|
csvContent.AppendFormat("{0},{1},{2},{3},{4}\n", feed.Prefix, feed.GeolocCountry, feed.GeolocRegion, feed.GeolocCity, feed.GeolocPostalCode);
|
||||||
}
|
}
|
||||||
|
|
||||||
return csvContent.ToString();
|
return csvContent.ToString();
|
||||||
|
|||||||
@@ -14,6 +14,7 @@
|
|||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<ProjectReference Include="..\AS1024.GeoFeed.Core.WebLogic\AS1024.GeoFeed.Core.WebLogic.csproj" />
|
||||||
<ProjectReference Include="..\AS1024.GeoFeed.Core\AS1024.GeoFeed.Core.csproj" />
|
<ProjectReference Include="..\AS1024.GeoFeed.Core\AS1024.GeoFeed.Core.csproj" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
|||||||
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
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,11 +1,11 @@
|
|||||||
#See https://aka.ms/customizecontainer to learn how to customize your debug container and how Visual Studio uses this Dockerfile to build your images for faster debugging.
|
#See https://aka.ms/customizecontainer to learn how to customize your debug container and how Visual Studio uses this Dockerfile to build your images for faster debugging.
|
||||||
|
|
||||||
FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS base
|
FROM mcr.microsoft.com/dotnet/runtime:8.0-jammy-chiseled AS base
|
||||||
USER app
|
USER app
|
||||||
WORKDIR /app
|
WORKDIR /app
|
||||||
EXPOSE 8080
|
EXPOSE 8080
|
||||||
|
|
||||||
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
|
FROM mcr.microsoft.com/dotnet/sdk:8.0-jammy AS build
|
||||||
# Install clang/zlib1g-dev dependencies for publishing to native
|
# Install clang/zlib1g-dev dependencies for publishing to native
|
||||||
RUN apt-get update \
|
RUN apt-get update \
|
||||||
&& apt-get install -y --no-install-recommends \
|
&& apt-get install -y --no-install-recommends \
|
||||||
@@ -20,9 +20,9 @@ RUN dotnet build "./AS1024.GeoFeed.MinimalAPI.csproj" -c $BUILD_CONFIGURATION -o
|
|||||||
|
|
||||||
FROM build AS publish
|
FROM build AS publish
|
||||||
ARG BUILD_CONFIGURATION=Release
|
ARG BUILD_CONFIGURATION=Release
|
||||||
RUN dotnet publish "./AS1024.GeoFeed.MinimalAPI.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=true
|
RUN dotnet publish "./AS1024.GeoFeed.MinimalAPI.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=true /p:DebugType=None /p:DebugSymbols=false
|
||||||
|
|
||||||
FROM mcr.microsoft.com/dotnet/runtime-deps:8.0 AS final
|
FROM base AS final
|
||||||
WORKDIR /app
|
WORKDIR /app
|
||||||
EXPOSE 8080
|
EXPOSE 8080
|
||||||
COPY --from=publish /app/publish .
|
COPY --from=publish /app/publish .
|
||||||
|
|||||||
55
AS1024.GeoFeed.MinimalAPI/Dockerfile.alpine-selfcontained
Normal file
55
AS1024.GeoFeed.MinimalAPI/Dockerfile.alpine-selfcontained
Normal file
@@ -0,0 +1,55 @@
|
|||||||
|
#See https://aka.ms/customizecontainer to learn how to customize your debug container and how Visual Studio uses this Dockerfile to build your images for faster debugging.
|
||||||
|
|
||||||
|
FROM alpine:latest AS base
|
||||||
|
ENV \
|
||||||
|
# UID of the non-root user 'app'
|
||||||
|
APP_UID=1654 \
|
||||||
|
# Configure web servers to bind to port 8080 when present
|
||||||
|
ASPNETCORE_HTTP_PORTS=8080 \
|
||||||
|
# Enable detection of running in a container
|
||||||
|
DOTNET_RUNNING_IN_CONTAINER=true \
|
||||||
|
# Set the invariant mode since ICU package isn't included (see https://github.com/dotnet/announcements/issues/20)
|
||||||
|
DOTNET_SYSTEM_GLOBALIZATION_INVARIANT=true
|
||||||
|
|
||||||
|
#RUN apk add --upgrade --no-cache \
|
||||||
|
# ca-certificates-bundle \
|
||||||
|
# \
|
||||||
|
# # .NET dependencies
|
||||||
|
# libgcc \
|
||||||
|
# libssl3 \
|
||||||
|
# libstdc++ \
|
||||||
|
# zlib
|
||||||
|
|
||||||
|
# Create a non-root user and group
|
||||||
|
RUN addgroup \
|
||||||
|
--gid=$APP_UID \
|
||||||
|
app \
|
||||||
|
&& adduser \
|
||||||
|
--uid=$APP_UID \
|
||||||
|
--ingroup=app \
|
||||||
|
--disabled-password \
|
||||||
|
app
|
||||||
|
WORKDIR /app
|
||||||
|
USER app
|
||||||
|
EXPOSE 8080
|
||||||
|
|
||||||
|
FROM mcr.microsoft.com/dotnet/sdk:8.0-alpine AS build
|
||||||
|
# Install clang/zlib1g-dev dependencies for publishing to native
|
||||||
|
RUN apk add clang zlib-dev musl-dev libc6-compat
|
||||||
|
ARG BUILD_CONFIGURATION=Release
|
||||||
|
WORKDIR /src
|
||||||
|
COPY ["AS1024.GeoFeed.MinimalAPI/AS1024.GeoFeed.MinimalAPI.csproj", "AS1024.GeoFeed.MinimalAPI/"]
|
||||||
|
RUN dotnet restore "./AS1024.GeoFeed.MinimalAPI/./AS1024.GeoFeed.MinimalAPI.csproj"
|
||||||
|
COPY . .
|
||||||
|
WORKDIR "/src/AS1024.GeoFeed.MinimalAPI"
|
||||||
|
RUN dotnet build "./AS1024.GeoFeed.MinimalAPI.csproj" -c $BUILD_CONFIGURATION -o /app/build
|
||||||
|
|
||||||
|
FROM build AS publish
|
||||||
|
ARG BUILD_CONFIGURATION=Release
|
||||||
|
RUN dotnet publish "./AS1024.GeoFeed.MinimalAPI.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=true /p:DebugType=None /p:DebugSymbols=false
|
||||||
|
|
||||||
|
FROM base AS final
|
||||||
|
WORKDIR /app
|
||||||
|
EXPOSE 8080
|
||||||
|
COPY --from=publish /app/publish .
|
||||||
|
ENTRYPOINT ["./AS1024.GeoFeed.MinimalAPI"]
|
||||||
24
AS1024.GeoFeed.MinimalAPI/Dockerfile.bare-image
Normal file
24
AS1024.GeoFeed.MinimalAPI/Dockerfile.bare-image
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
#See https://aka.ms/customizecontainer to learn how to customize your debug container and how Visual Studio uses this Dockerfile to build your images for faster debugging.
|
||||||
|
|
||||||
|
FROM mcr.microsoft.com/dotnet/sdk:8.0-alpine AS build
|
||||||
|
# Install clang/zlib1g-dev dependencies for publishing to native
|
||||||
|
RUN apk add clang zlib-static zlib-dev musl-dev libc6-compat cmake autoconf make openssl-dev openssl-libs-static icu-static icu-dev
|
||||||
|
ARG BUILD_CONFIGURATION=Release
|
||||||
|
WORKDIR /src
|
||||||
|
COPY ["AS1024.GeoFeed.MinimalAPI/AS1024.GeoFeed.MinimalAPI.csproj", "AS1024.GeoFeed.MinimalAPI/"]
|
||||||
|
RUN dotnet restore "./AS1024.GeoFeed.MinimalAPI/./AS1024.GeoFeed.MinimalAPI.csproj"
|
||||||
|
COPY . .
|
||||||
|
WORKDIR "/src/AS1024.GeoFeed.MinimalAPI"
|
||||||
|
RUN dotnet build "./AS1024.GeoFeed.MinimalAPI.csproj" -c $BUILD_CONFIGURATION -o /app/build
|
||||||
|
|
||||||
|
FROM build AS publish
|
||||||
|
ARG BUILD_CONFIGURATION=Release
|
||||||
|
RUN dotnet publish "./AS1024.GeoFeed.MinimalAPI.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:StaticOpenSslLinking=true /p:StaticExecutable=true /p:StaticallyLinked=true /p:StripSymbols=true /p:DebugType=None /p:DebugSymbols=false
|
||||||
|
|
||||||
|
FROM scratch AS final
|
||||||
|
WORKDIR /tmp
|
||||||
|
WORKDIR /app
|
||||||
|
EXPOSE 8080
|
||||||
|
COPY --from=publish /app/publish .
|
||||||
|
COPY --from=build /etc/ssl/certs/* /etc/ssl/certs/
|
||||||
|
ENTRYPOINT ["./AS1024.GeoFeed.MinimalAPI"]
|
||||||
@@ -1,21 +1,12 @@
|
|||||||
using AS1024.GeoFeed.Core.GeoFeedProviders;
|
using AS1024.GeoFeed.Core.GeoFeedProviders;
|
||||||
using AS1024.GeoFeed.Core.Interfaces;
|
using AS1024.GeoFeed.Core.Interfaces;
|
||||||
using AS1024.GeoFeed.Models;
|
using AS1024.GeoFeed.Models;
|
||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Diagnostics.CodeAnalysis;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Text;
|
|
||||||
using System.Text.Json;
|
using System.Text.Json;
|
||||||
using System.Text.Json.Serialization.Metadata;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
|
|
||||||
namespace AS1024.GeoFeed.MinimalAPI
|
namespace AS1024.GeoFeed.MinimalAPI
|
||||||
{
|
{
|
||||||
internal class NetboxAoTGeoFeedProvider : NetBoxGeoFeedProvider, IGeoFeedProvider
|
internal class NetboxAoTGeoFeedProvider : NetBoxGeoFeedProvider, IGeoFeedProvider
|
||||||
{
|
{
|
||||||
private readonly JsonSerializerOptions appJsonSerializer;
|
|
||||||
|
|
||||||
public NetboxAoTGeoFeedProvider(IConfiguration configuration,
|
public NetboxAoTGeoFeedProvider(IConfiguration configuration,
|
||||||
ILogger<NetBoxGeoFeedProvider> logger,
|
ILogger<NetBoxGeoFeedProvider> logger,
|
||||||
IHttpClientFactory httpClientFactory)
|
IHttpClientFactory httpClientFactory)
|
||||||
@@ -23,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);
|
return JsonSerializer.Deserialize(stringResult, AppJsonSerializerContext.Default.NetboxData);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,51 +1,41 @@
|
|||||||
using AS1024.GeoFeed.Core.GeoFeedProviders;
|
using AS1024.GeoFeed.Core.CacheService;
|
||||||
|
using AS1024.GeoFeed.Core.GeoFeedPreloader;
|
||||||
using AS1024.GeoFeed.Core.Interfaces;
|
using AS1024.GeoFeed.Core.Interfaces;
|
||||||
using AS1024.GeoFeed.Core.Tools;
|
using AS1024.GeoFeed.Core.WebLogic;
|
||||||
using AS1024.GeoFeed.Models;
|
|
||||||
using Microsoft.AspNetCore.Http.HttpResults;
|
|
||||||
using Microsoft.AspNetCore.Mvc;
|
|
||||||
using System.Text.Json.Serialization;
|
|
||||||
|
|
||||||
namespace AS1024.GeoFeed.MinimalAPI
|
namespace AS1024.GeoFeed.MinimalAPI
|
||||||
{
|
{
|
||||||
public class Program
|
public class Program
|
||||||
{
|
{
|
||||||
|
|
||||||
public static void Main(string[] args)
|
public static void Main(string[] args)
|
||||||
{
|
{
|
||||||
var builder = WebApplication.CreateSlimBuilder(args);
|
var builder = WebApplication.CreateSlimBuilder(args);
|
||||||
builder.Services.AddTransient<IGeoFeedProvider, NetboxAoTGeoFeedProvider>();
|
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.AddMemoryCache();
|
||||||
builder.Services.AddLogging();
|
builder.Services.AddLogging();
|
||||||
builder.Services.AddHttpClient();
|
builder.Services.AddHttpClient();
|
||||||
builder.Services.ConfigureHttpJsonOptions(options => {
|
builder.Services.ConfigureHttpJsonOptions(options =>
|
||||||
|
{
|
||||||
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, ILogger<Program> logger) => {
|
|
||||||
try
|
|
||||||
{
|
|
||||||
var results =
|
|
||||||
await provider.GetGeoFeedData();
|
|
||||||
return results.ToGeoFeedCsv();
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
logger.LogError($"Error: {ex}");
|
|
||||||
}
|
|
||||||
|
|
||||||
return "";
|
app.Map("/geofeed.csv", async (GeoFeedReturn feedReturn) =>
|
||||||
|
{
|
||||||
|
return await feedReturn.GetGeoFeed();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
app.Map("/geofeed", async (GeoFeedReturn feedReturn) =>
|
||||||
|
{
|
||||||
|
return await feedReturn.GetGeoFeed();
|
||||||
|
});
|
||||||
|
|
||||||
app.Run();
|
app.Run();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
[JsonSerializable(typeof(NetboxData))]
|
|
||||||
[JsonSerializable(typeof(Result))]
|
|
||||||
[JsonSerializable(typeof(CustomFields))]
|
|
||||||
[JsonSourceGenerationOptions(PropertyNamingPolicy = JsonKnownNamingPolicy.SnakeCaseLower)]
|
|
||||||
internal partial class AppJsonSerializerContext : JsonSerializerContext
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,11 +1,10 @@
|
|||||||
using System.Text.Json.Serialization;
|
namespace AS1024.GeoFeed.Models
|
||||||
namespace AS1024.GeoFeed.Models
|
|
||||||
{
|
{
|
||||||
public class NetboxData
|
public class NetboxData
|
||||||
{
|
{
|
||||||
public List<Result>? Results { get; set; }
|
public List<Result>? Results { get; set; }
|
||||||
public string? Next { get; set; }
|
public string? Next { get; set; }
|
||||||
public string? Previous { get; set; }
|
public string? Previous { get; set; }
|
||||||
}
|
}
|
||||||
|
|
||||||
public class Result
|
public class Result
|
||||||
@@ -41,7 +40,8 @@ namespace AS1024.GeoFeed.Models
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// This class represents the IP GeoFeed Entry
|
/// This class represents the IP GeoFeed Entry
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class IPGeoFeed : CustomFields {
|
public class IPGeoFeed : CustomFields
|
||||||
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Represents the IP Prefix for the associated GeoFeed entry
|
/// Represents the IP Prefix for the associated GeoFeed entry
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
@@ -14,7 +14,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AS1024.GeoFeed.Core", "AS10
|
|||||||
EndProject
|
EndProject
|
||||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AS1024.GeoFeed.MinimalAPI", "AS1024.GeoFeed.MinimalAPI\AS1024.GeoFeed.MinimalAPI.csproj", "{36F2958C-8D0E-463B-9BF3-D6E55E6FC0B8}"
|
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AS1024.GeoFeed.MinimalAPI", "AS1024.GeoFeed.MinimalAPI\AS1024.GeoFeed.MinimalAPI.csproj", "{36F2958C-8D0E-463B-9BF3-D6E55E6FC0B8}"
|
||||||
EndProject
|
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
|
EndProject
|
||||||
Global
|
Global
|
||||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
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}.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.ActiveCfg = Release|Any CPU
|
||||||
{3459BB31-FA7A-44D1-872D-C5338ACFBF80}.Release|Any CPU.Build.0 = 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
|
EndGlobalSection
|
||||||
GlobalSection(SolutionProperties) = preSolution
|
GlobalSection(SolutionProperties) = preSolution
|
||||||
HideSolutionNode = FALSE
|
HideSolutionNode = FALSE
|
||||||
|
|||||||
@@ -23,6 +23,7 @@
|
|||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ProjectReference Include="..\AS1024.GeoFeed.Core.SqliteGeoFeedCache\AS1024.GeoFeed.Core.SqliteGeoFeedCache.csproj" />
|
<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.Core\AS1024.GeoFeed.Core.csproj" />
|
||||||
<ProjectReference Include="..\AS1024.GeoFeed.Models\AS1024.GeoFeed.Models.csproj" />
|
<ProjectReference Include="..\AS1024.GeoFeed.Models\AS1024.GeoFeed.Models.csproj" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|||||||
@@ -1,9 +1,5 @@
|
|||||||
using Microsoft.AspNetCore.Mvc;
|
using AS1024.GeoFeed.Core.WebLogic;
|
||||||
using AS1024.GeoFeed.Core.Interfaces;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
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,75 +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()
|
||||||
{
|
{
|
||||||
try
|
return await feedReturn.GetGeoFeed();
|
||||||
{
|
|
||||||
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"
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,9 +1,10 @@
|
|||||||
using AS1024.GeoFeed.Core.Interfaces;
|
using AS1024.GeoFeed.Core.CacheService;
|
||||||
using AS1024.GeoFeed.Core.GeoFeedPreloader;
|
using AS1024.GeoFeed.Core.GeoFeedPreloader;
|
||||||
using AS1024.GeoFeed.Core.GeoFeedProviders;
|
using AS1024.GeoFeed.Core.GeoFeedProviders;
|
||||||
using Microsoft.EntityFrameworkCore;
|
|
||||||
using AS1024.GeoFeed.Core.GeoFeedSqliteLocalCache;
|
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
|
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 =>
|
||||||
{
|
{
|
||||||
|
|||||||
64
README.md
64
README.md
@@ -6,23 +6,44 @@ This web application provides a self-published IP geolocation feed, conforming t
|
|||||||
|
|
||||||
The application is implemented in C# using .NET 8.0, ensuring a robust and modern back-end architecture. It's built to communicate securely over HTTPS with the NetBox API, adhering to best practices for data transmission and security.
|
The application is implemented in C# using .NET 8.0, ensuring a robust and modern back-end architecture. It's built to communicate securely over HTTPS with the NetBox API, adhering to best practices for data transmission and security.
|
||||||
|
|
||||||
|
This project was inspired by the GitHub project [GeoBox](https://github.com/FrumentumNL/GeoBox), which provided foundational ideas for the development of this application.
|
||||||
|
|
||||||
|
## Application Variants
|
||||||
|
|
||||||
|
### AS1024.GeoFeed - MVC Version (Recommended)
|
||||||
|
|
||||||
|
The standard version of this application, named `AS1024.GeoFeed`, is built using the Model-View-Controller (MVC) architecture. This variant is fully supported and recommended for most use cases. It offers a complete set of features and is optimized for robustness and scalability.
|
||||||
|
|
||||||
|
### AS1024.GeoFeed.MinimalAPI - MinimalAPI Version (Experimental)
|
||||||
|
|
||||||
|
In addition to the standard MVC version, there is an experimental MinimalAPI version of the application named `AS1024.GeoFeed.MinimalAPI`. This variant is designed for environments that require extremely fast startup times, such as serverless containers. However, it comes with limited support and a reduced feature set. We recommend using the `AS1024.GeoFeed.MinimalAPI` version only if your deployment environment necessitates near-instant startup times and you are comfortable with its experimental nature and limitations.
|
||||||
|
|
||||||
|
**Note**: While the `AS1024.GeoFeed.MinimalAPI` version offers performance benefits in specific scenarios, we strongly recommend deploying the `AS1024.GeoFeed` MVC version for most applications to ensure full functionality and support.
|
||||||
|
|
||||||
## Features
|
## Features
|
||||||
|
|
||||||
- **GeoFeed Generation**: Dynamically generates a geolocation feed in CSV format as specified in RFC 8805.
|
- **GeoFeed Generation**: Dynamically generates a geolocation feed in CSV format as specified in RFC 8805.
|
||||||
- **Caching Mechanism**: Implements an efficient caching strategy to reduce redundant API calls and enhance performance.
|
- **Caching Mechanism**: Implements an efficient caching strategy to reduce redundant API calls and enhance performance.
|
||||||
- **Local Disk Fallback Caching Mechanism**: In the event of a failure to communicate with the NetBox instance specified, the web app will return data that is locally cached inside a SQLite database.
|
- **Local Disk Fallback Caching Mechanism**: In the event of a failure to communicate with the NetBox instance specified, the web app will return data that is locally cached inside a SQLite database. In the minimal API version of the GeoFeed application, this will be stored as a file, rather than a SQLite database as Minimal API does not yet support Entity Framework Core at this time.
|
||||||
- **Secure Communication**: Ensures secure data retrieval from NetBox over HTTPS.
|
- **Secure Communication**: Ensures secure data retrieval from NetBox over HTTPS.
|
||||||
|
|
||||||
## Configuration
|
## Configuration
|
||||||
|
|
||||||
The application requires the following configuration variables to be set:
|
The application requires the following configuration variables to be set, depending on the version you are using:
|
||||||
|
|
||||||
|
### For AS1024.GeoFeed - MVC Version
|
||||||
1. **APIKey**: This is the API key used for authenticating with the NetBox API. Ensure this key has the necessary permissions to access the required resources.
|
1. **APIKey**: This is the API key used for authenticating with the NetBox API. Ensure this key has the necessary permissions to access the required resources.
|
||||||
2. **NetBoxHost**: The hostname of the NetBox instance from which the application retrieves data. For example, `netbox.example.com`.
|
2. **NetBoxHost**: The hostname of the NetBox instance from which the application retrieves data. For example, `netbox.example.com`.
|
||||||
3. **LocalFeedCache**: This connection string is for a local SQLite Database that caches the geofeed data from Netbox.
|
3. **LocalFeedCache**: This connection string is for a local SQLite Database (using EF Core) that caches the geofeed data from Netbox. The syntax should follow the standard SQLite EF Core connection string format.
|
||||||
|
|
||||||
|
### For AS1024.GeoFeed.MinimalAPI - MinimalAPI Version
|
||||||
|
1. **APIKey**: This is the API key used for authenticating with the NetBox API. Ensure this key has the necessary permissions to access the required resources.
|
||||||
|
2. **NetBoxHost**: The hostname of the NetBox instance from which the application retrieves data. For example, `netbox.example.com`.
|
||||||
|
3. **TempCache** (optional): This configuration is specific to the Minimal API version. It is optional, but if users wish to specify a different location for storing and serving the cached geofeed data, this value can be adjusted to point to the desired save location.
|
||||||
|
|
||||||
These variables can be set in your application's configuration file or through environment variables, depending on your deployment strategy.
|
These variables can be set in your application's configuration file or through environment variables, depending on your deployment strategy.
|
||||||
|
|
||||||
|
|
||||||
## NetBox Custom Fields
|
## NetBox Custom Fields
|
||||||
|
|
||||||
Ensure that your NetBox instance is configured with the following custom fields:
|
Ensure that your NetBox instance is configured with the following custom fields:
|
||||||
@@ -46,6 +67,37 @@ To build and run the application, follow these steps:
|
|||||||
5. After a successful build, you can start the application by running `dotnet run`.
|
5. After a successful build, you can start the application by running `dotnet run`.
|
||||||
6. The application will start, and you can access the endpoints as specified.
|
6. The application will start, and you can access the endpoints as specified.
|
||||||
|
|
||||||
|
## Building a Docker Image
|
||||||
|
|
||||||
|
### Building a Docker Image for the Minimal API Version
|
||||||
|
|
||||||
|
To containerize the `AS1024.GeoFeed` version using Docker, you can follow these steps to build a Docker image:
|
||||||
|
|
||||||
|
1. Open your terminal or command line interface.
|
||||||
|
2. Navigate to the root directory of this repository.
|
||||||
|
3. Execute the following Docker build command: ``docker buildx build --platform=linux/amd64,linux/arm64 -f .\AS1024.GeoFeed\Dockerfile .``
|
||||||
|
|
||||||
|
### Building a Docker Image for the Minimal API Version
|
||||||
|
|
||||||
|
To containerize the `AS1024.GeoFeed.MinimalAPI` version using Docker, you can follow these steps to build a Docker image:
|
||||||
|
|
||||||
|
1. Open your terminal or command line interface.
|
||||||
|
2. Navigate to the root directory of this repository.
|
||||||
|
3. Execute the following Docker build command: ``docker buildx build --platform=linux/amd64 -f .\AS1024.GeoFeed.MinimalAPI\Dockerfile.alpine-selfcontained .``
|
||||||
|
|
||||||
|
**Currently the minimal API version does not support being cross compiled for different CPU architectures. It is best built with the same CPU architecture where the build machine is targeting to.**
|
||||||
|
|
||||||
|
Ensure you have Docker installed and configured on your machine before executing this command. This Docker image can then be used to deploy the application in containerized environments such as Kubernetes or Docker Compose setups.
|
||||||
|
|
||||||
|
## Docker Deployment
|
||||||
|
|
||||||
|
To deploy the Docker container of this GeoFeed application, simply pull the image for the following variants:
|
||||||
|
|
||||||
|
- MVC Variant ``docker pull git.startmywifi.com/as1024/geofeed:latest``
|
||||||
|
- Minimal API Variant ``docker pull git.startmywifi.com/as1024/geofeed:aot-minimal``
|
||||||
|
|
||||||
|
To configure the container, please refer to the Configuration section above. You should be able to configure this with either an environment variable or map an ``appSettings.json`` file to the container.
|
||||||
|
|
||||||
## Endpoints
|
## Endpoints
|
||||||
|
|
||||||
The application provides the following key endpoints:
|
The application provides the following key endpoints:
|
||||||
@@ -57,6 +109,8 @@ The application provides the following key endpoints:
|
|||||||
|
|
||||||
This application is designed to always communicate over HTTPS with NetBox, ensuring that the data transfer is encrypted and secure.
|
This application is designed to always communicate over HTTPS with NetBox, ensuring that the data transfer is encrypted and secure.
|
||||||
|
|
||||||
---
|
## Extending Beyond NetBox
|
||||||
|
|
||||||
For more information about configuring and using this application, please refer to the official .NET documentation and the NetBox API guide.
|
If your current IPAM solution is not netbox and wish to extend this web application to use the desired IPAM solution of choice, the interface `IGeoFeedProvider` is available for extensibility. To use your custom IPAM backend ensure that `NetboxAoTGeoFeedProvider` and `NetboxGeoFeedProvider` are not registered in the dependency injection container in the Web Apps. Once unregistered, register your custom IPAM communication backend provider to your needs and the web app should work in both AOT and MVC mode.
|
||||||
|
|
||||||
|
Currently the Minimal API implementation of this web application only supports code that does not require reflection. This is a known limitation of native AOT deployments. If your code utilizes reflection or is not properly adapted for source generation, the minimal API version will **not work**.
|
||||||
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