Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

For Play 1.X we wrote some code that we used instead of the @asset inside of html templates.

/**
 * 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. 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. 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! app on CloudFront could be done for 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.

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.

like image 899
myyk Avatar asked Oct 07 '22 19:10

myyk


1 Answers

James Ward has written an excellent tutorial to do it cleanly.

like image 178
mchv Avatar answered Oct 10 '22 02:10

mchv