Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Relay DataProvider #4

Merged
merged 1 commit into from
Feb 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"
}
}