using System.Text.Json; using System.Text.Json.Serialization; using AspNetCoreRateLimit; using Microsoft.AspNetCore.HttpOverrides; using Microsoft.Extensions.Caching.Memory; using Models.Model.External; using NetMQ; using NetMQ.Sockets; const string myAllowSpecificOrigins = "_myAllowSpecificOrigins"; WebApplicationBuilder builder = WebApplication.CreateSlimBuilder(args); builder.Services.ConfigureHttpJsonOptions(options => { options.SerializerOptions.TypeInfoResolverChain.Insert(0, AppJsonSerializerContext.Default); }); builder.Services.AddCors(options => { options.AddPolicy(name: myAllowSpecificOrigins, x => x.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader()); }); builder.Services.AddMemoryCache(options => options.ExpirationScanFrequency = TimeSpan.FromSeconds(5)); WebApplication app = builder.Build(); app.UseForwardedHeaders(new() { ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto }); app.UseCors(myAllowSpecificOrigins); RouteGroupBuilder progressApi = app.MapGroup("/progress"); progressApi.MapGet("/", (IMemoryCache memoryCache) => { const string cacheKey = "progress_status"; if (memoryCache.TryGetValue(cacheKey, out ScanningStatus scanningStatus)) { return scanningStatus; } CommunicationObject communicationObject = new() { Command = CommunicationCommand.GetScanningProgress }; byte[] bytes = JsonSerializer.SerializeToUtf8Bytes(communicationObject); using RequestSocket client = new(); client.Connect("tcp://127.0.0.1:5556"); client.SendFrame(bytes); byte[] msg = client.ReceiveFrameBytes(); client.Close(); scanningStatus = JsonSerializer.Deserialize(msg); memoryCache.Set(cacheKey, scanningStatus, DateTimeOffset.Now.AddSeconds(5)); return scanningStatus; }); RouteGroupBuilder searchApi = app.MapGroup("/search"); searchApi.MapGet("/{term}", (string term) => { CommunicationObject communicationObject = new(); communicationObject.Command = CommunicationCommand.GetSearches; communicationObject.SearchTerm = term; byte[] bytes = JsonSerializer.SerializeToUtf8Bytes(communicationObject); using RequestSocket client = new(); client.Connect("tcp://127.0.0.1:5556"); client.SendFrame(bytes); string msg = client.ReceiveFrameString(); client.Close(); return JsonSerializer.Deserialize(msg); }); app.Run(); [JsonSerializable(typeof(ScanningStatus))] //[JsonSerializable(typeof(SearchResults))] [JsonSerializable(typeof(CommunicationObject))] internal partial class AppJsonSerializerContext : JsonSerializerContext { }