private void startWebServer(int port, WebSocketsHandler webSockets)
{
var baseDirectory = AppContext.BaseDirectory;
var host = new WebHostBuilder()
.UseKestrel()
.UseContentRoot(baseDirectory)
.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);
});
#if DEBUG
configureStaticFiles(app);
#endif
app.Run(async http =>
{
if (http.Request.Path == "/favicon.ico")
{
var stream =
GetType()
.GetTypeInfo()
.Assembly.GetManifestResourceStream("StorytellerRunner.favicon.ico");
http.Response.ContentType = "image/x-icon";
await stream.CopyToAsync(http.Response.Body).ConfigureAwait(false);
return;
}
try
{
string html;
if (http.Request.Path.HasValue && http.Request.Path.Value == "/preview")
{
html = ExportWriter.BuildPage(_application);
}
else
{
html = HomeEndpoint.BuildPage(_application, _input).ToString();
}
http.Response.ContentType = "text/html";
await http.Response.WriteAsync(html).ConfigureAwait(false);
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
throw;
}
});
});
_server = host.Start();
}