Created
January 22, 2017 11:33
-
-
Save oguzhaneren/eaf45261e4572bd2f480625a256f93b1 to your computer and use it in GitHub Desktop.
OwinRouter
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.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