Compare commits
4 Commits
a44b4f9421
...
42aacf497c
| Author | SHA1 | Date | |
|---|---|---|---|
| 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();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
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,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)
|
||||||
|
|||||||
@@ -1,19 +1,26 @@
|
|||||||
|
using AS1024.GeoFeed.Core.CacheService;
|
||||||
using AS1024.GeoFeed.Core.GeoFeedProviders;
|
using AS1024.GeoFeed.Core.GeoFeedProviders;
|
||||||
using AS1024.GeoFeed.Core.Interfaces;
|
using AS1024.GeoFeed.Core.Interfaces;
|
||||||
using AS1024.GeoFeed.Core.Tools;
|
using AS1024.GeoFeed.Core.Tools;
|
||||||
using AS1024.GeoFeed.Models;
|
using AS1024.GeoFeed.Models;
|
||||||
using Microsoft.AspNetCore.Http.HttpResults;
|
using Microsoft.AspNetCore.Http.HttpResults;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
using System.Text.Json.Serialization;
|
using Microsoft.Extensions.Caching.Memory;
|
||||||
|
using Microsoft.Extensions.Hosting;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using System;
|
||||||
|
|
||||||
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.AddTransient<IGeoFeedPersistentCacheProvider, GeoFeedLocalFileCache>();
|
||||||
builder.Services.AddMemoryCache();
|
builder.Services.AddMemoryCache();
|
||||||
builder.Services.AddLogging();
|
builder.Services.AddLogging();
|
||||||
builder.Services.AddHttpClient();
|
builder.Services.AddHttpClient();
|
||||||
@@ -21,31 +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, 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 (IGeoFeedProvider provider,
|
||||||
});
|
ILogger<Program> logger,
|
||||||
|
IGeoFeedPersistentCacheProvider cacheProvider,
|
||||||
|
IMemoryCache memoryCache,
|
||||||
|
IWebHostEnvironment environment) => {
|
||||||
|
return await GeoFeedDataRunner(provider, logger, cacheProvider, memoryCache, environment);
|
||||||
|
});
|
||||||
|
|
||||||
|
app.Map("/geofeed", async (IGeoFeedProvider provider,
|
||||||
|
ILogger<Program> logger,
|
||||||
|
IGeoFeedPersistentCacheProvider cacheProvider,
|
||||||
|
IMemoryCache memoryCache,
|
||||||
|
IWebHostEnvironment environment) => {
|
||||||
|
return await GeoFeedDataRunner(provider, logger, cacheProvider, memoryCache, environment);
|
||||||
|
});
|
||||||
|
|
||||||
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