private IWebHost startHost(int port, WebSocketsHandler webSockets, TopicMiddleware middleware)
{
var host = new WebHostBuilder()
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseUrls($"http://localhost:{port}")
.Configure(app =>
{
app.UseWebSockets();
app.Use(async (http, next) =>
{
if (http.WebSockets.IsWebSocketRequest)
await webSockets.HandleSocket(http).ConfigureAwait(false);
else
await next().ConfigureAwait(false);
});
app.UseStaticFiles(new StaticFileOptions
{
ServeUnknownFileTypes = true,
FileProvider = new PhysicalFileProvider(_settings.Root)
});
app.Use(async (http, next) =>
{
if (http.Request.Method.EqualsIgnoreCase("POST"))
switch (http.Request.Path)
{
case "/refresh":
Console.WriteLine("Hey, I got the order to do the hard refresh!");
await HardRefresh().ConfigureAwait(false);
_refresher.RefreshPage();
break;
case "/open":
var url = new Uri(http.Request.Headers["referer"]);
Console.WriteLine("Open requested to " + url);
var topic = FindTopicByUrl(url.AbsolutePath.TrimStart('/'));
if (topic != null)
{
try {
var start = new ProcessStartInfo();
start.UseShellExecute = true;
start.FileName = "open";
start.Arguments = topic.File;
Process.Start(start);
}
catch (Exception ex){
Process.Start(topic.File);
}
}
break;
}
else
await next().ConfigureAwait(false);
});
app.Run(middleware.Invoke);
})
.Build();
host.Start();
return host;
}