Skip to content
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
12 changes: 12 additions & 0 deletions src/clients/Wyam/App.config
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,18 @@
<assemblyIdentity name="System.Runtime.Extensions" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.1.1.0" newVersion="4.1.1.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Reflection" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.1.1.0" newVersion="4.1.1.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Diagnostics.Tracing" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.1.1.0" newVersion="4.1.1.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Runtime.InteropServices" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.1.0.0" newVersion="4.1.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
102 changes: 46 additions & 56 deletions src/clients/Wyam/PreviewServer.cs
Original file line number Diff line number Diff line change
@@ -1,73 +1,28 @@
using System;
using System.Collections.Generic;
using System.IO;

using Microsoft.Owin;
using Microsoft.Owin.FileSystems;
using Microsoft.Owin.Hosting;
using Microsoft.Owin.Hosting.Tracing;
using Microsoft.Owin.StaticFiles;

using Owin;

using Wyam.Common.IO;
using Wyam.Common.Tracing;
using Wyam.Owin;
using Wyam.Server;

namespace Wyam
{
internal static class PreviewServer
{
public static IDisposable Start(DirectoryPath path, int port, bool forceExtension, DirectoryPath virtualDirectory)
{
IDisposable server;
HttpServer server;
try
{
StartOptions options = new StartOptions("http://localhost:" + port);

// Disable built-in owin tracing by using a null trace output
// http://stackoverflow.com/questions/17948363/tracelistener-in-owin-self-hosting
options.Settings.Add(typeof(ITraceOutputFactory).FullName, typeof(NullTraceOutputFactory).AssemblyQualifiedName);

server = WebApp.Start(options, app =>
{
Microsoft.Owin.FileSystems.IFileSystem outputFolder = new PhysicalFileSystem(path.FullPath);

// Support for virtual directory
if (virtualDirectory != null)
{
app.UseVirtualDirectory(virtualDirectory.FullPath);
}

// Disable caching
app.Use((c, t) =>
{
c.Response.Headers.Append("Cache-Control", "no-cache, no-store, must-revalidate");
c.Response.Headers.Append("Pragma", "no-cache");
c.Response.Headers.Append("Expires", "0");
return t();
});

// Support for extensionless URLs
if (!forceExtension)
{
app.UseExtensionlessUrls(new ExtensionlessUrlsOptions
{
FileSystem = outputFolder
});
}

// Serve up all static files
app.UseDefaultFiles(new DefaultFilesOptions
{
RequestPath = PathString.Empty,
FileSystem = outputFolder,
DefaultFileNames = new List<string> { "index.html", "index.htm", "home.html", "home.htm", "default.html", "default.html" }
});
app.UseStaticFiles(new StaticFileOptions
{
RequestPath = PathString.Empty,
FileSystem = outputFolder,
ServeUnknownFileTypes = true
});
});
server = new HttpServer();
server.StartServer(port, app => ConfigureOwin(app, path, forceExtension, virtualDirectory));
}
catch (Exception ex)
{
Expand All @@ -76,16 +31,51 @@ public static IDisposable Start(DirectoryPath path, int port, bool forceExtensio
}

Trace.Information($"Preview server listening on port {port} and serving from path {path}"
+ (virtualDirectory == null ? string.Empty : $" with virtual directory {virtualDirectory.FullPath}"));
+ (virtualDirectory == null ? string.Empty : $" with virtual directory {virtualDirectory.FullPath}"));
return server;
}

private class NullTraceOutputFactory : ITraceOutputFactory
internal static void ConfigureOwin(IAppBuilder app, DirectoryPath path, bool forceExtension, DirectoryPath virtualDirectory)
{
public TextWriter Create(string outputFile)
Microsoft.Owin.FileSystems.IFileSystem outputFolder = new PhysicalFileSystem(path.FullPath);

// Support for virtual directory
if (virtualDirectory != null)
{
return StreamWriter.Null;
app.UseVirtualDirectory(virtualDirectory.FullPath);
}

// Disable caching
app.Use((c, t) =>
{
c.Response.Headers.Append("Cache-Control", "no-cache, no-store, must-revalidate");
c.Response.Headers.Append("Pragma", "no-cache");
c.Response.Headers.Append("Expires", "0");
return t();
});

// Support for extensionless URLs
if (!forceExtension)
{
app.UseExtensionlessUrls(new ExtensionlessUrlsOptions
{
FileSystem = outputFolder
});
}

// Serve up all static files
app.UseDefaultFiles(new DefaultFilesOptions
{
RequestPath = PathString.Empty,
FileSystem = outputFolder,
DefaultFileNames = new List<string> {"index.html", "index.htm", "home.html", "home.htm", "default.html", "default.html"}
});
app.UseStaticFiles(new StaticFileOptions
{
RequestPath = PathString.Empty,
FileSystem = outputFolder,
ServeUnknownFileTypes = true
});
}
}
}
4 changes: 1 addition & 3 deletions src/clients/Wyam/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
using System.Threading;
using Microsoft.Owin;
using Microsoft.Owin.FileSystems;
using Microsoft.Owin.Hosting;
using Microsoft.Owin.Hosting.Tracing;
using Microsoft.Owin.StaticFiles;
using Owin;
using Wyam.Commands;
Expand Down Expand Up @@ -45,7 +43,7 @@ private static void UnhandledExceptionEvent(object sender, UnhandledExceptionEve
private int Run(string[] args)
{
// Add a default trace listener
Trace.AddListener(new SimpleColorConsoleTraceListener { TraceOutputOptions = System.Diagnostics.TraceOptions.None });
Trace.AddListener(new SimpleColorConsoleTraceListener { TraceOutputOptions = TraceOptions.None });

// Output version info
Trace.Information($"Wyam version {Engine.Version}");
Expand Down
36 changes: 36 additions & 0 deletions src/clients/Wyam/Server/ApplicationBuilderExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

using Microsoft.AspNetCore.Builder;
using Microsoft.Owin.Builder;

using Owin;

namespace Wyam.Server
{
internal static class ApplicationBuilderExtensions
{
// http://stackoverflow.com/a/30742029/2001966

public static void UseOwinBuilder(this IApplicationBuilder app, Action<IAppBuilder> owinConfiguration)
{
app.UseOwin(
addToPipeline =>
{
addToPipeline(
next =>
{
AppBuilder builder = new AppBuilder();
owinConfiguration(builder);
builder.Run(ctx => next(ctx.Environment));

// ReSharper disable once SuggestVarOrType_Elsewhere
var appFunc = (Func<IDictionary<string, object>, Task>) builder.Build(typeof(Func<IDictionary<string, object>, Task>));

return appFunc;
});
});
}
}
}
36 changes: 36 additions & 0 deletions src/clients/Wyam/Server/HttpServer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System;

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;

using Owin;

namespace Wyam.Server
{
internal class HttpServer : IDisposable
{
private IWebHost _host;

public void StartServer(int port, Action<IAppBuilder> owinConfiguration)
{
_host = new WebHostBuilder()
.UseKestrel()
.UseUrls($"http://localhost:{port}")
.Configure(builder =>
{
// Enable websocket support
builder.UseWebSockets();

// Support drop-in replacement
builder.UseOwinBuilder(owinConfiguration);
})
.Build();
_host.Start();
}

public void Dispose()
{
_host?.Dispose();
}
}
}
Loading