From da051dd150650a1b0b3416d8c215cc3109cd0671 Mon Sep 17 00:00:00 2001 From: moreal Date: Mon, 5 Feb 2024 16:49:32 +0900 Subject: [PATCH] Relay DataProvider --- .../DataProviderRelayController.cs | 34 +++++++++++++++++++ .../Options/DataProviderOption.cs | 6 ++++ NineChroniclesUtilBackend/Program.cs | 9 +++++ NineChroniclesUtilBackend/appsettings.json | 3 ++ 4 files changed, 52 insertions(+) create mode 100644 NineChroniclesUtilBackend/Controllers/DataProviderRelayController.cs create mode 100644 NineChroniclesUtilBackend/Options/DataProviderOption.cs diff --git a/NineChroniclesUtilBackend/Controllers/DataProviderRelayController.cs b/NineChroniclesUtilBackend/Controllers/DataProviderRelayController.cs new file mode 100644 index 00000000..f6c82485 --- /dev/null +++ b/NineChroniclesUtilBackend/Controllers/DataProviderRelayController.cs @@ -0,0 +1,34 @@ +using System.Text; +using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.Options; +using NineChroniclesUtilBackend.Options; + +namespace NineChroniclesUtilBackend.Controllers; + +[ApiController] +[Route("dp")] +public class DataProviderRelayController(IOptions options) : ControllerBase +{ + [HttpPost] + public async Task Post([FromServices] IHttpClientFactory clientFactory) + { + var body = await new StreamReader(Request.Body).ReadToEndAsync(); + + var client = clientFactory.CreateClient(); + + try + { + var response = await client.PostAsync(options.Value.Endpoint, + new StringContent(body, Encoding.UTF8, "application/json")); + + response.EnsureSuccessStatusCode(); + + var responseString = await response.Content.ReadAsStringAsync(); + return Content(responseString, "application/json"); + } + catch (HttpRequestException e) + { + return StatusCode(StatusCodes.Status503ServiceUnavailable, e.Message); + } + } +} diff --git a/NineChroniclesUtilBackend/Options/DataProviderOption.cs b/NineChroniclesUtilBackend/Options/DataProviderOption.cs new file mode 100644 index 00000000..5cd99ecb --- /dev/null +++ b/NineChroniclesUtilBackend/Options/DataProviderOption.cs @@ -0,0 +1,6 @@ +namespace NineChroniclesUtilBackend.Options; + +public class DataProviderOption +{ + public Uri Endpoint { get; set; } +} diff --git a/NineChroniclesUtilBackend/Program.cs b/NineChroniclesUtilBackend/Program.cs index 5155841c..a0908051 100644 --- a/NineChroniclesUtilBackend/Program.cs +++ b/NineChroniclesUtilBackend/Program.cs @@ -8,6 +8,7 @@ builder.Configuration.AddEnvironmentVariables(); builder.Services.Configure(builder.Configuration.GetRequiredSection("StateService")); +builder.Services.Configure(builder.Configuration.GetRequiredSection("DataProvider")); builder.Services.ConfigureHttpJsonOptions(options => options.SerializerOptions.Converters.Add(new JsonStringEnumConverter())); @@ -21,6 +22,8 @@ builder.Services.AddHeadlessGQLClient() .ConfigureHttpClient((provider, client) => client.BaseAddress = provider.GetRequiredService>().Value.HeadlessEndpoint); +builder.Services.AddCors(); +builder.Services.AddHttpClient(); var app = builder.Build(); @@ -28,5 +31,11 @@ app.UseSwaggerUI(); app.UseHttpsRedirection(); app.MapControllers(); +app.UseCors(policy => +{ + policy.AllowAnyMethod(); + policy.AllowAnyOrigin(); + policy.AllowAnyHeader(); +}); app.Run(); diff --git a/NineChroniclesUtilBackend/appsettings.json b/NineChroniclesUtilBackend/appsettings.json index 257fcb44..56087c99 100644 --- a/NineChroniclesUtilBackend/appsettings.json +++ b/NineChroniclesUtilBackend/appsettings.json @@ -8,5 +8,8 @@ "AllowedHosts": "*", "StateService": { "HeadlessEndpoint": "https://9c-main-full-state.nine-chronicles.com/graphql" + }, + "DataProvider": { + "Endpoint": "http://heimdall-dp.9c.gg/graphql" } }