Finatra使用mustache的疑问,避免local.doc.root干扰

访问本地的mustache模板文件的源码路径如下

com.twitter.finatra.http.modules.MustacheModule

从源码可以看出,是否使用cache的template是取决为local.doc.root是否为空.

为了避免这个参数干扰到使用cache缓存mustache

重新复制一份代码,命名为myMustacheModule

def provideMustacheFactory(
                              resolver: FileResolver,
                              @Flag("local.doc.root") localDocRoot: String): MustacheFactory = {
    // templates are cached only if there is no local.doc.root
//    val cacheMustacheTemplates = localDocRoot.isEmpty
    val templatesDirectory = templatesDir()

    new DefaultMustacheFactory(templatesDirectory) {
      setObjectHandler(new ScalaObjectHandler)

      override def compile(name: String): Mustache = {
//        if (cacheMustacheTemplates) {
          super.compile(name)
//        } else {
//          new LocalFilesystemDefaultMustacheFactory(templatesDirectory, resolver).compile(name)
//        }
      }
    }
  }
}

直接使用super.compile(name)

然后在继承HttpServer类里面声明一个mustacheModule的变量,覆盖默认的就可以

object web_test extends HttpServer {
  override def configureHttp(router : HttpRouter) = {
    router
      .add[testController]
  }
  override val defaultFinatraHttpPort = ":8000"
  override def mustacheModule = myMustacheModule  // 这样就可以使用自定义的module里
}

这样就可以在有local.doc.root的时候,继续使用cache

转载于:https://my.oschina.net/u/1538135/blog/657990

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值