简体   繁体   English

CDN在Play 2.0上使用

[英]CDN usages on Play 2.0

I have a high traffic website with a lot of static content. 我有一个拥有大量静态内容的高流量网站。 It is currently on Play 1.2.4 but I am doing the migration to Play 2.0.2. 它目前在Play 1.2.4上,但我正在迁移到Play 2.0.2。

For Play 1.X we wrote some code that we used instead of the @asset inside of html templates. 对于Play 1.X,我们编写了一些代码,而不是html模板中的@asset。

/**
 * Drop-in replacement for @asset. Use to take advantage of cloudfront on live.
 * Paths are always absolute to root. Leading '/' is optional.
 *
 * @param path relative to the application root. This should usually be "public/some-file"
 * @return path to asset on the currently configured CDN.
 */
def cdnAsset(path: String) : String = {
  cdnEnabled match {
    case "true" =>
      path(0) match {
        case '/' => "https://" + cdnUrl + path
        case _ =>  "https://" + cdnUrl + "/" + path
      }

    case _ =>
        play.mvc.Router.reverse(play.Play.getVirtualFile(path))
  }
}

For Play 2.0 I think we can improve upon this. 对于Play 2.0,我认为我们可以改进这一点。 I think it would be better if we didn't have to litter our templates with our custom code instead of using the the @Asset.at provided by Play 2.0. 我认为如果我们不必使用我们的自定义代码而不是使用Play 2.0提供的@ Asset.at来丢弃我们的模板会更好。 I not sure the best way to do this. 我不确定最好的方法。 I wondering if doing something like was done in the answer to this question on Play 1.2.X Hosting static HTML in a Play! 我想知道是否做了类似的事情在Play 1.2.X中回答这个问题的答案在播放中托管静态HTML! app on CloudFront could be done for Play 2.0. CloudFront上的应用程序可以为Play 2.0完成。

I would like to take full advantage of the Assets controller provided by Play 2.0 since it performs a few optimizations that would be nice to have. 我想充分利用Play 2.0提供的Assets控制器,因为它执行了一些很好的优化。

Does anyone know a way of doing this? 有谁知道这样做的方法? I'm thinking if it can be done with just some Router magic, that would be ideal but I'm still a little too beginner with Play to know if or how that is possible. 我正在考虑是否可以用一些路由器魔法来完成,这将是理想的但我仍然有点太初学的Play来知道是否或如何可能。

詹姆斯沃德已经写了一个很好的教程 ,干净利落地完成它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM