Skip to content

Instantly share code, notes, and snippets.

@DominicFinn
Forked from serialseb/owinfx
Created May 30, 2014 15:19
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 DominicFinn/260d37a8a36e82d1fc0e to your computer and use it in GitHub Desktop.
Save DominicFinn/260d37a8a36e82d1fc0e to your computer and use it in GitHub Desktop.
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;
var myApp = new MyUriDispatcher(new Dictionary<string, MidFunc>
{
{ "/hello", appBuilder.UseMessage("hello") },
{ "/motd", appBuilder.UseMessage(QuoteOfTheDay)},
{ "/exception", next=>env => { throw new InvalidOperationException(); } },
{ "/nancy", appBuilder.UseNancy()},
{ "/nancy2", appBuilder.UseNancy()}
});
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 static class MyBuilderExtensions
{
public static MidFunc UseNancy(this IAppBuilder appBuilder)
{
return next => new NancyOwinHost(_ => Task.FromResult(0), new NancyOptions()).Invoke;
}
public static MidFunc UseMessage(this IAppBuilder appBuilder, string message)
{
return next => new MessageWriterApp(message).Invoke;
}
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 interface IAppBuilder
{
}
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, MidFunc> _uriMaps;
public MyUriDispatcher(IDictionary<string,MidFunc> 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<MidFunc> notFound)
{
MidFunc myApp = null;
_uriMaps.TryGetValue(requestUri, out myApp) ;
var execute = myApp ?? notFound();
return execute(null)(env);
}
MidFunc Write404()
{
return next=>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