Skip to content

Instantly share code, notes, and snippets.

View thecodejunkie's full-sized avatar

Andreas Håkansson thecodejunkie

View GitHub Profile
@thecodejunkie
thecodejunkie / gist:834598
Created February 18, 2011 23:29
Sample of what a route could look like in Nancy using the new view engine integration code
Get[@"/(?<foo>\d{2,4})/{bar}"] = parameters => {
return View["viewname", parameters];
};
@thecodejunkie
thecodejunkie / gist:834601
Created February 18, 2011 23:31
Example of what a route condition could look like using Nancy
Get[@"/(?<foo>\d{2,4})/{bar}", () => Request.Protocol.Equals("https")] = parameters => {
return View["viewname", parameters];
};
@thecodejunkie
thecodejunkie / gist:837889
Created February 21, 2011 23:11
Sample for how a nested closure could be used to configure a ServiceHost in Elasticity
class Program
{
static void Main(string[] args)
{
var scheduler =
new DefaultScheduler();
var host = new ServiceHost(scheduler, x => {
x.JobRequestQueue = "foo";
x.TaskResponseQueue = "bar";
namespace NancyConNegDemo
{
using Extensions;
using Models;
using Nancy;
public class ConNegModule : NancyModule
{
public ConNegModule() : base("/conneg")
{
@thecodejunkie
thecodejunkie / DefaultNancyModuleBuilders.cs
Created February 24, 2011 08:28
Implementation of NancyModuleBuilder
public class DefaultNancyModuleBuilders : INancyModuleBuilder
{
private readonly INancyModuleCatalog moduleCatalog;
private readonly IViewFactory viewFactory;
private readonly ResponseFormatter responseFormatter;
public DefaultNancyModuleBuilders(INancyModuleCatalog moduleCatalog, IViewFactory viewFactory, ResponseFormatter responseFormatter)
{
this.moduleCatalog = moduleCatalog;
this.viewFactory = viewFactory;
@thecodejunkie
thecodejunkie / gist:843598
Created February 25, 2011 09:55
Proof of concept for serving static contents with a BeforeRequest application hook in Nancy.
BeforeRequest += ctx => {
var rootPathProvider =
container.Resolve<IRootPathProvider>();
var staticFileExtensions =
new Dictionary<string, string>
{
{ "jpg", "image/jpg" },
{ "png", "image/png" },
@thecodejunkie
thecodejunkie / gist:857603
Created March 6, 2011 20:07
Scanning for start end end of boundary
private Tuple<long, long> GetNextBoundaryPositions()
{
var boundryMarkerSequence = new[] { (byte) '-', (byte) '-'};
var buffer = new byte[2];
var boundaryBuffer = new byte[Encoding.UTF8.GetBytes(this.boundary).Length];
while(true)
{
var numberOfBytesRead =
this.requestStream.Read(buffer, 0, 2);
@thecodejunkie
thecodejunkie / Boundary.cs
Created March 7, 2011 08:06
Read HTTP multipart/form-data formatted streams
public class Boundary
{
private readonly SubStream boundaryStream;
private const byte LF = (byte)'\n';
private const byte CR = (byte)'\r';
public Boundary(SubStream boundaryStream)
{
this.boundaryStream = boundaryStream;
this.ExtractHeaders();
@thecodejunkie
thecodejunkie / RequestStream.cs
Created March 15, 2011 08:01
Spike of Nancy RequestStream
public class RequestStream : Stream
{
private Stream stream;
private readonly long expectedLength;
private readonly long threshHoldLength;
private bool disableStreamSwapping;
public RequestStream(long expectedLength, long threshHoldLength, bool disableStreamSwapping)
: this(null, expectedLength, threshHoldLength, disableStreamSwapping)
{
@thecodejunkie
thecodejunkie / gist:879433
Created March 21, 2011 13:14
Spike of players in Nancy view resolution using conventions
/*
* Conventions are maintained as Func<string, NancyModule, string> delegates
* Bootstrapper lets you register these in a IList<Func<string, NancyModule, string>> which then goes into the container
* Take a dependency on IEnumerable<Func<string, NancyModule, string>> when you need access to the delegates
*/
public interface IViewFactory
{
Action<Stream> RenderView(NancyModule module, string viewName, dynamic model);
}