-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from planetarium/relay-dp
Relay DataProvider
- Loading branch information
Showing
4 changed files
with
52 additions
and
0 deletions.
There are no files selected for viewing
34 changes: 34 additions & 0 deletions
34
NineChroniclesUtilBackend/Controllers/DataProviderRelayController.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters