Skip to content

Commit

Permalink
feat: add proxy aa-server route
Browse files Browse the repository at this point in the history
  • Loading branch information
chaoxkang committed Feb 4, 2025
1 parent 0fe3be7 commit cba05ba
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/EoaServer.Application.Contracts/Proxy/IProxyService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;

namespace EoaServer.Proxy;

public interface IProxyService
{
Task<object> ProxyGetRequestAsync(string targetUrl);
}
1 change: 1 addition & 0 deletions src/EoaServer.Application/EoaServerApplicationModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public override void ConfigureServices(ServiceConfigurationContext context)
Configure<TokenSpenderOptions>(configuration.GetSection("TokenSpender"));
Configure<ActivityOptions>(configuration.GetSection("ActivityOptions"));
Configure<AElfScanOptions>(configuration.GetSection("AElfScanOptions"));
Configure<DidServerOptions>(configuration.GetSection("DidServerOptions"));
Configure<TokenListOptions>(configuration.GetSection("Tokens"));
Configure<TokenInfoOptions>(configuration.GetSection("TokenInfo"));
Configure<AssetsInfoOptions>(configuration.GetSection("AssetsInfo"));
Expand Down
7 changes: 7 additions & 0 deletions src/EoaServer.Application/Options/DidServerOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace EoaServer.Options;

public class DidServerOptions
{
public string BaseUrl { get; set; }
public int Timeout { get; set; }
}
42 changes: 42 additions & 0 deletions src/EoaServer.Application/Proxy/ProxyService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System;
using System.Net.Http;
using System.Threading.Tasks;
using EoaServer.Options;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using Volo.Abp;
using Volo.Abp.Auditing;
using JsonSerializer = System.Text.Json.JsonSerializer;

namespace EoaServer.Proxy;

[RemoteService(false)]
[DisableAuditing]
public class ProxyService : EoaServerBaseService, IProxyService
{
private readonly DidServerOptions _didServerOptions;
private readonly HttpClient _httpClient;

public ProxyService(IOptionsSnapshot<DidServerOptions> didServerOptions, IHttpClientFactory httpClientFactory)
{
_didServerOptions = didServerOptions.Value;
_httpClient = httpClientFactory.CreateClient();
}

public async Task<object> ProxyGetRequestAsync(string targetUrl)
{
var targetRequest = new HttpRequestMessage
{
Method = HttpMethod.Get,
RequestUri = new Uri(_didServerOptions.BaseUrl + "/" + targetUrl),
};

var response = await _httpClient.SendAsync(targetRequest, HttpCompletionOption.ResponseHeadersRead);
var responseBody = await response.Content.ReadAsStringAsync();
var result = JsonSerializer.Deserialize<object?>(responseBody);
return new JsonResult(result)
{
ContentType = "application/json"
};
}
}
4 changes: 4 additions & 0 deletions src/EoaServer.HttpApi.Host/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@
"BaseUrl": "https://testnet.aelfscan.io",
"Timeout": 5000
},
"DidServerOptions": {
"BaseUrl": "https://aa-portkey-test.portkey.finance",
"Timeout": 5000
},
"ActivityOptions": {
"ActivityTransactionFeeFix": [
{
Expand Down
28 changes: 28 additions & 0 deletions src/EoaServer.HttpApi/Controllers/ProxyController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System.Threading.Tasks;
using Asp.Versioning;
using EoaServer.Proxy;
using Microsoft.AspNetCore.Mvc;
using Volo.Abp;

namespace EoaServer.Controllers;

[RemoteService]
[Area("proxy")]
[ControllerName("Proxy")]
[Route("proxy")]
public class ProxyController : EoaServerBaseController
{
private readonly IProxyService _proxyService;

public ProxyController(IProxyService proxyService)
{
_proxyService = proxyService;
}

[HttpGet]
[Route("{**proxyUrl}")]
public async Task<object> ProxyAsync(string proxyUrl)
{
return await _proxyService.ProxyGetRequestAsync(proxyUrl);
}
}

0 comments on commit cba05ba

Please sign in to comment.