首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

Play+Nginx配备多网站,动态生成routes,动态更新Application

2013-03-01 
Play+Nginx配置多网站,动态生成routes,动态更新ApplicationPlay 运行多网站(主要是静态网站),初步想法是把

Play+Nginx配置多网站,动态生成routes,动态更新Application
Play 运行多网站(主要是静态网站),初步想法是把新网站的views和controllers及routes链接或重写一下


views直接链接到总网站的“views/项目名”文件夹下,这时你的views下会多了诸如taobao_com、baidu_com的文件夹,而文件夹里的内容就是相关项目的views

contollers比较复杂,因为每一个项目默认的conrollers都是Application.scala,而用views文件夹的方法又会导致编译失败,所以只好把项目的controllers链接到总网站的“controllers/.项目名”文件夹下,加点的原因是在linux下会被隐藏(什么,你用windows开发,汗。。。有前途吗?自己想辙吧),这样就不会被编译,而文件夹拷贝过来主要是为了方便一会进行处理

routes的处理也不简单,需要重写“conf/rout”文件,我们就把所有的routes链接到“conf/route/项目名”下,方便我们处理

Where there is a shell,there is a way.shell 自动处理play多网站的views,conrollers,routes链接

文件链接完成了,我们可以在“app/Global.scala”里面在对conrollers和routes进行处理,代码如下
import play.api._import play.api.mvc._import play.api.mvc.Results._    import scala.io._import scalax.io._import java.io._import java.math._    import scala.collection.mutable.LinkedHashSetobject  Global extends GlobalSettings {        override def beforeStart(app:Application){            val filename="conf/routes"            new File("conf/routes").delete()            println("正在删除原来的routes文件")            val output:Output=Resource.fromFile("conf/routes")            println("正在准备创建新的routes文件")            var fileLines=LinkedHashSet("# This file is generate automatically.\r\n")            val files=new File("conf/route/")            for(file<-files.list()){                print("正在处理"+file)                val fileName=file.replaceAll("_","").replaceFirst(file.substring(0, 1),file.su                                     bstring(0, 1).toUpperCase()) ;             //把诸如baidu_com变成Baiducom,主要是为了符合conrollers/Application.scala的命名            //规范                           fileLines+="# This part is generate from "+file+"\r\n"                for(line<-Source.fromFile("conf/route/"+file).getLines){                                var lineNew=line.toString.replaceAll("\t+", " ").replaceAll(" +", " ")               //为了找到routes里面的重复内容,我们对routes里面的没一行进行格式化,把tab和多个空               // 格替换成一个空格                                var tabStr=""                                       while(tabStr.length<(100-lineNew.length-file.toString.length))                                    tabStr+=" "                                tabStr+="controllers."+fileName                //这里有一次进行格式化,通过添加空格使每一行都至少100个字符,即简单的右对齐效果                            fileLines+=lineNew.replaceAll("controllers.Application",tabStr).replaceAll("GE                               T /","GET /"+file+"/").replaceAll("POST /","POST     /"+fil                               e+"/")            //由于controllers/Application.scala被改成诸如Baiducom.scala的格式,所以把routes            // 里面的所有的controllers.Application替换成类似controllers.Baiducom把GET / 替换             // 成类似GET /baidu_com/ 等等                   fileLines+="GET /assets/*file controllers.Assets.at(path=\"/public\", file                                     )"                    print(".")                }                println()                new File("app/controllers/"+fileName+".scala").delete()                println("正在删除原来的"+file+"Applicaton.scala文件")                print("正在重写"+file+"的Applicaton.scala")                var outputApp:Output=Resource.fromFile("app/controllers/"+fileName+".scala")                for(line<-Source.fromFile("app/controllers/."+file+"/Application.scala").getLines){                var lineNewApp=line.toString.replaceAll("views.html.","views.html."+file.repla                                          ce("-","_")+".").replaceAll("Application",fileName).replaceAll(fileName+".email","Application.e                                          mail")            //拷贝“controllers/.项目名/Appllication.scala“到“/controllers/处理过的项目名如            //Baiducom.scala”,并把厘里面的的view.html替换成views.html.项目名如baidu_com,本            //工程尽量用_而不是-                    outputApp.write(lineNewApp+"\r\n")(scalax.io.Codec.UTF8)                    print(".")                }                println()            }            print("正在生成新的routes文件")            fileLines+="# For contact us"            fileLines+="POST    /pricing                     controllers.Application.postMsg"            fileLines+="GET     /email                       controllers.Application.email"             for(fileLine<-fileLines){                output.write(fileLine.toString+"\r\n\r\n")(scalax.io.Codec.UTF8)                print(".")           }            output.write("GET /assets/*file controllers.Assets.at(path=\"/public\", file)")            println()       }}

具体步骤如下


运行原来的项目,确保能正确运行

cd [原项目路径] sudo sbtrun

访问http://localhost:9000


将原项目的routes文件链接到新项目中的/conf/route/文件夹下
ln -s [原项目路径]/conf/routes [新项目路径]/[项目名,例:dm6467_cn,注意下划线,不是减号]
将原项目的views文件夹链接到新项目的views文件夹下
ln -s  [原项目路径]/app/views/ [新项目路径]/[项目名,例:dm6467_cn]
将原项目中的controllers文件夹链接到新项目的controllers文件夹下
ln -s  [原项目路径]/app/controllers/ [新项目路径]/[项目名,例:.dm6467_cn,注意文件夹名前面的点号,可保证不编译,避免不必要的错误麻烦]
运行新项目
sudo sbtrun
访问新项目 http://localhost:9000/ 可能出现错误如下Compilation error
not enough arguments for method apply: (p: models.Page)play.api.templates.Html in object index. Unspecified value parameter p.

解决办法:找到/app/views/[项目名称]/index.scala.html(根据提示自行推断),去掉第一行的@(p:Page)

找不到scala文件,请自行将原项目里/app/models/里的相关文件(提示缺少的文件)拷贝或整合到新项目中配置ngnix,添加如下内容
server {        listen 80;        server_name www.[域名].com *.[域名].com [域名].com;        location / {        proxy_set_header  Host  $host;        proxy_pass http://127.0.0.1:9000/[项目名]/;        }}
重启nginx
nano /var/run/nginx.pid(此步骤为查看nginx主进程号,记住打开文件里的数字,然后关闭文件)
kill -QUIT <主进程号>(其中<主进程号>为上一步骤中记住的数字)
sudo nginx
访问域名www.[域名].com如果出现问题,新项目初始化的方法为
轻度初始化:删除"conf/routes"文件,将"routes.bak"复制一份,粘贴为"routes"
强力初始化:在上一步的基础上,删除"app/controllers/"里面的"<首字母大写项目名>.scala"的文件
完全初始化:在上两步的基础上,删除"app/controllers/"里面的".<项目名>"的文件夹,删除app/views/里面的"<项目名>"的文件夹

热点排行