Skip to content

Instantly share code, notes, and snippets.

@thecodejunkie
Last active August 29, 2015 14:04
Show Gist options
  • Save thecodejunkie/cc5c1dde24cd81435723 to your computer and use it in GitHub Desktop.
Save thecodejunkie/cc5c1dde24cd81435723 to your computer and use it in GitHub Desktop.
Spiking Nancy and Jails integration
namespace JailsPlayground
{
using System;
using System.Collections.Generic;
using System.Reflection;
using Jails;
using Jails.Extensions.Roslyn;
using Jails.Isolators.AppDomain;
using Nancy;
using Nancy.Bootstrapper;
class Program
{
static void Main(string[] args)
{
var code = @"using System;
using Nancy;
namespace Foo
{
public class IndexModule : NancyModule
{
public IndexModule()
{
Get[""/""] = x => ""Hello from Nancy"";
}
}
}";
var isolator = new AppDomainIsolator();
var environment = new NancyEnvironment();
environment.Register(new CSharpSource(code));
using (var jail = Jail.Create(isolator, environment))
{
dynamic bar = jail.Resolve("JailsPlayground.CodeDrivenNancyHost");
var request = new Request("GET", "/", "http");
var response = bar.Execute(request);
Console.WriteLine(response.StatusCode);
}
Console.ReadLine();
}
}
public class CodeDrivenNancyHost : IDisposable
{
private readonly INancyBootstrapper bootstrapper;
private readonly INancyEngine engine;
public CodeDrivenNancyHost()
{
this.bootstrapper = NancyBootstrapperLocator.Bootstrapper;
this.bootstrapper.Initialise();
this.engine = this.bootstrapper.GetEngine();
}
public void Dispose()
{
this.bootstrapper.Dispose();
}
public TestResponse Execute(TestRequest request)
{
var actualRequest =
new Request(request.Method, request.Path, "http");
using (var nancyContext = this.engine.HandleRequest(actualRequest))
{
return new TestResponse {StatusCode = (int) nancyContext.Response.StatusCode};
}
}
}
[Serializable]
public class TestRequest
{
public string Method { get; set; }
public string Path { get; set; }
}
[Serializable]
public class TestResponse
{
public int StatusCode { get; set; }
}
public class NancyEnvironment : IEnvironment
{
public void Register(AssemblyName assembly)
{
}
public void Register(InMemoryAssembly assembly)
{
}
public IEnumerable<object> GetRegistrations()
{
yield return typeof (INancyEngine).Assembly.GetName();
yield return typeof (NancyEnvironment).Assembly.GetName();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment