Roy Notes

技术 创业 思考

Play CDN Support Module

根据前一篇文章的实现思路写了这个插件,并放到了github,有需要的朋友请自行下载使用。

http://github.com/roymax/play-cdn {将不再维护和准备关闭了}


Notes: 从Play! 1.1开始已经华丽地通过修改route原生支持CDN部署。
在route中修改原来的静态资源映射如下

# Map static resources from the /app/public folder to the /public path
#{if play.mode.isDev()}
GET     /public/images/                 staticDir:public/images
GET     /public/                staticDir:public/
#{/}
#{else}
GET     img.myapp.com/         staticDir:public/images
GET     jscss.myapp.com/     staticDir:public
#{/}

在引用资源的时候使用@@{} 而不是默认的@{}标签,如

<link rel="stylesheet" type="text/css" media="screen,projection" href="@@{'/public/stylesheets/style.css'}">

以上代码能够在开发环境与部署环境中完美地自动切换。

具体看这里的 , http://www.playframework.org/documentation/1.1RC1/releasenotes-1.1

Virtual hosting in routes

The routes file now supports Host matching. this can be useful if action parameters must be extracted from the host parameter. For example, for a SAAS application, you could use:

GET    {client}.mysoftware.com/         Application.index

and then automatically retrieve the client value as for any other request parameter:

public static void index(String client) {
    ...
}

When using the @@{…} notation (absolute reverse routing) in a template, the host will be used if provided by the corresponding route. This can be useful in several situations.

For example, if you want to use a content distribution network to distribute your static assets in production, you could write a routes file like this:

#{if Play.mode.isDev()}
    GET     /public/                        staticDir:public
#{/}
#{else}
    GET     assets.myapp.com/               staticDir:public
#{/}

And in your templates:

<img src="@@{'/public/images/logo.png'}">

This will be reversed as http://locahost:9000/public/images/logo.png in DEV mode, andhttp://assets.myapp.com/images/logo.png in PROD mode.

Comments