Skip to content

Commit

Permalink
Merge pull request #63 from moreal/bugfix/api/graphql-by-network
Browse files Browse the repository at this point in the history
fix(api): request GraphQL by network name
  • Loading branch information
moreal authored May 16, 2024
2 parents 857a861 + 5b85ede commit c9409d9
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 8 deletions.
9 changes: 7 additions & 2 deletions Mimir/Options/HeadlessStateServiceOption.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
namespace Mimir.Options;

public class HeadlessStateServiceOption
public class HeadlessEndpoint
{
public Uri HeadlessEndpoint { get; set; }
public Uri Endpoint { get; set; }
public string? JwtIssuer { get; set; }
public string? JwtSecretKey { get; set; }
}

public class HeadlessStateServiceOption
{
public Dictionary<string, HeadlessEndpoint> Endpoints { get; set; }
}
17 changes: 12 additions & 5 deletions Mimir/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
{
options.SupportNonNullableReferenceTypes();
});
builder.Services.AddSingleton<IStateService, HeadlessStateService>();
builder.Services.AddHttpContextAccessor();
builder.Services.AddTransient<IStateService, HeadlessStateService>();
builder.Services.AddSingleton<MongoDBCollectionService>();
builder.Services.AddSingleton<ArenaRankingRepository>();
builder.Services.AddSingleton<TableSheetsRepository>();
Expand All @@ -33,15 +34,21 @@
.ConfigureHttpClient((provider, client) =>
{
var headlessStateServiceOption = provider.GetRequiredService<IOptions<HeadlessStateServiceOption>>();
client.BaseAddress = headlessStateServiceOption.Value.HeadlessEndpoint;
var httpContextAccessor = provider.GetRequiredService<IHttpContextAccessor>();
if (httpContextAccessor.HttpContext is null
|| !httpContextAccessor.HttpContext.Request.RouteValues.TryGetValue("network", out object? value)
|| value is not string network
|| !headlessStateServiceOption.Value.Endpoints.TryGetValue(network, out var endpoint)) return;
client.BaseAddress = endpoint.Endpoint;

if (headlessStateServiceOption.Value.JwtSecretKey is not null && headlessStateServiceOption.Value.JwtIssuer is not null)
if (endpoint.JwtSecretKey is not null && endpoint.JwtIssuer is not null)
{
var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(headlessStateServiceOption.Value.JwtSecretKey));
var key = new SymmetricSecurityKey(
Encoding.UTF8.GetBytes(endpoint.JwtSecretKey));
var creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);

var token = new JwtSecurityToken(
issuer: headlessStateServiceOption.Value.JwtIssuer,
issuer: endpoint.JwtIssuer,
expires: DateTime.UtcNow.AddMinutes(5),
signingCredentials: creds);

Expand Down
9 changes: 8 additions & 1 deletion Mimir/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@
"ConnectionString": "mongodb://rootuser:rootpass@localhost:27017"
},
"StateService": {
"HeadlessEndpoint": "https://9c-main-full-state.nine-chronicles.com/graphql"
"Endpoints": {
"odin": {
"Endpoint": "https://9c-main-full-state.nine-chronicles.com/graphql"
},
"heimdall": {
"Endpoint": "https://heimdall-full-state.nine-chronicles.com/graphql"
}
}
}
}

0 comments on commit c9409d9

Please sign in to comment.