Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Force mustache partials to be uncached from the local filesystem in development mode. #36

Merged
merged 1 commit into from
Feb 18, 2013
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 46 additions & 6 deletions src/main/scala/com/twitter/finatra/View.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,58 @@
package com.twitter.finatra

import com.github.mustachejava._
import com.google.common.base.Charsets;
import com.twitter.io.TempFile
import com.twitter.mustache._

import java.io._
import com.twitter.io.TempFile
import java.util.concurrent.Callable
import java.util.concurrent.Executors

class FinatraMustacheFactory extends DefaultMustacheFactory {
class FinatraMustacheFactory(baseTemplatePath:String) extends DefaultMustacheFactory {
override def encode(str: String, wtr: Writer) {
wtr.append(str, 0, str.length)
}

override def getReader(resourceName:String) : Reader = {
if (!"development".equals(Config.get("env"))) {
super.getReader(resourceName)
}
// In development mode, we look to the local file
// system and avoid using the classloader which has
// priority in DefaultMustacheFactory.getReader
else {
val file:File = new File(baseTemplatePath, resourceName)
if (file.exists() && file.isFile()) {
try {
new BufferedReader(new InputStreamReader(new FileInputStream(file),
Charsets.UTF_8));
} catch {
case exception:FileNotFoundException =>
throw new MustacheException("Found file, could not open: " + file, exception)
}
}
else {
throw new MustacheException("Template '" + resourceName + "' not found at " + file);
}

}
}

/**
* Invalidate template caches during development
*/
def invalidateMustacheCaches() : Unit = {
mustacheCache.invalidateAll()
templateCache.invalidateAll()
}

}

object View {
lazy val mustacheFactory = new FinatraMustacheFactory
lazy val mustacheFactory = new FinatraMustacheFactory(Config.get("local_docroot"))
var baseTemplatePath = Config.get("template_path")
def templatePath = baseTemplatePath


mustacheFactory.setObjectHandler(new TwitterObjectHandler)
mustacheFactory.setExecutorService(Executors.newCachedThreadPool)
}
Expand All @@ -50,10 +83,17 @@ abstract class View extends Callable[String] {
def mustache = factory.compile(new InputStreamReader(FileResolver.getInputStream(templatePath)), "template")

def render = {
// In development mode, we flush all of our template
// caches on each render. Otherwise, partials will
// remain unchanged in the browser while being edited.
if ("development".equals(Config.get("env"))) {
factory.invalidateMustacheCaches()
}

val output = new StringWriter
mustache.execute(output, this).flush()
output.toString
}

def call = render
}
}