Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat(config): Add custom config for web UI URLs/port. #1462

Merged
merged 2 commits into from
May 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Dockerfile.linux
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ VOLUME ["/smtp4dev"]
WORKDIR /
ENV XDG_CONFIG_HOME /
ENV ASPNETCORE_HTTP_PORTS 80
ENV SERVEROPTIONS__URLS http://*:80
EXPOSE 80
EXPOSE 25
EXPOSE 143
Expand Down
1 change: 1 addition & 0 deletions Dockerfile.linux.arm64
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ VOLUME ["/smtp4dev"]
WORKDIR /
ENV XDG_CONFIG_HOME /
ENV ASPNETCORE_HTTP_PORTS 80
ENV SERVEROPTIONS__URLS http://*:80
EXPOSE 80
EXPOSE 25
EXPOSE 143
Expand Down
1 change: 1 addition & 0 deletions Dockerfile.windows
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ EXPOSE 80
EXPOSE 25
EXPOSE 143
ENV ASPNETCORE_HTTP_PORTS 80
ENV SERVEROPTIONS__URLS http://*:80
VOLUME c:\smtp4dev
WORKDIR c:\
ENTRYPOINT ["/app/Rnwood.Smtp4dev.exe"]
33 changes: 19 additions & 14 deletions Rnwood.Smtp4dev/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,6 @@ private static string GetContentRoot()

private static IHost BuildWebHost(string[] args, CommandLineOptions cmdLineOptions, MapOptions<CommandLineOptions> commandLineOptions)
{


var contentRoot = GetContentRoot();
var dataDir = GetOrCreateDataDir(cmdLineOptions);
_log.Information("DataDir: {dataDir}", dataDir);
Expand All @@ -155,9 +153,9 @@ private static IHost BuildWebHost(string[] args, CommandLineOptions cmdLineOptio
.ConfigureAppConfiguration(
(hostingContext, configBuilder) =>
{
IHostEnvironment env = hostingContext.HostingEnvironment;
var env = hostingContext.HostingEnvironment;

IConfigurationBuilder cb = configBuilder
var cb = configBuilder
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);

Expand All @@ -168,20 +166,20 @@ private static IHost BuildWebHost(string[] args, CommandLineOptions cmdLineOptio
cb = cb.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true);
cb = cb.AddJsonFile(Path.Join(dataDir, "appsettings.json"), optional: true, reloadOnChange: true);


_log.Information("User settings file: {file}", Path.Join(dataDir, "appsettings.json"));
}

cb.AddEnvironmentVariables()
.AddCommandLineOptions(commandLineOptions);

IConfigurationRoot config = cb
var config = cb
.Build();

hostingContext.HostingEnvironment.EnvironmentName = config["Environment"];

if (cmdLineOptions.DebugSettings)
{

Console.WriteLine(JsonSerializer.Serialize(new SettingsDebugInfo
{
CmdLineArgs = Environment.GetCommandLineArgs(),
Expand All @@ -192,26 +190,33 @@ private static IHost BuildWebHost(string[] args, CommandLineOptions cmdLineOptio
}, SettingsDebugInfoSerializationContext.Default.SettingsDebugInfo));
}


});

builder.UseWindowsService(s => s.ServiceName = "smtp4dev");

builder.ConfigureWebHostDefaults(c =>
{
c.UseStartup<Startup>();
c.UseShutdownTimeout(TimeSpan.FromSeconds(10));

if (!string.IsNullOrEmpty(cmdLineOptions.Urls))
{
c.UseUrls(cmdLineOptions.Urls.Split(';', StringSplitOptions.RemoveEmptyEntries).Select(u => u.Trim()).ToArray());
}
c.ConfigureServices(services =>
c.ConfigureServices((webBuilderContext, services) =>
{
ServerOptions serverOptions = webBuilderContext.Configuration.GetSection("ServerOptions").Get<ServerOptions>();

if (!string.IsNullOrEmpty(serverOptions.Urls))
{
c.UseUrls(serverOptions.Urls.Split(';', StringSplitOptions.RemoveEmptyEntries).Select(u => u.Trim()).ToArray());
}

services.AddSingleton(cmdLineOptions);
services.AddHostedService<Smtp4devServer>(sp => (Smtp4devServer)sp.GetRequiredService<ISmtp4devServer>());
services.AddHostedService<ImapServer>(sp => sp.GetRequiredService<ImapServer>());
services.AddHostedService(sp => (Smtp4devServer)sp.GetRequiredService<ISmtp4devServer>());
services.AddHostedService(sp => sp.GetRequiredService<ImapServer>());
});
});

builder.UseWindowsService(s => s.ServiceName = "smtp4dev");




return builder.Build();
Expand Down
3 changes: 2 additions & 1 deletion Rnwood.Smtp4dev/Properties/launchSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"commandLineArgs": "--db=database2.db",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
"ASPNETCORE_ENVIRONMENT": "Development",
"SERVEROPTIONS__URLS": "http://*:1234"
},
"applicationUrl": "http://localhost:5000/"
}
Expand Down
2 changes: 2 additions & 0 deletions Rnwood.Smtp4dev/Server/Settings/ServerOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ namespace Rnwood.Smtp4dev.Server.Settings
{
public record ServerOptions
{
public string Urls { get; set; }

public int Port { get; set; } = 25;
public bool AllowRemoteConnections { get; set; } = true;

Expand Down
2 changes: 2 additions & 0 deletions Rnwood.Smtp4dev/Server/Settings/ServerOptionsSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ namespace Rnwood.Smtp4dev.Server.Settings
{
public record ServerOptionsSource
{
public string Urls { get; set; }

private int? port;

public int? Port { get => port; set => port = value.HasValue ? Guard.Against.OutOfRange(value.Value, nameof(value), 0, 65535) : null; }
Expand Down
6 changes: 6 additions & 0 deletions Rnwood.Smtp4dev/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@
// Default value: false
"LockSettings": false,

// The URLs the web interface should listen on.
// For example, http://localhost:123.
// Use `*` in place of hostname to listen for requests on any IP address or hostname using the specified port and protocol (for example, http://*:5000).
// Separate multiple values with ;
"Urls": "http://localhost:5000",

// Specifies the virtual path from web server root where SMTP4DEV web interface will be hosted. e.g. "/" or "/smtp4dev"
// Default value: "/"
"BasePath": "/",
Expand Down
3 changes: 3 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ services:
#Specifies the virtual path from web server root where SMTP4DEV web interface will be hosted. e.g. "/" or "/smtp4dev"
#- ServerOptions__BasePath=/smtp4dev

#Specifies the URLs the web UI will use inside the container.
- ServerOptions__Urls=http://*:80

#Specifies the server hostname. Used in auto-generated TLS certificate if enabled.
- ServerOptions__HostName=smtp4dev

Expand Down