Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve zones.json for deployment #64

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion complete/Api/Api.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,7 @@
<ItemGroup>
<ProjectReference Include="..\ServiceDefaults\ServiceDefaults.csproj" />
</ItemGroup>
</Project>
<ItemGroup>
<EmbeddedResource Include="wwwroot\zones.json" />
</ItemGroup>
</Project>
16 changes: 9 additions & 7 deletions complete/Api/Data/NwsManager.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
using System.Text.Json;
using System.Web;
using System.Reflection;
using Microsoft.AspNetCore.Http.HttpResults;
using Microsoft.Extensions.Caching.Memory;
using Api.Data;

namespace Api
{
public class NwsManager(HttpClient httpClient, IMemoryCache cache, IWebHostEnvironment webHostEnvironment)
public class NwsManager(HttpClient httpClient, IMemoryCache cache)
{
private static readonly JsonSerializerOptions options = new(JsonSerializerDefaults.Web);

Expand All @@ -25,15 +26,16 @@ public class NwsManager(HttpClient httpClient, IMemoryCache cache, IWebHostEnvir
// .Distinct()
// .ToArray() ?? [];

// Deserialize the zones.json file from the wwwroot folder
var zonesFilePath = Path.Combine(webHostEnvironment.WebRootPath, "zones.json");
if (!File.Exists(zonesFilePath))
// Deserialize the zones.json file from the embedded resource
var assembly = Assembly.GetExecutingAssembly();
var resourceName = "Api.wwwroot.zones.json";
using var stream = assembly.GetManifestResourceStream(resourceName);
if (stream == null)
{
return [];
}

using var zonesJson = File.OpenRead(zonesFilePath);
var zones = await JsonSerializer.DeserializeAsync<ZonesResponse>(zonesJson, options);
var zones = await JsonSerializer.DeserializeAsync<ZonesResponse>(stream, options);

return zones?.Features
?.Where(f => f.Properties?.ObservationStations?.Count > 0)
Expand Down Expand Up @@ -122,4 +124,4 @@ public static IServiceCollection AddNwsManager(this IServiceCollection services)
return app;
}
}
}
}