Last active
June 12, 2016 03:53
-
-
Save jonfriesen/4437b45097651c35ed12 to your computer and use it in GitHub Desktop.
Creates a http file server, if /exit is called server is shutdown
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
using Microsoft.Owin.Hosting; | |
using Owin; | |
using Microsoft.Owin.FileSystems; | |
using Microsoft.Owin.StaticFiles; | |
using System.Threading; | |
using System.Web.Http; | |
using System.Net; | |
namespace OwinFileServerControl | |
{ | |
class Program | |
{ | |
static ManualResetEvent waitForExit = new ManualResetEvent(false); | |
static void Main(string[] args) | |
{ | |
var url = "http://localhost:8080"; | |
var root = @"C:\temp"; | |
var fs = new PhysicalFileSystem(root); | |
var fsConfig = new FileServerOptions | |
{ | |
EnableDirectoryBrowsing = true, | |
FileSystem = fs | |
}; | |
var app = WebApp.Start(url, | |
builder => | |
{ | |
builder.UseFileServer(fsConfig); | |
builder.Run(context => | |
{ | |
var task = context.Response.WriteAsync(context.Request.Path.ToString()); | |
if (context.Request.Path.ToString().Equals("/exit")) | |
{ | |
waitForExit.Set(); | |
} | |
return task; | |
}); | |
} | |
); | |
Console.WriteLine("Waiting for /exit or 8 hours"); | |
Console.WriteLine("Listening at: " + url); | |
waitForExit.WaitOne(TimeSpan.FromHours(8)); // will wait here until waitForExit.Set() is called or 8 hours, whichever occurs first | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment