简体   繁体   English

Grails运行应用程序从根上下文提供文件

[英]grails run-app serve files from root context

How does one serve up files from the root context when running a grails application with run-app. 使用run-app运行grails应用程序时,如何从根上下文提供文件。 For example, I want to serve up robots.txt file from /robots.txt and serve up my application from /mygrailsapp/ . 例如,我想从/robots.txt提供robots.txt文件,从/mygrailsapp/提供服务我的应用程序。

Does grails run-app use tomcat? Grails运行应用程序是否使用tomcat? How do you configure the tomcat plugin to serve static files from the root context? 如何配置tomcat插件以从根上下文提供静态文件?

Failed paths taken thus far... 到目前为止失败的路径...

1) I found a way to set the project to the root context so everything is served from the root context but this is not want I want. 1)我找到了一种将项目设置为根上下文的方法,以便从根上下文提供所有服务,但这不是我想要的。 http://nickcarroll.me/2009/03/27/configuring-the-grails-root-application-context/ http://nickcarroll.me/2009/03/27/configuring-the-grails-root-application-context/

2) I found an old post (2008) describing how to enable multiple contexts with run-app and jetty. 2)我发现了一篇旧文章(2008),描述了如何通过run-app和jetty启用多个上下文。 Does run-app still use jetty? 运行应用程序是否仍使用码头? http://colinharrington.net/blog/2008/07/grails-jetty-and-crossdomainxml/ http://colinharrington.net/blog/2008/07/grails-jetty-and-crossdomainxml/

Similar to question How to place certain files in the root directory of the Grails server? 类似于问题如何将某些文件放置在Grails服务器的根目录中? but I want a solution that is contained within the grails project. 但是我想要一个包含在grails项目中的解决方案。

Thanks, 谢谢,

Nathan 弥敦道

After some digging it looks like this is easy. 经过一些挖掘,看起来很容易。

First, embedded tomcat can be configured via scripts/_Events.groovy with the eventConfigureTomcat event handler. 首先,可以使用eventConfigureTomcat事件处理程序通过scripts / _Events.groovy配置嵌入式tomcat。 http://roshandawrani.wordpress.com/2011/03/13/grails-tip-configuring-embedded-tomcat-instance-used-in-developmenttest-env/ http://roshandawrani.wordpress.com/2011/03/13/grails-tip-configuring-embedded-tomcat-instance-used-in-developmenttest-env/

Second, you can add another context to serve out files from root. 其次,您可以添加另一个上下文来从根目录提供文件。 http://grails.1312388.n4.nabble.com/Add-virtual-directory-in-grails-tomcat-plugin-td2234448.html#a2234448 http://grails.1312388.n4.nabble.com/Add-virtual-directory-in-grails-tomcat-plugin-td2234448.html#a2234448

import org.apache.catalina.loader.WebappLoader

eventConfigureTomcat = {tomcat ->
    File dir = new File("./scripts/rootContext")
    def rootContext = tomcat.addWebapp("/", dir.getAbsolutePath())
    rootContext.reloadable = true
    rootContext.loader = new WebappLoader(tomcat.class.classLoader)

    //bonus answer - gzip json response
    tomcat.connector.setAttribute("compression", "on")
    tomcat.connector.setAttribute("compressableMimeType", "applicatio/json")
}

start grails with run-app and /robots.txt will get served from scripts/rootContext/robots.txt. 使用run-app和/robots.txt可以从scripts / rootContext / robots.txt获取内容。

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

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