Skip to content

Instantly share code, notes, and snippets.

@oguzhaneren
Created January 22, 2017 11:33
Show Gist options
  • Save oguzhaneren/eaf45261e4572bd2f480625a256f93b1 to your computer and use it in GitHub Desktop.
Save oguzhaneren/eaf45261e4572bd2f480625a256f93b1 to your computer and use it in GitHub Desktop.
OwinRouter
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.Owin;
using AppFunc = System.Func<System.Collections.Generic.IDictionary<string, object>, System.Threading.Tasks.Task>;
using MidFunc = System.Func<
System.Func<System.Collections.Generic.IDictionary<string, object>, System.Threading.Tasks.Task>,
System.Func<System.Collections.Generic.IDictionary<string, object>, System.Threading.Tasks.Task>>;
public class OwinRouter
{
private readonly Dictionary<string, AppFunc> _hosts = new Dictionary<string, AppFunc>();
public void AddHost(string hostname, AppFunc appFunc)
{
_hosts.Add(hostname, appFunc);
}
public void AddHost(string hostname, MidFunc midFunc)
{
var appFunc = midFunc(env =>
{
var context = new OwinContext(env);
context.Response.StatusCode = 404;
context.Response.ReasonPhrase = "OK";
return Task.CompletedTask;
});
AddHost(hostname, appFunc);
}
public AppFunc AppFunc
{
get
{
return env =>
{
var context = new OwinContext();
var host = context.Request.Host.Value;
AppFunc route;
if (_hosts.TryGetValue(host, out route))
{
return route(env);
}
throw new InvalidOperationException("Unknown host.");
};
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment