Skip to content
This repository has been archived by the owner on Dec 5, 2024. It is now read-only.

Commit

Permalink
Merge pull request #7 from Etherna/feature/BEET-13-intercept-get-chunks
Browse files Browse the repository at this point in the history
implement chunk handling
  • Loading branch information
tmm360 authored Oct 14, 2024
2 parents c31493e + 119d525 commit 214599a
Show file tree
Hide file tree
Showing 7 changed files with 150 additions and 19 deletions.
24 changes: 12 additions & 12 deletions src/BeeTurbo/Handlers/BzzHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@
using Etherna.BeeNet.Hashing.Store;
using Etherna.BeeNet.Manifest;
using Etherna.BeeNet.Models;
using Etherna.BeeTurbo.Tools;
using Etherna.BeeTurbo.Options;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Options;
using System.Diagnostics.CodeAnalysis;
using System.Net.Http;
using System.Threading;
Expand All @@ -26,15 +27,17 @@
namespace Etherna.BeeTurbo.Handlers
{
internal sealed class BzzHandler(
DbRepositoryChunkStore chunkStore,
IHttpForwarder forwarder)
IChunkStore chunkStore,
IHttpForwarder forwarder,
IOptions<ForwarderOptions> options)
: IBzzHandler
{
// Fields.
private readonly ForwarderOptions options = options.Value;

// Methods.
[SuppressMessage("Design", "CA1031:Do not catch general exception types")]
public async Task<IResult> HandleAsync(
HttpContext httpContext,
string address,
string beeUrl)
public async Task<IResult> HandleAsync(HttpContext httpContext, string address)
{
if (httpContext.Request.Method == "GET")
{
Expand Down Expand Up @@ -62,17 +65,14 @@ public async Task<IResult> HandleAsync(

return Results.File(dataStream, contentType, fileName);
}
// catch (KeyNotFoundException)
catch //proceed with forward on any error
{
}
catch { } //proceed with forward on any error
}

using var socketsHttpHandler = new SocketsHttpHandler();
using var httpClient = new HttpMessageInvoker(socketsHttpHandler);
var error = await forwarder.SendAsync(
httpContext,
beeUrl,
options.BeeUrl,
httpClient);

if (error != ForwarderError.None)
Expand Down
72 changes: 72 additions & 0 deletions src/BeeTurbo/Handlers/ChunksHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// Copyright 2024-present Etherna SA
// This file is part of BeeTurbo.
//
// BeeTurbo is free software: you can redistribute it and/or modify it under the terms of the
// GNU Affero General Public License as published by the Free Software Foundation,
// either version 3 of the License, or (at your option) any later version.
//
// BeeTurbo is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
// without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// See the GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License along with BeeTurbo.
// If not, see <https://www.gnu.org/licenses/>.

using Etherna.BeeNet.Hashing.Store;
using Etherna.BeeNet.Models;
using Etherna.BeeTurbo.Options;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Options;
using System.Diagnostics.CodeAnalysis;
using System.Net.Http;
using System.Threading.Tasks;
using Yarp.ReverseProxy.Forwarder;

namespace Etherna.BeeTurbo.Handlers
{
internal sealed class ChunksHandler(
IChunkStore chunkStore,
IHttpForwarder forwarder,
IOptions<ForwarderOptions> options)
: IChunksHandler
{
// Fields.
private readonly ForwarderOptions options = options.Value;

// Methods.
[SuppressMessage("Design", "CA1031:Do not catch general exception types")]
public async Task<IResult> HandleAsync(HttpContext httpContext, string hash)
{
if (httpContext.Request.Method == "GET")
{
try
{
var swarmHash = SwarmHash.FromString(hash);
var chunk = await chunkStore.GetAsync(swarmHash, null);

return Results.File(
chunk.GetSpanAndData(),
"application/octet-stream",
hash);
}
catch { } //proceed with forward on any error
}

using var socketsHttpHandler = new SocketsHttpHandler();
using var httpClient = new HttpMessageInvoker(socketsHttpHandler);
var error = await forwarder.SendAsync(
httpContext,
options.BeeUrl,
httpClient);

if (error != ForwarderError.None)
{
httpContext.Response.StatusCode = StatusCodes.Status502BadGateway;
await httpContext.Response.WriteAsync("An error occurred while forwarding the request.");
return Results.StatusCode(StatusCodes.Status502BadGateway);
}

return null!;
}
}
}
3 changes: 1 addition & 2 deletions src/BeeTurbo/Handlers/IBzzHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ namespace Etherna.BeeTurbo.Handlers
[SuppressMessage("Design", "CA1054:URI-like parameters should not be strings")]
public interface IBzzHandler
{
Task<IResult> HandleAsync(HttpContext httpContext, string address,
string beeUrl);
Task<IResult> HandleAsync(HttpContext httpContext, string address);
}
}
24 changes: 24 additions & 0 deletions src/BeeTurbo/Handlers/IChunksHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright 2024-present Etherna SA
// This file is part of BeeTurbo.
//
// BeeTurbo is free software: you can redistribute it and/or modify it under the terms of the
// GNU Affero General Public License as published by the Free Software Foundation,
// either version 3 of the License, or (at your option) any later version.
//
// BeeTurbo is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
// without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// See the GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License along with BeeTurbo.
// If not, see <https://www.gnu.org/licenses/>.

using Microsoft.AspNetCore.Http;
using System.Threading.Tasks;

namespace Etherna.BeeTurbo.Handlers
{
public interface IChunksHandler
{
Task<IResult> HandleAsync(HttpContext httpContext, string hash);
}
}
24 changes: 24 additions & 0 deletions src/BeeTurbo/Options/ForwarderOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright 2024-present Etherna SA
// This file is part of BeeTurbo.
//
// BeeTurbo is free software: you can redistribute it and/or modify it under the terms of the
// GNU Affero General Public License as published by the Free Software Foundation,
// either version 3 of the License, or (at your option) any later version.
//
// BeeTurbo is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
// without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// See the GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License along with BeeTurbo.
// If not, see <https://www.gnu.org/licenses/>.

using System.Diagnostics.CodeAnalysis;

namespace Etherna.BeeTurbo.Options
{
public class ForwarderOptions
{
[SuppressMessage("Design", "CA1056:URI-like properties should not be strings")]
public string BeeUrl { get; set; } = default!;
}
}
20 changes: 16 additions & 4 deletions src/BeeTurbo/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@
// If not, see <https://www.gnu.org/licenses/>.

using Etherna.BeeNet;
using Etherna.BeeNet.Hashing.Store;
using Etherna.BeeTurbo.Domain;
using Etherna.BeeTurbo.Handlers;
using Etherna.BeeTurbo.Options;
using Etherna.BeeTurbo.Persistence;
using Etherna.BeeTurbo.Tools;
using Etherna.MongODM;
Expand Down Expand Up @@ -116,8 +118,15 @@ private static void ConfigureServices(WebApplicationBuilder builder, string beeU
services.AddHttpForwarder();

// Add request handlers.
services.AddSingleton<IBzzHandler, BzzHandler>();
services.AddSingleton<IStreamTurboHandler, StreamTurboHandler>();
services.AddScoped<IBzzHandler, BzzHandler>();
services.AddScoped<IChunksHandler, ChunksHandler>();
services.AddScoped<IStreamTurboHandler, StreamTurboHandler>();

// Configure options.
services.Configure<ForwarderOptions>(options =>
{
options.BeeUrl = beeUrl;
});

// Configure persistence.
services.AddMongODMWithHangfire(configureHangfireOptions: options =>
Expand All @@ -143,7 +152,7 @@ private static void ConfigureServices(WebApplicationBuilder builder, string beeU
// Singleton services.
services.AddSingleton<IBeeClient>(_ => new BeeClient(new Uri(beeUrl, UriKind.Absolute)));
services.AddSingleton<IChunkStreamTurboProcessor, ChunkStreamTurboProcessor>();
services.AddSingleton<DbRepositoryChunkStore>();
services.AddSingleton<IChunkStore, DbChunkStore>();
}

[SuppressMessage("Reliability", "CA2000:Dispose objects before losing scope")]
Expand All @@ -157,7 +166,10 @@ private static void ConfigureApplication(WebApplication app, string beeUrl)

// Configure endpoint mapping
app.Map("/bzz/{*address}", (HttpContext httpContext, string address, IBzzHandler handler) =>
handler.HandleAsync(httpContext, address, beeUrl));
handler.HandleAsync(httpContext, address));

app.Map("/chunks/{*hash}", (HttpContext httpContext, string hash, IChunksHandler handler) =>
handler.HandleAsync(httpContext, hash));

app.Map("/chunks/stream-turbo", (HttpContext httpContext, IStreamTurboHandler handler) =>
handler.HandleAsync(httpContext));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
namespace Etherna.BeeTurbo.Tools
{
[SuppressMessage("Design", "CA1031:Do not catch general exception types")]
internal sealed class DbRepositoryChunkStore(
internal sealed class DbChunkStore(
IChunkDbContext dbContext)
: IChunkStore
{
Expand Down

0 comments on commit 214599a

Please sign in to comment.