17
17
using Microsoft . EntityFrameworkCore . Metadata . Internal ;
18
18
using DeepEqual . Syntax ;
19
19
using System . Text . Json . Serialization . Metadata ;
20
+ using Microsoft . Extensions . Configuration . EnvironmentVariables ;
21
+ using Microsoft . Extensions . Configuration ;
22
+ using CommandLiners ;
20
23
21
24
namespace Rnwood . Smtp4dev . Controllers
22
25
{
@@ -25,7 +28,7 @@ namespace Rnwood.Smtp4dev.Controllers
25
28
public class ServerController : Controller
26
29
{
27
30
public ServerController ( ISmtp4devServer server , ImapServer imapServer , IOptionsMonitor < ServerOptions > serverOptions ,
28
- IOptionsMonitor < RelayOptions > relayOptions , IOptionsMonitor < DesktopOptions > desktopOptions , CommandLineOptions cmdLineOptions , IHostingEnvironmentHelper hostingEnvironmentHelper )
31
+ IOptionsMonitor < RelayOptions > relayOptions , IOptionsMonitor < DesktopOptions > desktopOptions , MapOptions < CommandLineOptions > commandLineOptions , CommandLineOptions cmdLineOptions , IHostingEnvironmentHelper hostingEnvironmentHelper )
29
32
{
30
33
this . server = server ;
31
34
this . imapServer = imapServer ;
@@ -34,6 +37,7 @@ public ServerController(ISmtp4devServer server, ImapServer imapServer, IOptionsM
34
37
this . desktopOptions = desktopOptions ;
35
38
this . hostingEnvironmentHelper = hostingEnvironmentHelper ;
36
39
this . cmdLineOptions = cmdLineOptions ;
40
+ this . commandLineOptions = commandLineOptions ;
37
41
}
38
42
39
43
private readonly ISmtp4devServer server ;
@@ -43,6 +47,7 @@ public ServerController(ISmtp4devServer server, ImapServer imapServer, IOptionsM
43
47
private readonly IOptionsMonitor < DesktopOptions > desktopOptions ;
44
48
private readonly IHostingEnvironmentHelper hostingEnvironmentHelper ;
45
49
private readonly CommandLineOptions cmdLineOptions ;
50
+ private readonly MapOptions < CommandLineOptions > commandLineOptions ;
46
51
47
52
/// <summary>
48
53
/// Gets the current state and settings for the smtp4dev server.
@@ -124,34 +129,56 @@ private IDictionary<string, string> GetLockedSettings(bool toJsonCasing)
124
129
}
125
130
else
126
131
{
127
- if ( this . hostingEnvironmentHelper . IsRunningInContainer ( ) )
132
+ var fixedConfigSources = new ConfigurationBuilder ( ) . AddEnvironmentVariables ( )
133
+ . AddCommandLineOptions ( commandLineOptions ) . Build ( ) ;
134
+
135
+ var fixedConfig = new SettingsFile
128
136
{
129
- lockedSettings . Add ( nameof ( ApiModel . Server . Port ) , "Running in a container. Change this port mapping in the container host." ) ;
130
- lockedSettings . Add ( nameof ( ApiModel . Server . ImapPort ) , "Running in a container. Change this port mapping in the container host." ) ;
131
- lockedSettings . Add ( nameof ( ApiModel . Server . AllowRemoteConnections ) , "Running in a container. Change this port mapping in the container host." ) ;
137
+ ServerOptions = fixedConfigSources . GetSection ( "ServerOptions" ) . Get < ServerOptionsSource > ( ) ,
138
+ DesktopOptions = fixedConfigSources . GetSection ( "DesktopOptions" ) . Get < DesktopOptionsSource > ( ) ,
139
+ RelayOptions = fixedConfigSources . GetSection ( "RelayOptions" ) . Get < RelayOptionsSource > ( )
140
+ } ;
141
+
142
+ if ( fixedConfig . ServerOptions != null )
143
+ {
144
+ foreach ( var p in fixedConfig . ServerOptions . GetType ( ) . GetProperties ( System . Reflection . BindingFlags . Instance | System . Reflection . BindingFlags . Public ) )
145
+ {
146
+ if ( p . GetValue ( fixedConfig . ServerOptions ) != null )
147
+ {
148
+ lockedSettings [ p . Name ] = "Specified using command line option or environment variable" ;
149
+ }
150
+ }
132
151
}
133
152
134
- if ( cmdLineOptions . ServerOptions != null )
153
+ if ( fixedConfig . RelayOptions != null )
135
154
{
136
- foreach ( var p in cmdLineOptions . ServerOptions . GetType ( ) . GetProperties ( System . Reflection . BindingFlags . Instance | System . Reflection . BindingFlags . Public ) )
155
+ foreach ( var p in fixedConfig . RelayOptions . GetType ( ) . GetProperties ( System . Reflection . BindingFlags . Instance | System . Reflection . BindingFlags . Public ) )
137
156
{
138
- if ( p . GetValue ( cmdLineOptions . ServerOptions ) != null )
157
+ if ( p . GetValue ( fixedConfig . RelayOptions ) != null )
139
158
{
140
- lockedSettings [ p . Name ] = "Specified using command line option" ;
159
+ lockedSettings [ "Relay" + p . Name ] = "Specified using command line option or environment variable " ;
141
160
}
142
161
}
143
162
}
144
163
145
- if ( cmdLineOptions . RelayOptions != null )
164
+ if ( fixedConfig . DesktopOptions != null )
146
165
{
147
- foreach ( var p in cmdLineOptions . RelayOptions . GetType ( ) . GetProperties ( System . Reflection . BindingFlags . Instance | System . Reflection . BindingFlags . Public ) )
166
+ foreach ( var p in fixedConfig . DesktopOptions . GetType ( ) . GetProperties ( System . Reflection . BindingFlags . Instance | System . Reflection . BindingFlags . Public ) )
148
167
{
149
- if ( p . GetValue ( cmdLineOptions . RelayOptions ) != null )
168
+ if ( p . GetValue ( fixedConfig . DesktopOptions ) != null )
150
169
{
151
- lockedSettings [ "Relay " + p . Name ] = "Specified using command line option" ;
170
+ lockedSettings [ "Desktop " + p . Name ] = "Specified using command line option or environment variable " ;
152
171
}
153
172
}
154
173
}
174
+
175
+
176
+ if ( this . hostingEnvironmentHelper . IsRunningInContainer ( ) )
177
+ {
178
+ lockedSettings . Add ( nameof ( ApiModel . Server . Port ) , "Running in a container. Change this port mapping in the container host." ) ;
179
+ lockedSettings . Add ( nameof ( ApiModel . Server . ImapPort ) , "Running in a container. Change this port mapping in the container host." ) ;
180
+ lockedSettings . Add ( nameof ( ApiModel . Server . AllowRemoteConnections ) , "Running in a container. Change this port mapping in the container host." ) ;
181
+ }
155
182
}
156
183
157
184
if ( toJsonCasing )
0 commit comments