RSE/Proxy/Program.cs

72 lines
2.0 KiB
C#

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 = JsonSerializer.SerializeToUtf8Bytes(communicationObject);
using RequestSocket client = new();
client.Connect("tcp://127.0.0.1:5556");
client.SendFrame(bytes);
byte[] msg = client.ReceiveFrameBytes();
client.Close();
return JsonSerializer.Deserialize<ScanningStatus>(msg);
});
RouteGroupBuilder searchApi = app.MapGroup("/search");
progressApi.AllowAnonymous();
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<SearchResults?>(msg);
});
app.Run();
[JsonSerializable(typeof(ScanningStatus))]
[JsonSerializable(typeof(SearchResults))]
[JsonSerializable(typeof(CommunicationObject))]
internal partial class AppJsonSerializerContext : JsonSerializerContext
{
}