Created
January 5, 2011 01:00
-
-
Save kevinswiber/765754 to your computer and use it in GitHub Desktop.
Shows an example of NRack middleware...
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using NRack.Adapters; | |
namespace NRack.Example.AspNet | |
{ | |
public class EnvironmentOutput : ICallable, IIterable | |
{ | |
private readonly dynamic _application; | |
private dynamic _response; | |
private IDictionary<string, dynamic> _environment; | |
public EnvironmentOutput(dynamic application) | |
{ | |
_application = application; | |
} | |
public EnvironmentOutput() : this(null) | |
{ | |
} | |
public dynamic[] Call(IDictionary<string, dynamic> environment) | |
{ | |
_environment = environment; | |
if (_application != null) | |
{ | |
var response = _application.Call(environment); | |
_response = response[2]; | |
return new[] { response[0], response[1], this }; | |
} | |
return new dynamic[] {200, new Hash{{"Content-Type", "text/html"}}, this}; | |
} | |
public void Each(Action<string> action) | |
{ | |
if (_response != null) | |
{ | |
_response.Each(action); | |
} | |
var envOutput = _environment.Keys | |
.Aggregate("", (current, key) => current + string.Format("<li>{0}={1}</li>", key, _environment[key].ToString())); | |
action(envOutput); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment