-
-
Save DominicFinn/70611eef68f0ac0a3ea2 to your computer and use it in GitHub Desktop.
dsfg
This file contains 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.IO; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
using Nancy; | |
using Nancy.Owin; | |
using Nowin; | |
namespace OwinFx | |
{ | |
using Env = IDictionary<string, object>; | |
using AppFunc = Func<IDictionary<string,object>,Task>; | |
using MidFunc = Func<Func<IDictionary<string, object>, Task>, Func<IDictionary<string, object>, Task>>; | |
class Program | |
{ | |
static string QuoteOfTheDay = "Masak harus galau , antara berhati hati dengan biasa saja"; | |
static void Main(string[] args) | |
{ | |
IAppBuilder appBuilder = null; | |
AppFunc nancyApp = | |
new NancyOwinHost(_=>Task.FromResult(0),new NancyOptions()).Invoke; | |
var myApp = new MyUriDispatcher(new Dictionary<string, AppFunc> | |
{ | |
{ "/hello", new MessageWriterApp("hello").Invoke }, | |
{ "/motd", new MessageWriterApp(QuoteOfTheDay).Invoke}, | |
{ "/exception", env => { throw new InvalidOperationException(); } }, | |
{ "/nancy", nancyApp}, | |
{ "/nancy2", nancyApp} | |
}); | |
MidFunc myVanity = appBuilder.UseVanity(); | |
MidFunc myNotFound = appBuilder.UseNotFound(); | |
var myAppWithVanity = myNotFound(myVanity(myApp.Dispatch)); | |
//var myAppWithVanity = new VanityHeaderMiddleware(myApp.Dispatch); | |
new ServerBuilder().SetPort(8080) | |
.SetOwinApp(myAppWithVanity.Invoke).Start(); | |
Console.ReadLine(); | |
} | |
} | |
public interface IAppBuilder | |
{ | |
} | |
public static class MyBuilderExtensions | |
{ | |
public static MidFunc UseVanity(this IAppBuilder appBuilder) | |
{ | |
return next => new VanityHeaderMiddleware(next).Invoke; | |
} | |
public static MidFunc UseNotFound(this IAppBuilder appBuilder) | |
{ | |
return next => new NotFoundMiddleware(next).Invoke; | |
} | |
} | |
public class NotFoundMiddleware | |
{ | |
readonly AppFunc _next; | |
public NotFoundMiddleware(AppFunc next) | |
{ | |
_next = next; | |
} | |
public async Task Invoke(Env env) | |
{ | |
await _next(env); | |
if ((int)env["owin.ResponseStatusCode"] != 404) return; | |
var headers = (IDictionary<string, string[]>)env["owin.ResponseHeaders"]; | |
if (headers["Content-Length"].Any()) return; | |
var responseStream = (Stream)env["owin.ResponseBody"]; | |
var buffer = Encoding.UTF8.GetBytes("Not found"); | |
await responseStream.WriteAsync(buffer, 0, buffer.Length); | |
} | |
} | |
public class VanityHeaderMiddleware | |
{ | |
readonly AppFunc _next; | |
public VanityHeaderMiddleware(AppFunc next) | |
{ | |
_next = next; | |
} | |
public Task Invoke(Env env) | |
{ | |
var headers = (IDictionary<string, string[]>)env["owin.ResponseHeaders"]; | |
headers["X-PoweredByOwin"] = new[] { "1.0" }; | |
return _next(env); | |
} | |
} | |
public class MyNancyCodeModule : NancyModule | |
{ | |
public MyNancyCodeModule() | |
{ | |
Get["/nancy"] = ctx => "nancy"; | |
} | |
} | |
public class MyUriDispatcher | |
{ | |
readonly IDictionary<string, AppFunc> _uriMaps; | |
public MyUriDispatcher(IDictionary<string,AppFunc> uriMaps) | |
{ | |
_uriMaps = uriMaps; | |
} | |
public Task Dispatch(IDictionary<string, object> env) | |
{ | |
var requestUri = (string)env["owin.RequestPath"]; | |
return DispatchToAppFuncOr(requestUri,env, Write404); | |
} | |
Task DispatchToAppFuncOr(string requestUri, | |
IDictionary<string, object> env, | |
Func<AppFunc> notFound) | |
{ | |
AppFunc myApp = null; | |
_uriMaps.TryGetValue(requestUri, out myApp) ; | |
return (myApp ?? notFound())(env); | |
} | |
AppFunc Write404() | |
{ | |
return async env=> { env["owin.ResponseStatusCode"] = 404; }; | |
} | |
} | |
public class MessageWriterApp | |
{ | |
readonly string _text; | |
readonly int _statusCode; | |
public MessageWriterApp(string text, int statusCode = 200) | |
{ | |
_text = text; | |
_statusCode = statusCode; | |
} | |
public async Task Invoke(IDictionary<string, object> env) | |
{ | |
var responseStream = (Stream)env["owin.ResponseBody"]; | |
env["owin.ResponseStatusCode"] = _statusCode; | |
var buffer = Encoding.UTF8.GetBytes(_text); | |
await responseStream.WriteAsync(buffer, 0, buffer.Length); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment