Skip to content

Instantly share code, notes, and snippets.

@mastoj
Created November 12, 2013 15:48
Show Gist options
  • Save mastoj/7433172 to your computer and use it in GitHub Desktop.
Save mastoj/7433172 to your computer and use it in GitHub Desktop.
Simple Nowin app with middleware
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Threading.Tasks;
using Nowin;
namespace HelloNowin
{
using AppFunc = Func<IDictionary<string, object>, Task>;
class Program
{
static void Main(string[] args)
{
AppFunc app = env =>
{
var responseBytes = System.Text.Encoding.UTF8.GetBytes(
string.Format("Serviced request on {0} at {1}", DateTime.Now.ToLongDateString(),
DateTime.Now.ToLongTimeString()));
Stream responseStream = (Stream)env["owin.ResponseBody"];
IDictionary<string, string[]> responseHeaders =
(IDictionary<string, string[]>)env["owin.ResponseHeaders"];
responseHeaders["Content-Length"] = new string[] { responseBytes.Length.ToString(CultureInfo.InvariantCulture) };
responseHeaders["Content-Type"] = new string[] { "text/plain" };
return responseStream.WriteAsync(responseBytes, 0, responseBytes.Length);
};
AppFunc appWithMiddleWare = env =>
{
var responseBytes = System.Text.Encoding.UTF8.GetBytes(
string.Format("Some middleware {0} at {1} {2}", DateTime.Now.ToLongDateString(),
DateTime.Now.ToLongTimeString(), Environment.NewLine));
Stream responseStream = (Stream) env["owin.ResponseBody"];
responseStream.WriteAsync(responseBytes, 0, responseBytes.Length);
return app(env);
};
var serverBuilder = ServerBuilder.New().SetPort(1337).SetOwinApp(appWithMiddleWare);
using (serverBuilder.Start())
{
Console.WriteLine("Listening on port 1337");
Console.ReadLine();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment