Compare commits
11 Commits
ade2861395
...
dd643ab8ab
| Author | SHA1 | Date | |
|---|---|---|---|
| dd643ab8ab | |||
| 2d68115f25 | |||
| 020db780a6 | |||
| 5fec5f4f92 | |||
| c43b218974 | |||
| c18e486750 | |||
| 9974bb5f0c | |||
| 37841db663 | |||
| e974635cfb | |||
| 2fa8f32f44 | |||
| 6e9d58c6e3 |
@@ -9,6 +9,15 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.0">
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="8.0.0" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="8.0.0">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.19.5" />
|
||||
</ItemGroup>
|
||||
|
||||
|
||||
@@ -4,6 +4,7 @@ using AS1024.GeoFeed.GeoFeedBuilder;
|
||||
using Microsoft.Extensions.Caching.Memory;
|
||||
using AS1024.GeoFeed.Models;
|
||||
using System.Text;
|
||||
using AS1024.GeoFeed.GeoFeedLocalCache;
|
||||
|
||||
namespace AS1024.GeoFeed.Controllers
|
||||
{
|
||||
@@ -17,13 +18,16 @@ namespace AS1024.GeoFeed.Controllers
|
||||
private readonly IMemoryCache memoryCache;
|
||||
private readonly IWebHostEnvironment environment;
|
||||
private readonly ILogger<GeofeedController> logger;
|
||||
private readonly GeoFeedCacheDbContext dbContext;
|
||||
private const string GeoFeedCacheKey = "GeoFeedData";
|
||||
|
||||
public GeofeedController(IGeoFeedProvider builder,
|
||||
IMemoryCache memoryCache,
|
||||
IWebHostEnvironment environment,
|
||||
ILogger<GeofeedController> logger) {
|
||||
ILogger<GeofeedController> logger,
|
||||
GeoFeedCacheDbContext dbContext) {
|
||||
this.logger = logger;
|
||||
this.dbContext = dbContext;
|
||||
this.builder = builder;
|
||||
this.memoryCache = memoryCache;
|
||||
this.environment = environment;
|
||||
@@ -46,19 +50,36 @@ namespace AS1024.GeoFeed.Controllers
|
||||
}
|
||||
}
|
||||
|
||||
string csvContent = feed.ToGeoFeedCsv(); // Assuming ToGeoFeedCsv() returns a string in CSV format.
|
||||
byte[] contentBytes = Encoding.UTF8.GetBytes(csvContent);
|
||||
string contentType = "text/csv";
|
||||
return ReturnFile(feed);
|
||||
} catch (HttpRequestException ex)
|
||||
{
|
||||
logger.LogWarning($"Temporary failure of retrieving GeoData from upstream. {ex}");
|
||||
var results =
|
||||
dbContext.GeoFeedCacheEntries.ToList();
|
||||
List<IPGeoFeed> cachedData = [];
|
||||
results.ForEach(cachedData.Add);
|
||||
return ReturnFile(cachedData);
|
||||
}
|
||||
|
||||
return new FileContentResult(contentBytes, contentType)
|
||||
{
|
||||
FileDownloadName = "geofeed.csv"
|
||||
};
|
||||
} catch (Exception ex)
|
||||
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.
|
||||
byte[] contentBytes = Encoding.UTF8.GetBytes(csvContent);
|
||||
string contentType = "text/csv";
|
||||
|
||||
return new FileContentResult(contentBytes, contentType)
|
||||
{
|
||||
FileDownloadName = "geofeed.csv"
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
15
AS1024.GeoFeed/GeoFeedLocalCache/GeoFeedCacheDbContext.cs
Normal file
15
AS1024.GeoFeed/GeoFeedLocalCache/GeoFeedCacheDbContext.cs
Normal file
@@ -0,0 +1,15 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace AS1024.GeoFeed.GeoFeedLocalCache
|
||||
{
|
||||
public class GeoFeedCacheDbContext : DbContext
|
||||
{
|
||||
public GeoFeedCacheDbContext(DbContextOptions options)
|
||||
: base(options)
|
||||
{
|
||||
}
|
||||
|
||||
public virtual DbSet<GeoFeedCacheEntry> GeoFeedCacheEntries { get; set; }
|
||||
}
|
||||
|
||||
}
|
||||
12
AS1024.GeoFeed/GeoFeedLocalCache/GeoFeedCacheEntry.cs
Normal file
12
AS1024.GeoFeed/GeoFeedLocalCache/GeoFeedCacheEntry.cs
Normal file
@@ -0,0 +1,12 @@
|
||||
using AS1024.GeoFeed.Models;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace AS1024.GeoFeed.GeoFeedLocalCache
|
||||
{
|
||||
public class GeoFeedCacheEntry : IPGeoFeed
|
||||
{
|
||||
[Key]
|
||||
public int Id { get; set; }
|
||||
}
|
||||
|
||||
}
|
||||
90
AS1024.GeoFeed/GeoFeedLocalCache/GeoFeedCacheService.cs
Normal file
90
AS1024.GeoFeed/GeoFeedLocalCache/GeoFeedCacheService.cs
Normal file
@@ -0,0 +1,90 @@
|
||||
using AS1024.GeoFeed.Interfaces;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
namespace AS1024.GeoFeed.GeoFeedLocalCache
|
||||
{
|
||||
public class GeoFeedCacheService : IHostedService
|
||||
{
|
||||
private readonly ILogger<GeoFeedCacheService> logger;
|
||||
private readonly IGeoFeedProvider feedProvider;
|
||||
private readonly IHost host;
|
||||
|
||||
public GeoFeedCacheService(ILogger<GeoFeedCacheService> logger,
|
||||
IGeoFeedProvider feedProvider,
|
||||
IHost host)
|
||||
{
|
||||
this.logger = logger;
|
||||
this.feedProvider = feedProvider;
|
||||
this.host = host;
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
await DBContextMigrate();
|
||||
|
||||
List<GeoFeedCacheEntry> geoFeedCacheEntry = [];
|
||||
while (!Token.IsCancellationRequested)
|
||||
{
|
||||
logger.LogInformation("Running on disk fallback cache process...");
|
||||
try
|
||||
{
|
||||
using var scope = host.Services.CreateScope();
|
||||
using var dbContext = scope.ServiceProvider.GetRequiredService<GeoFeedCacheDbContext>();
|
||||
var results = await feedProvider.GetGeoFeedData();
|
||||
|
||||
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, Token);
|
||||
await dbContext.SaveChangesAsync(Token);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogWarning("On disk cache failed to run. Waiting on 30 minutes before retry...");
|
||||
}
|
||||
await Task.Delay(TimeSpan.FromMinutes(30));
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private async Task DBContextMigrate()
|
||||
{
|
||||
using IServiceScope scope = host.Services.CreateScope();
|
||||
using GeoFeedCacheDbContext? dbContext =
|
||||
scope.ServiceProvider.GetService<GeoFeedCacheDbContext>();
|
||||
#pragma warning disable CS8602 // Dereference of a possibly null reference.
|
||||
if (dbContext.Database.GetPendingMigrations().Any()) {
|
||||
await dbContext.Database.MigrateAsync();
|
||||
}
|
||||
#pragma warning restore CS8602 // Dereference of a possibly null reference.
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Design;
|
||||
|
||||
namespace AS1024.GeoFeed.GeoFeedLocalCache
|
||||
{
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
54
AS1024.GeoFeed/Migrations/20240108180753_DiskCacheMigration.Designer.cs
generated
Normal file
54
AS1024.GeoFeed/Migrations/20240108180753_DiskCacheMigration.Designer.cs
generated
Normal file
@@ -0,0 +1,54 @@
|
||||
// <auto-generated />
|
||||
using System;
|
||||
using AS1024.GeoFeed.GeoFeedLocalCache;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace AS1024.GeoFeed.Migrations
|
||||
{
|
||||
[DbContext(typeof(GeoFeedCacheDbContext))]
|
||||
[Migration("20240108180753_DiskCacheMigration")]
|
||||
partial class DiskCacheMigration
|
||||
{
|
||||
/// <inheritdoc />
|
||||
protected override void BuildTargetModel(ModelBuilder modelBuilder)
|
||||
{
|
||||
#pragma warning disable 612, 618
|
||||
modelBuilder.HasAnnotation("ProductVersion", "8.0.0");
|
||||
|
||||
modelBuilder.Entity("AS1024.GeoFeed.GeoFeedLocalCache.GeoFeedCacheEntry", b =>
|
||||
{
|
||||
b.Property<int>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("INTEGER");
|
||||
|
||||
b.Property<string>("GeolocCity")
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.Property<string>("GeolocCountry")
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.Property<bool?>("GeolocHasLocation")
|
||||
.HasColumnType("INTEGER");
|
||||
|
||||
b.Property<string>("GeolocPostalCode")
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.Property<string>("GeolocRegion")
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.Property<string>("Prefix")
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.ToTable("GeoFeedCacheEntries");
|
||||
});
|
||||
#pragma warning restore 612, 618
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace AS1024.GeoFeed.Migrations
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public partial class DiskCacheMigration : Migration
|
||||
{
|
||||
/// <inheritdoc />
|
||||
protected override void Up(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.CreateTable(
|
||||
name: "GeoFeedCacheEntries",
|
||||
columns: table => new
|
||||
{
|
||||
Id = table.Column<int>(type: "INTEGER", nullable: false)
|
||||
.Annotation("Sqlite:Autoincrement", true),
|
||||
GeolocCity = table.Column<string>(type: "TEXT", nullable: true),
|
||||
GeolocCountry = table.Column<string>(type: "TEXT", nullable: true),
|
||||
GeolocHasLocation = table.Column<bool>(type: "INTEGER", nullable: true),
|
||||
GeolocRegion = table.Column<string>(type: "TEXT", nullable: true),
|
||||
GeolocPostalCode = table.Column<string>(type: "TEXT", nullable: true),
|
||||
Prefix = table.Column<string>(type: "TEXT", nullable: true)
|
||||
},
|
||||
constraints: table =>
|
||||
{
|
||||
table.PrimaryKey("PK_GeoFeedCacheEntries", x => x.Id);
|
||||
});
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override void Down(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.DropTable(
|
||||
name: "GeoFeedCacheEntries");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
// <auto-generated />
|
||||
using System;
|
||||
using AS1024.GeoFeed.GeoFeedLocalCache;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace AS1024.GeoFeed.Migrations
|
||||
{
|
||||
[DbContext(typeof(GeoFeedCacheDbContext))]
|
||||
partial class GeoFeedCacheDbContextModelSnapshot : ModelSnapshot
|
||||
{
|
||||
protected override void BuildModel(ModelBuilder modelBuilder)
|
||||
{
|
||||
#pragma warning disable 612, 618
|
||||
modelBuilder.HasAnnotation("ProductVersion", "8.0.0");
|
||||
|
||||
modelBuilder.Entity("AS1024.GeoFeed.GeoFeedLocalCache.GeoFeedCacheEntry", b =>
|
||||
{
|
||||
b.Property<int>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("INTEGER");
|
||||
|
||||
b.Property<string>("GeolocCity")
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.Property<string>("GeolocCountry")
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.Property<bool?>("GeolocHasLocation")
|
||||
.HasColumnType("INTEGER");
|
||||
|
||||
b.Property<string>("GeolocPostalCode")
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.Property<string>("GeolocRegion")
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.Property<string>("Prefix")
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.ToTable("GeoFeedCacheEntries");
|
||||
});
|
||||
#pragma warning restore 612, 618
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,7 @@
|
||||
using AS1024.GeoFeed.GeoFeedBuilder;
|
||||
using AS1024.GeoFeed.GeoFeedLocalCache;
|
||||
using AS1024.GeoFeed.Interfaces;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace AS1024.GeoFeed
|
||||
{
|
||||
@@ -11,6 +13,11 @@ namespace AS1024.GeoFeed
|
||||
|
||||
builder.Services.AddHostedService<PreLoadGeoFeed>();
|
||||
builder.Services.AddTransient<IGeoFeedProvider, NetBoxGeoFeedProvider>();
|
||||
builder.Services.AddHostedService<GeoFeedCacheService>();
|
||||
builder.Services.AddDbContext<GeoFeedCacheDbContext>(options =>
|
||||
{
|
||||
options.UseSqlite(builder.Configuration.GetConnectionString("LocalFeedCache"));
|
||||
});
|
||||
builder.Services.AddHttpClient();
|
||||
builder.Services.AddMemoryCache();
|
||||
// Add services to the container.
|
||||
|
||||
@@ -5,6 +5,9 @@
|
||||
"Microsoft.AspNetCore": "Warning"
|
||||
}
|
||||
},
|
||||
"ConnectionStrings": {
|
||||
"LocalFeedCache": "Data Source=localcache.db"
|
||||
},
|
||||
"AllowedHosts": "*",
|
||||
"APIKey": "",
|
||||
"NetBoxHost": ""
|
||||
|
||||
@@ -10,6 +10,7 @@ The application is implemented in C# using .NET 8.0, ensuring a robust and moder
|
||||
|
||||
- **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.
|
||||
- **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.
|
||||
- **Secure Communication**: Ensures secure data retrieval from NetBox over HTTPS.
|
||||
|
||||
## Configuration
|
||||
@@ -18,6 +19,7 @@ The application requires the following configuration variables to be set:
|
||||
|
||||
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. **LocalFeedCache**: This connection string is for a local SQLite Database that caches the geofeed data from Netbox.
|
||||
|
||||
These variables can be set in your application's configuration file or through environment variables, depending on your deployment strategy.
|
||||
|
||||
|
||||
Reference in New Issue
Block a user