using System.Text.Json; using System.Text.Json.Serialization; using MessagePack; using Microsoft.AspNetCore.Cors.Infrastructure; using Models.Model.External; using NetMQ; using NetMQ.Sockets; WebApplicationBuilder builder = WebApplication.CreateSlimBuilder(args); builder.Services.ConfigureHttpJsonOptions(options => { options.SerializerOptions.TypeInfoResolverChain.Insert(0, AppJsonSerializerContext.Default); }); builder.Services.AddCors(options => { options.AddPolicy("CorsPolicy", x => x.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader().Build()); }); WebApplication app = builder.Build(); app.UseCors(); RouteGroupBuilder progressApi = app.MapGroup("/progress"); progressApi.AllowAnonymous(); progressApi.DisableAntiforgery(); progressApi.MapGet("/", () => { CommunicationObject communicationObject = new() { Command = CommunicationCommand.GetScanningProgress }; byte[] bytes = MessagePackSerializer.Serialize(communicationObject); using RequestSocket client = new(); client.Connect("tcp://127.0.0.1:5556"); client.SendFrame(bytes); byte[] msg = client.ReceiveFrameBytes(); client.Close(); return MessagePackSerializer.Deserialize(msg, MessagePackSerializerOptions.Standard.WithCompression(MessagePackCompression.Lz4BlockArray)); }); RouteGroupBuilder searchApi = app.MapGroup("/search"); progressApi.AllowAnonymous(); searchApi.MapGet("/{term}", (string term) => { CommunicationObject communicationObject = new(); communicationObject.Command = CommunicationCommand.GetSearches; communicationObject.SearchTerm = term; byte[] bytes = MessagePackSerializer.Serialize(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))] internal partial class AppJsonSerializerContext : JsonSerializerContext { }