Skip to content

Instantly share code, notes, and snippets.

@HCanber
Created August 31, 2011 18:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save HCanber/1184386 to your computer and use it in GitHub Desktop.
Save HCanber/1184386 to your computer and use it in GitHub Desktop.
Console app running an OWIN Hello World application on Kayak
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using Gate;
using Gate.Kayak;
using Kayak;
namespace OwinHelloWorld
{
class Program
{
static void Main(string[] args)
{
//Create the endpoint for incoming requests to Kayak
//Let SchedulerDelegate handle exceptions and shut downs
var kayakEndPoint = new IPEndPoint(IPAddress.Any, 5500);
Console.WriteLine("Listening on " + kayakEndPoint);
//Start Kayak and call the method Startup.Configuration when starting
KayakGate.Start(new SchedulerDelegate(), kayakEndPoint, Startup.Configuration);
}
public class SchedulerDelegate : ISchedulerDelegate
{
public void OnException(IScheduler scheduler, Exception e)
{
// called whenever an exception occurs on Kayak's event loop.
// this is good place for logging. here's a start:
Console.WriteLine("Exception on scheduler");
Console.Out.WriteStackTrace(e);
}
public void OnStop(IScheduler scheduler)
{
// called when Kayak's run loop is about to exit.
// this is a good place for doing clean-up or other chores.
Console.WriteLine("Scheduler is stopping.");
}
}
public class Startup
{
// called automatically when Kayak starts up.
public static void Configuration(IAppBuilder builder)
{
// we'll create a very simple pipeline:
var app = new HelloWorldOwinApp();
builder.Run(Delegates.ToDelegate(app.ProcessRequest));
}
}
public class HelloWorldOwinApp
{
public void ProcessRequest(IDictionary<string, object> environment,
Action<string, IDictionary<string, string>, Func<Func<ArraySegment<byte>, Action, bool>, Action<Exception>, Action, Action>> responseCallBack,
Action<Exception> errorCallback)
{
var path = environment["owin.RequestPath"] as string;
var responseHeaders = new Dictionary<string, string>();
ArraySegment<byte> responseBody;
string responseStatus;
if (path == "/")
{
responseStatus = "200 OK";
responseHeaders.Add("Content-Type", "text/html");
responseBody = new ArraySegment<byte>(Encoding.UTF8.GetBytes((
"<!doctype html><html><head><meta charset=\"utf-8\">" +
"<title>Hello World</title></head>" +
"<body><strong>Hello world</strong></body>" +
"</html>")));
}
else
{
responseStatus = "404 Not Found";
responseHeaders.Add("Content-Type", "text/html");
responseBody = new ArraySegment<byte>(Encoding.UTF8.GetBytes((
"<!doctype html><html><head><meta charset=\"utf-8\">" +
"<title>404 Not Found</title></head>" +
"<body>The resource cannot be found.</body>" +
"</html>")));
}
responseCallBack(
responseStatus,
responseHeaders,
(next, error, complete) =>
{
next(responseBody, null);
complete();
return () => { };
});
}
}
}
}
@HCanber
Copy link
Author

HCanber commented Aug 31, 2011

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment