Skip to content

Instantly share code, notes, and snippets.

@mikeminutillo
Created February 11, 2020 03:55
Show Gist options
  • Save mikeminutillo/a92f14680c97588ab7f9f6e31e0c808b to your computer and use it in GitHub Desktop.
Save mikeminutillo/a92f14680c97588ab7f9f6e31e0c808b to your computer and use it in GitHub Desktop.
Hosting ServicePulse from a zip within ServiceControl
using System.Net;
using Owin;
static class OwinExtensions
{
public static IAppBuilder RedirectRootTo(this IAppBuilder app, string destination)
{
app.Use(async (ctx, next) =>
{
if (ctx.Request.Path.Value == "/")
{
ctx.Response.StatusCode = (int)HttpStatusCode.Redirect;
ctx.Response.Headers.Set("Location", ctx.Request.Uri + destination);
}
else
{
await next().ConfigureAwait(false);
}
});
return app;
}
}
app.Map("/web", b =>
{
var uiFileSystem = new ZipFileSystem(@"C:\Code\Particular\ServicePulse-1.22.0.zip");
b.UseDefaultFiles(new DefaultFilesOptions
{
FileSystem = uiFileSystem,
DefaultFileNames = new List<string>{ "index.html" }
});
var options = new StaticFileOptions
{
FileSystem = uiFileSystem,
ServeUnknownFileTypes = true
};
b.UseStaticFiles(options);
});
app.RedirectRootTo("web");
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Linq;
using Microsoft.Owin.FileSystems;
class ZipFileSystem : IFileSystem
{
ZipArchive archive;
public ZipFileSystem(string path)
{
archive = ZipFile.OpenRead(path);
}
public bool TryGetFileInfo(string subpath, out IFileInfo fileInfo)
{
if (subpath.Length > 1)
{
var entry = archive.GetEntry(subpath.Substring(1));
if (entry != null)
{
fileInfo = new ZipEntryFileInfo(entry);
return true;
}
}
fileInfo = null;
return false;
}
public bool TryGetDirectoryContents(string subpath, out IEnumerable<IFileInfo> contents)
{
contents = Enumerable.Empty<IFileInfo>();
return true;
}
class ZipEntryFileInfo : IFileInfo
{
readonly ZipArchiveEntry entry;
public ZipEntryFileInfo(ZipArchiveEntry entry)
{
this.entry = entry;
}
public Stream CreateReadStream() => entry.Open();
public long Length => entry.Length;
public string PhysicalPath => null;
public string Name => entry.Name;
public DateTime LastModified => entry.LastWriteTime.DateTime;
public bool IsDirectory => false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment