Skip to content

Commit

Permalink
Add web api extension method sample
Browse files Browse the repository at this point in the history
  • Loading branch information
cristipufu committed Oct 10, 2024
1 parent 8c38589 commit 4f7f1b9
Show file tree
Hide file tree
Showing 8 changed files with 168 additions and 4 deletions.
13 changes: 10 additions & 3 deletions Tunnelite.sln
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{AFD8CF9B-F8B
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{230DD554-CD20-4A77-874B-E9BA59A2DC18}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Test.TcpClient", "test\Test.TcpClient\Test.TcpClient.csproj", "{6C9A59E4-2278-4CCA-8C88-59B183EBE9C1}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Test.TcpClient", "test\Test.TcpClient\Test.TcpClient.csproj", "{6C9A59E4-2278-4CCA-8C88-59B183EBE9C1}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Test.TcpServer", "test\Test.TcpServer\Test.TcpServer.csproj", "{9E529D00-D5E7-4E2E-80AD-83B793B77DF8}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Test.TcpServer", "test\Test.TcpServer\Test.TcpServer.csproj", "{9E529D00-D5E7-4E2E-80AD-83B793B77DF8}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Test.TcpForwarder", "test\Test.TcpForwarder\Test.TcpForwarder.csproj", "{BCC3AD29-688B-4482-B952-F0095D104020}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Test.TcpForwarder", "test\Test.TcpForwarder\Test.TcpForwarder.csproj", "{BCC3AD29-688B-4482-B952-F0095D104020}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Test.WebApi", "test\Test.WebApi\Test.WebApi.csproj", "{B2656A6C-79E5-41A1-93B2-F87D550FB02E}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down Expand Up @@ -43,6 +45,10 @@ Global
{BCC3AD29-688B-4482-B952-F0095D104020}.Debug|Any CPU.Build.0 = Debug|Any CPU
{BCC3AD29-688B-4482-B952-F0095D104020}.Release|Any CPU.ActiveCfg = Release|Any CPU
{BCC3AD29-688B-4482-B952-F0095D104020}.Release|Any CPU.Build.0 = Release|Any CPU
{B2656A6C-79E5-41A1-93B2-F87D550FB02E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B2656A6C-79E5-41A1-93B2-F87D550FB02E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B2656A6C-79E5-41A1-93B2-F87D550FB02E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B2656A6C-79E5-41A1-93B2-F87D550FB02E}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -53,6 +59,7 @@ Global
{6C9A59E4-2278-4CCA-8C88-59B183EBE9C1} = {230DD554-CD20-4A77-874B-E9BA59A2DC18}
{9E529D00-D5E7-4E2E-80AD-83B793B77DF8} = {230DD554-CD20-4A77-874B-E9BA59A2DC18}
{BCC3AD29-688B-4482-B952-F0095D104020} = {230DD554-CD20-4A77-874B-E9BA59A2DC18}
{B2656A6C-79E5-41A1-93B2-F87D550FB02E} = {230DD554-CD20-4A77-874B-E9BA59A2DC18}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {E385A6C3-BECA-4888-B12B-31AA9984F86F}
Expand Down
2 changes: 1 addition & 1 deletion src/Tunnelite.Client/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ private static async Task RunMainLoop(string localUrl)
}
}

private static Table WriteStatusTable(string localUrl, string? tunnelUrl, string color, string currentStatus)
public static Table WriteStatusTable(string localUrl, string? tunnelUrl, string color, string currentStatus)
{
AnsiConsole.Clear();

Expand Down
40 changes: 40 additions & 0 deletions test/Test.WebApi/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using Test.WebApi;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.

var app = builder.Build();

// Configure the HTTP request pipeline.

app.UseHttpsRedirection();

var summaries = new[]
{
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};

app.MapGet("/", () =>
{
var forecast = Enumerable.Range(1, 5).Select(index =>
new WeatherForecast
(
DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
Random.Shared.Next(-20, 55),
summaries[Random.Shared.Next(summaries.Length)]
))
.ToArray();
return forecast;
});

// Add tunnelite

app.UseTunnelite();

app.Run();

internal record WeatherForecast(DateOnly Date, int TemperatureC, string? Summary)
{
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
}
41 changes: 41 additions & 0 deletions test/Test.WebApi/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"$schema": "http://json.schemastore.org/launchsettings.json",
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:17902",
"sslPort": 44322
}
},
"profiles": {
"http": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "",
"applicationUrl": "http://localhost:5223",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"https": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "",
"applicationUrl": "https://localhost:7272;http://localhost:5223",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
13 changes: 13 additions & 0 deletions test/Test.WebApi/Test.WebApi.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\Tunnelite.Client\Tunnelite.Client.csproj" />
</ItemGroup>

</Project>
46 changes: 46 additions & 0 deletions test/Test.WebApi/TunneliteExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using Microsoft.AspNetCore.Hosting.Server;
using Microsoft.AspNetCore.Hosting.Server.Features;
using Spectre.Console;
using Tunnelite.Client.HttpTunnel;

namespace Test.WebApi
{
public static class TunneliteExtensions
{
private static readonly Guid ClientId = Guid.NewGuid();

public static IApplicationBuilder UseTunnelite(this IApplicationBuilder app)
{
var lifetime = app.ApplicationServices.GetRequiredService<IHostApplicationLifetime>();
var server = app.ApplicationServices.GetRequiredService<IServer>();
var logger = app.ApplicationServices.GetRequiredService<ILogger<IApplicationBuilder>>();

lifetime.ApplicationStarted.Register(() =>
{
var addressFeature = server.Features.Get<IServerAddressesFeature>();
var localUrl = addressFeature?.Addresses.FirstOrDefault();
if (string.IsNullOrEmpty(localUrl))
{
throw new InvalidOperationException("Unable to determine the local URL of the application.");
}
var httpTunnel = new HttpTunnelRequest
{
ClientId = ClientId,
LocalUrl = localUrl,
PublicUrl = "https://tunnelite.com",
};
var client = new HttpTunnelClient(httpTunnel, null);
client.ConnectAsync().GetAwaiter().GetResult();
Table statusTable = Tunnelite.Client.Program.WriteStatusTable(localUrl, client.TunnelUrl, "green", "Connected");
});

return app;
}
}
}
8 changes: 8 additions & 0 deletions test/Test.WebApi/appsettings.Development.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}
9 changes: 9 additions & 0 deletions test/Test.WebApi/appsettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}

0 comments on commit 4f7f1b9

Please sign in to comment.