Compare commits

8 Commits

4 changed files with 50 additions and 16 deletions

View File

@@ -9,6 +9,8 @@ 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;
@@ -46,8 +48,8 @@ namespace AS1024.GeoFeed.Core.WebLogic
}
return Results.File(Encoding.UTF8.GetBytes(feed.ToGeoFeedCsv(true, isCached)),
"text/csv",
"geofeed.csv");
GeoFeedMimeTypeReturn,
GeoFeedFileName);
}
catch (HttpRequestException ex)
@@ -56,8 +58,8 @@ namespace AS1024.GeoFeed.Core.WebLogic
string geoFeedData = cacheProvider.GetGeoFeed();
return Results.File(Encoding.UTF8.GetBytes(geoFeedData),
"text/csv",
"geofeed.csv");
GeoFeedMimeTypeReturn,
GeoFeedFileName);
}
catch (Exception ex)
{

View File

@@ -6,28 +6,34 @@ namespace AS1024.GeoFeed.Core.Tools
public static class GeoFeedTools
{
/// <summary>
/// Returns a CSV string for a given GeoFeed retreived from various sources
/// 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></returns>
/// <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();
// Append timestamp header if required
if (timeStamp)
csvContent.AppendLine($"# GeoFeed generated on {DateTime.UtcNow:R}");
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");
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)
{
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();
}
}
}
}

View 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"]

View File

@@ -107,8 +107,10 @@ The application provides the following key endpoints:
## Security and Compliance
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**.