Skip to content

Commit

Permalink
Relay DataProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
moreal committed Feb 5, 2024
1 parent 8817475 commit da051dd
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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<DataProviderOption> options) : ControllerBase
{
[HttpPost]
public async Task<IActionResult> 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);
}
}
}
6 changes: 6 additions & 0 deletions NineChroniclesUtilBackend/Options/DataProviderOption.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace NineChroniclesUtilBackend.Options;

public class DataProviderOption
{
public Uri Endpoint { get; set; }
}
9 changes: 9 additions & 0 deletions NineChroniclesUtilBackend/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
builder.Configuration.AddEnvironmentVariables();

builder.Services.Configure<HeadlessStateServiceOption>(builder.Configuration.GetRequiredSection("StateService"));
builder.Services.Configure<DataProviderOption>(builder.Configuration.GetRequiredSection("DataProvider"));
builder.Services.ConfigureHttpJsonOptions(options =>
options.SerializerOptions.Converters.Add(new JsonStringEnumConverter()));

Expand All @@ -21,12 +22,20 @@
builder.Services.AddHeadlessGQLClient()
.ConfigureHttpClient((provider, client) =>
client.BaseAddress = provider.GetRequiredService<IOptions<HeadlessStateServiceOption>>().Value.HeadlessEndpoint);
builder.Services.AddCors();
builder.Services.AddHttpClient();

var app = builder.Build();

app.UseSwagger();
app.UseSwaggerUI();
app.UseHttpsRedirection();
app.MapControllers();
app.UseCors(policy =>
{
policy.AllowAnyMethod();
policy.AllowAnyOrigin();
policy.AllowAnyHeader();
});

app.Run();
3 changes: 3 additions & 0 deletions NineChroniclesUtilBackend/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,8 @@
"AllowedHosts": "*",
"StateService": {
"HeadlessEndpoint": "https://9c-main-full-state.nine-chronicles.com/graphql"
},
"DataProvider": {
"Endpoint": "http://heimdall-dp.9c.gg/graphql"
}
}

0 comments on commit da051dd

Please sign in to comment.