Skip to content

Commit

Permalink
Merge pull request #1850 from qdraw/feature/202411_portmapper_refactor
Browse files Browse the repository at this point in the history
Portmapper
  • Loading branch information
qdraw authored Nov 29, 2024
2 parents dd33e8a + 32c62ee commit 8a7d6b2
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 19 deletions.
2 changes: 2 additions & 0 deletions history.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ Semantic Versioning 2.0.0 is from version 0.1.6+
- [x] (Fixed) _Back-end_ Notification duplicate error handling (Issue #1832) (PR #1834)
- [x] (Fixed) _Front-end_ Add cache headers for download publish for cloudfare (PR #1839)
- [x] (Changed) _Back-end_ Small readablity fixes to avoid code smells (PR #1844)
- [x] (Changed) _Back-end_ Filename checks for extensions (PR #1849)
- [x] (Changed) _Back-end_ Improve tests for port mapper (PR #1850)

## version 0.6.3 - 2024-11-14 {#v0.6.3}

Expand Down
15 changes: 8 additions & 7 deletions starsky/starsky.project.web/Helpers/PortProgramHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,30 +12,31 @@ namespace starsky.project.web.Helpers;

public static class PortProgramHelper
{
public static async Task SetEnvPortAspNetUrlsAndSetDefault(string[] args,
public static async Task<bool> SetEnvPortAspNetUrlsAndSetDefault(string[] args,
string appSettingsPath)
{
if ( await SkipForAppSettingsJsonFile(appSettingsPath) )
{
return;
return true;
}

SetEnvPortAspNetUrls(args);
SetDefaultAspNetCoreUrls(args);
return false;
}

internal static async Task<bool> SkipForAppSettingsJsonFile(string appSettingsPath)
{
var appContainer = await ReadAppSettings.Read(appSettingsPath);
if ( appContainer?.Kestrel?.Endpoints?.Http?.Url == null &&
appContainer?.Kestrel?.Endpoints?.Https?.Url == null )
appContainer?.Kestrel?.Endpoints?.Https?.Url == null )
{
return false;
}

Console.WriteLine("Kestrel Endpoints are set in appsettings.json, " +
"this results in skip setting the PORT and default " +
"ASPNETCORE_URLS environment variable");
"this results in skip setting the PORT and default " +
"ASPNETCORE_URLS environment variable");
return true;
}

Expand All @@ -45,7 +46,7 @@ internal static void SetEnvPortAspNetUrls(IEnumerable<string> args)
var portString = Environment.GetEnvironmentVariable("PORT");

if ( args.Contains("--urls") || string.IsNullOrEmpty(portString)
|| !int.TryParse(portString, out var port) )
|| !int.TryParse(portString, out var port) )
{
return;
}
Expand All @@ -57,7 +58,7 @@ internal static void SetEnvPortAspNetUrls(IEnumerable<string> args)
private static void SetEnvironmentVariableForPort(int port)
{
Console.WriteLine($"Set port from environment variable: {port} " +
$"\nPro tip: Its recommended to use a https proxy like nginx or traefik");
$"\nPro tip: Its recommended to use a https proxy like nginx or traefik");
Environment.SetEnvironmentVariable("ASPNETCORE_URLS", $"http://*:{port}");
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
namespace starskytest.starsky.project.web.Helpers.Models;

public class AppSettingsExampleModel
{
public class KestrelGlobalConfig
{
public KestrelConfig? Kestrel { get; set; }
}

public class KestrelConfig
{
public ExampleEndpoints? Endpoints { get; set; }
}

public class ExampleEndpoints
{
public EndpointObject? Https { get; set; }
public EndpointObject? Http { get; set; }
}

public class EndpointObject
{
public string? Url { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,39 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Text.Json;
using System.Threading.Tasks;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using starsky.foundation.platform.Models;
using starsky.foundation.storage.Helpers;
using starsky.foundation.storage.Storage;
using starsky.project.web.Helpers;
using starskytest.FakeMocks;
using starskytest.starsky.project.web.Helpers.Models;

namespace starskytest.starsky.project.web.Helpers;

[TestClass]
public class PortProgramHelperTest
{
private readonly AppSettingsExampleModel.KestrelGlobalConfig _kestrelConfig = new()
{
Kestrel = new AppSettingsExampleModel.KestrelConfig
{
Endpoints = new AppSettingsExampleModel.ExampleEndpoints
{
Http = new AppSettingsExampleModel.EndpointObject
{
Url = "http://*:8000"
},
Https = new AppSettingsExampleModel.EndpointObject
{
Url = "https://*:8001"
}
}
}
};

private readonly string? _preAspNetUrls;
private readonly string? _prePort;

Expand Down Expand Up @@ -72,17 +92,17 @@ public async Task SetEnvPortAspNetUrlsAndSetDefault_ShouldIgnore_DueAppSettingsF

var appSettingsPath =
Path.Combine(new AppSettings().BaseDirectoryProject, "appsettings-222.json");

var stream = StringToStreamHelper.StringToStream(
"{ \"Kestrel\": {\n \"Endpoints\": {\n " +
" \"Https\": {\n \"Url\": \"https://*:8001\"\n },\n \"Http\": {\n " +
" \"Url\": \"http://*:8000\"\n }\n }\n }\n }");
JsonSerializer.Serialize(_kestrelConfig));

await new StorageHostFullPathFilesystem(new FakeIWebLogger()).WriteStreamAsync(stream,
appSettingsPath);

await PortProgramHelper.SetEnvPortAspNetUrlsAndSetDefault(Array.Empty<string>(),
var result = await PortProgramHelper.SetEnvPortAspNetUrlsAndSetDefault([],
appSettingsPath);

Assert.AreEqual(null, Environment.GetEnvironmentVariable("ASPNETCORE_URLS"));
Assert.IsTrue(result);

Environment.SetEnvironmentVariable("PORT", _prePort);
Environment.SetEnvironmentVariable("ASPNETCORE_URLS", _preAspNetUrls);
Expand All @@ -100,10 +120,10 @@ public async Task SkipForAppSettingsJsonFile_ShouldIgnore_DueAppSettingsFile()

var appSettingsPath =
Path.Combine(new AppSettings().BaseDirectoryProject, "appsettings-111.json");

var stream = StringToStreamHelper.StringToStream(
"{ \"Kestrel\": {\n \"Endpoints\": {\n " +
" \"Https\": {\n \"Url\": \"https://*:8001\"\n },\n \"Http\": {\n " +
" \"Url\": \"http://*:8000\"\n }\n }\n }\n }");
JsonSerializer.Serialize(_kestrelConfig));

await new StorageHostFullPathFilesystem(new FakeIWebLogger()).WriteStreamAsync(stream,
appSettingsPath);

Expand All @@ -127,10 +147,24 @@ public async Task SkipForAppSettingsJsonFile_ShouldIgnore_DueAppSettingsFile2()

var appSettingsPath =
Path.Combine(new AppSettings().BaseDirectoryProject, "appsettings-333.json");
var stream = StringToStreamHelper.StringToStream(
"{ \"Kestrel\": {\n \"Endpoints\": {\n " +
" \"Https\": {\n \"Url\": \"https://*:8001\"\n }\n " +
"\n }\n }\n }");

var kestrelConfig = new AppSettingsExampleModel.KestrelGlobalConfig
{
Kestrel = new AppSettingsExampleModel.KestrelConfig
{
Endpoints = new AppSettingsExampleModel.ExampleEndpoints
{
Https = new AppSettingsExampleModel.EndpointObject
{
Url = "https://*:8001"
}
}
}
};

var jsonString = JsonSerializer.Serialize(kestrelConfig);
var stream = StringToStreamHelper.StringToStream(jsonString);

await new StorageHostFullPathFilesystem(new FakeIWebLogger()).WriteStreamAsync(stream,
appSettingsPath);

Expand Down

0 comments on commit 8a7d6b2

Please sign in to comment.