Skip to content

Commit 78a2fe7

Browse files
author
0phios
committed
- Expose RagnarokClient on INgrokHostedService
- Add extension method to open tunnel using TunnelDetail
1 parent 1d14c03 commit 78a2fe7

File tree

3 files changed

+35
-3
lines changed

3 files changed

+35
-3
lines changed

src/Ragnarok.AgentApi/Helpers/Extensions/RagnorokClientExtensions.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,31 @@ public static async Task<TunnelDetail> ConnectAsync(this RagnarokClient client,
6767
return await ConnectAsync(client, definition.Value, authToken, cancellationToken);
6868
}
6969

70+
/// <summary>
71+
/// Start ngrok and open a tunnel
72+
/// </summary>
73+
/// <param name="client">An instance of <see cref="RagnarokClient"/></param>
74+
/// <param name="details"><see cref="TunnelDetail"/> to replicate</param>
75+
/// <param name="authToken">Authorization token to register in ngrok.yml</param>
76+
/// <param name="cancellationToken">Propagates notification that operations should be canceled</param>
77+
/// <remarks>
78+
/// Creates a new tunnel based on properties defined in the provided <see cref="TunnelDetail"/> <br/>
79+
/// <see cref="RagnarokClient.InitializeAsync"/> will be called if not previously executed
80+
/// </remarks>
81+
public static async Task<TunnelDetail> ConnectAsync(this RagnarokClient client, TunnelDetail details,
82+
string authToken = null, CancellationToken cancellationToken = default)
83+
{
84+
return await ConnectAsync(client, options =>
85+
{
86+
options.Name = details.Name;
87+
options.Protocol = details.Proto;
88+
options.Address = details.Config.Address;
89+
options.BindTLS = details.Proto == TunnelProtocol.HTTP ? BindTLS.False : BindTLS.True;
90+
},
91+
authToken: authToken,
92+
cancellationToken: cancellationToken);
93+
}
94+
7095
/// <summary>
7196
/// Start ngrok and open a tunnel
7297
/// </summary>

src/Ragnarok.HostedService/Contracts/INgrokHostedService.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
using Microsoft.Extensions.Hosting;
2+
using Ragnarok.AgentApi;
3+
using Ragnarok.HostedService.Models;
24
using System;
35

46
namespace Ragnarok.HostedService.Contracts
@@ -10,6 +12,11 @@ public interface INgrokHostedService : IHostedService
1012
/// <summary>
1113
/// Raised when all tunnels have been created
1214
/// </summary>
13-
event EventHandler Ready;
15+
event EventHandler<ReadyEventArgs> Ready;
16+
17+
/// <summary>
18+
/// An instance of <see cref="RagnarokClient"/> that provides access to the ngrok client process and the ngrok Agent Api
19+
/// </summary>
20+
public RagnarokClient RagnarokClient { get; }
1421
}
1522
}

src/Ragnarok.HostedService/NgrokHostedService.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public class NgrokHostedService : INgrokHostedService, IDisposable
2323
/// <summary>
2424
/// <inheritdoc/>
2525
/// </summary>
26-
public event EventHandler Ready;
26+
public event EventHandler<ReadyEventArgs> Ready;
2727

2828
/// <summary>
2929
/// Raises the <see cref="Ready"/> event
@@ -32,7 +32,7 @@ public class NgrokHostedService : INgrokHostedService, IDisposable
3232
protected virtual void OnReady(IEnumerable<TunnelDetail> tunnels) => Ready?.Invoke(this, new ReadyEventArgs() { Tunnels = tunnels });
3333

3434
/// <summary>
35-
/// An instance of <see cref="RagnarokClient"/> that provides access to the ngrok client process and the ngrok Agent Api
35+
/// <inheritdoc/>
3636
/// </summary>
3737
public RagnarokClient RagnarokClient { get; }
3838

0 commit comments

Comments
 (0)