Skip to content

Instantly share code, notes, and snippets.

@ScottWeinstein
Created March 27, 2011 03:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ScottWeinstein/888889 to your computer and use it in GitHub Desktop.
Save ScottWeinstein/888889 to your computer and use it in GitHub Desktop.
CoffeeScriptHandler for ASP.Net
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Diagnostics;
using System.Web.Caching;
using System.IO;
namespace CoffeeScriptHandler
{
public class CoffeeScriptHandler : IHttpHandler
{
private const string NODE_BIN = @"c:\node\bin\";
public bool IsReusable
{
get { return true; }
}
public void ProcessRequest(HttpContext context)
{
ProcessRequest(new HttpContextWrapper(context));
}
public static void ProcessRequest(HttpContextWrapper context)
{
string path = context.Request.PhysicalPath;
DateTime lastWrite = File.GetLastWriteTime(path);
Cache cache = context.Cache;
var cachedItem = ((Tuple<DateTime, string>)cache[path]);
if (cachedItem == null || cachedItem.Item1 < lastWrite)
{
ProcessStartInfo psi = new ProcessStartInfo(NODE_BIN + "node.exe", "coffee -cs");
psi.UseShellExecute = false;
psi.RedirectStandardOutput = true;
psi.RedirectStandardInput = true;
psi.WorkingDirectory = NODE_BIN;
Process process = Process.Start(psi);
process.StandardInput.Write(File.ReadAllText(path));
process.StandardInput.Close();
process.WaitForExit();
string result = process.StandardOutput.ReadToEnd();
cache[path] = cachedItem = Tuple.Create<DateTime, string>(lastWrite, result);
}
context.Response.ContentType = "application/javascript";
context.Response.Write(cachedItem.Item2);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment