Skip to content

Instantly share code, notes, and snippets.

@jonfriesen
Last active June 12, 2016 03:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jonfriesen/4437b45097651c35ed12 to your computer and use it in GitHub Desktop.
Save jonfriesen/4437b45097651c35ed12 to your computer and use it in GitHub Desktop.
Creates a http file server, if /exit is called server is shutdown
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