bool InitializeServer()
{
bool result = true;
try
{
string webServerPort = null;
string webServerSslPort = null;
Dictionary<string, string> arguments = new Dictionary<string, string>();
if(_arguments.Any())
{
foreach(string t in _arguments)
{
string[] arg = t.Split(new[] { '=' });
if(arg.Length == 2)
{
arguments.Add(arg[0].Replace("/", string.Empty), arg[1]);
}
}
}
foreach(KeyValuePair<string, string> argument in arguments)
{
if(argument.Key.Equals("endpointAddress", StringComparison.InvariantCultureIgnoreCase))
{
continue;
}
if(argument.Key.Equals("webServerPort", StringComparison.InvariantCultureIgnoreCase))
{
webServerPort = argument.Value;
continue;
}
if(argument.Key.Equals("webServerSslPort", StringComparison.InvariantCultureIgnoreCase))
{
webServerSslPort = argument.Value;
continue;
}
if(argument.Key.Equals("lifecycleConfigFile", StringComparison.InvariantCultureIgnoreCase))
{
_configFile = argument.Value;
}
}
GlobalConstants.WebServerPort = webServerPort = webServerPort ?? ConfigurationManager.AppSettings["webServerPort"];
GlobalConstants.WebServerSslPort = webServerSslPort = webServerSslPort ?? ConfigurationManager.AppSettings["webServerSslPort"];
_isWebServerEnabled = false;
Boolean.TryParse(ConfigurationManager.AppSettings["webServerEnabled"], out _isWebServerEnabled);
Boolean.TryParse(ConfigurationManager.AppSettings["webServerSslEnabled"], out _isWebServerSslEnabled);
if(_isWebServerEnabled)
{
if(string.IsNullOrEmpty(webServerPort) && _isWebServerEnabled)
{
throw new ArgumentException(
"Web server port not set but web server is enabled. Please set the webServerPort value in the configuration file.");
}
int realPort;
if(!Int32.TryParse(webServerPort, out realPort))
{
throw new ArgumentException("Web server port is not valid. Please set the webServerPort value in the configuration file.");
}
var endpoints = new List<Dev2Endpoint>();
var httpEndpoint = new IPEndPoint(IPAddress.Any, realPort);
var httpUrl = string.Format("http://*:{0}/", webServerPort);
endpoints.Add(new Dev2Endpoint(httpEndpoint, httpUrl));
EnvironmentVariables.WebServerUri = httpUrl.Replace("*", Environment.MachineName);
// start SSL traffic if it is enabled ;)
if(!string.IsNullOrEmpty(webServerSslPort) && _isWebServerSslEnabled)
{
int realWebServerSslPort;
Int32.TryParse(webServerSslPort, out realWebServerSslPort);
var sslCertPath = ConfigurationManager.AppSettings["sslCertificateName"];
if(!string.IsNullOrEmpty(sslCertPath))
{
var httpsEndpoint = new IPEndPoint(IPAddress.Any, realWebServerSslPort);
var httpsUrl = string.Format("https://*:{0}/", webServerSslPort);
var canEnableSsl = HostSecurityProvider.Instance.EnsureSsl(sslCertPath, httpsEndpoint);
if(canEnableSsl)
{
endpoints.Add(new Dev2Endpoint(httpsEndpoint, httpsUrl, sslCertPath));
}
else
{
WriteLine("Could not start webserver to listen for SSL traffic with cert [ " + sslCertPath + " ]");
}
}
}
_endpoints = endpoints.ToArray();
}
}
catch(Exception ex)
{
result = false;
Fail("Server initialization failed", ex);
}
return result;
}