diff --git a/AS1024.GeoFeed.Core.WebLogic/AS1024.GeoFeed.Core.WebLogic.csproj b/AS1024.GeoFeed.Core.WebLogic/AS1024.GeoFeed.Core.WebLogic.csproj new file mode 100644 index 0000000..15b90de --- /dev/null +++ b/AS1024.GeoFeed.Core.WebLogic/AS1024.GeoFeed.Core.WebLogic.csproj @@ -0,0 +1,14 @@ + + + + net8.0 + enable + enable + Library + + + + + + + diff --git a/AS1024.GeoFeed.Core.WebLogic/GeoFeedRetrurn.cs b/AS1024.GeoFeed.Core.WebLogic/GeoFeedRetrurn.cs new file mode 100644 index 0000000..a17944e --- /dev/null +++ b/AS1024.GeoFeed.Core.WebLogic/GeoFeedRetrurn.cs @@ -0,0 +1,70 @@ +using System.Text; +using AS1024.GeoFeed.Core.Interfaces; +using AS1024.GeoFeed.Core.Tools; +using AS1024.GeoFeed.Models; +using Microsoft.Extensions.Caching.Memory; + +namespace AS1024.GeoFeed.Core.WebLogic +{ + public class GeoFeedReturn + { + private const string GeoFeedCacheKey = "GeoFeedData"; + private readonly IGeoFeedProvider provider; + private readonly ILogger logger; + private readonly IGeoFeedPersistentCacheProvider cacheProvider; + private readonly IMemoryCache memoryCache; + private readonly IWebHostEnvironment environment; + + public GeoFeedReturn(IGeoFeedProvider provider, + ILogger 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 GetGeoFeed() + { + bool isCached = true; + try + { + if (!memoryCache.TryGetValue(GeoFeedCacheKey, out List? 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(); + } + } +} \ No newline at end of file diff --git a/AS1024.GeoFeed.Core.WebLogic/Properties/launchSettings.json b/AS1024.GeoFeed.Core.WebLogic/Properties/launchSettings.json new file mode 100644 index 0000000..a25447b --- /dev/null +++ b/AS1024.GeoFeed.Core.WebLogic/Properties/launchSettings.json @@ -0,0 +1,12 @@ +{ + "profiles": { + "AS1024.GeoFeed.Core.WebLogic": { + "commandName": "Project", + "launchBrowser": true, + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + }, + "applicationUrl": "https://localhost:54455;http://localhost:54456" + } + } +} \ No newline at end of file diff --git a/AS1024.GeoFeed.MinimalAPI/AS1024.GeoFeed.MinimalAPI.csproj b/AS1024.GeoFeed.MinimalAPI/AS1024.GeoFeed.MinimalAPI.csproj index 53a525c..3249b47 100644 --- a/AS1024.GeoFeed.MinimalAPI/AS1024.GeoFeed.MinimalAPI.csproj +++ b/AS1024.GeoFeed.MinimalAPI/AS1024.GeoFeed.MinimalAPI.csproj @@ -14,6 +14,7 @@ + diff --git a/AS1024.GeoFeed.MinimalAPI/Program.cs b/AS1024.GeoFeed.MinimalAPI/Program.cs index 9f094a9..74e662b 100644 --- a/AS1024.GeoFeed.MinimalAPI/Program.cs +++ b/AS1024.GeoFeed.MinimalAPI/Program.cs @@ -1,11 +1,7 @@ using AS1024.GeoFeed.Core.CacheService; -using AS1024.GeoFeed.Core.GeoFeedLogic; using AS1024.GeoFeed.Core.GeoFeedPreloader; using AS1024.GeoFeed.Core.Interfaces; -using AS1024.GeoFeed.Core.Tools; -using AS1024.GeoFeed.Models; -using Microsoft.Extensions.Caching.Memory; -using System.Text; +using AS1024.GeoFeed.Core.WebLogic; namespace AS1024.GeoFeed.MinimalAPI { diff --git a/AS1024.GeoFeed.sln b/AS1024.GeoFeed.sln index 2fdd42c..e40d0d4 100644 --- a/AS1024.GeoFeed.sln +++ b/AS1024.GeoFeed.sln @@ -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 diff --git a/AS1024.GeoFeed/AS1024.GeoFeed.csproj b/AS1024.GeoFeed/AS1024.GeoFeed.csproj index 258f3ef..fa15b7c 100644 --- a/AS1024.GeoFeed/AS1024.GeoFeed.csproj +++ b/AS1024.GeoFeed/AS1024.GeoFeed.csproj @@ -23,6 +23,7 @@ + diff --git a/AS1024.GeoFeed/Controllers/GeofeedController.cs b/AS1024.GeoFeed/Controllers/GeofeedController.cs index a1d914a..e293ce2 100644 --- a/AS1024.GeoFeed/Controllers/GeofeedController.cs +++ b/AS1024.GeoFeed/Controllers/GeofeedController.cs @@ -1,5 +1,5 @@ using Microsoft.AspNetCore.Mvc; -using AS1024.GeoFeed.Core.GeoFeedLogic; +using AS1024.GeoFeed.Core.WebLogic; namespace AS1024.GeoFeed.Controllers { diff --git a/AS1024.GeoFeed/Program.cs b/AS1024.GeoFeed/Program.cs index 8124417..ba98d81 100644 --- a/AS1024.GeoFeed/Program.cs +++ b/AS1024.GeoFeed/Program.cs @@ -4,7 +4,7 @@ using AS1024.GeoFeed.Core.GeoFeedProviders; using Microsoft.EntityFrameworkCore; using AS1024.GeoFeed.Core.GeoFeedSqliteLocalCache; using AS1024.GeoFeed.Core.CacheService; -using AS1024.GeoFeed.Core.GeoFeedLogic; +using AS1024.GeoFeed.Core.WebLogic; namespace AS1024.GeoFeed {