Grails static resource unacessible from root - grails

I have a file named
favicon.ico
in the web-app folder (grails 2.2.1).
My app is configured to run as root app /
If I open the url
http://mydomain/favicon.ico
I got a not 404 not found.
If I try to move the file in any subfolder it works. For example if I move it to
web-app/test/favicon.ico
and I open
http://mydomain/favicon.ico
the file is correctly served.
The problem is the same if I run the app with grails run-app

You can configure in the Config.groovy to process resources in / like this:
grails.resources.adhoc.patterns = ['/*','/images/*', '/css/*', '/js/*', '/plugins/*']
http://mydomain/favicon.ico
will get a 302 to:
http://mydomain/static/favicon.ico
That should work, but I think its better to move your ico to /images

Related

Grails 5 application is not finding GSP files in .war, grails run-app is fine

I have an application that runs perfectly using grails run-app but fails when I try to run is using the embedded server. I use the following commands to build and run the .war.
./gradlew assemble
java -Dgrails.env=dev -jar build/libs/rideshare-services-3.0.war
Below is the error and the steps I've taken to try to determine what the issue is.
The browser shows 500 error page, and the log shows the following:
Servlet.service() for servlet [grailsDispatcherServlet] in context with path [] threw exception [Could not resolve view with name '/index' in servlet with name 'grailsDispatcherServlet'] with root cause
javax.servlet.ServletException: Could not resolve view with name '/index' in servlet with name 'grailsDispatcherServlet'
at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1384)
at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1149)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1088)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:964)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:898)
<snip>
However, spring security is enabled, and it renders its form fine with this path:
/login/auth
So I started poking around and assets can be accessed, for example:
/assets/advancedgrails.svg
Also, the built-in actuator paths all work fine:
/actuator/*
I found this question
grails and debugging UrlMappings which seemed like it could help.
To test if the URL mapping were working I added a /test route in UrlMappings.groovy, mapped to a controller named 'member' and the 'index' action.
I also added the logging as suggested:
<logger name="org.grails.web.mapping" level="ALL"/>
It resolved to the correct controller and action, but still failed to load the page, so I added a log statement to the action, and the controller definitely runs, but the view fails to load.
At which point, with the debug statement in the controller, I could confirm that even without the /test URL mapping, the controller fires if I use the standard controller/action URI /member/index. That confirmed that the configuration in UrlMapping.groovy was working.
So, thinking the .gsp files failed to be compiled, I searched and found this:
Grails 3 application works locally, but unable to resolve view when deployed to a remote Tomcat
But I already had the plugins suggested in the answer in my build, so I figured maybe they weren't be added to the .war so I unziped it.
The .war has the gsp files as well as a mapping file WEB-INF/classes/views.properties which included a mapping for the view that corresponds to the controller/action I was using to test (among all of the other mappings in the file):
/WEB-INF/grails-app/views/member/index.gsp=gsp_rideshare_services_memberindex_gsp
I then confirmed the referenced gsp_rideshare_services_memberindex_gsp files exist, and the do with the following being present WEB-INF/classes:
gsp_rideshare_services_memberindex_gsp_html.data
gsp_rideshare_services_memberindex_gsp_linenumbers.data
gsp_rideshare_services_memberindex_gsp.class
gsp_rideshare_services_memberindex_gsp$_run_closure1.class
gsp_rideshare_services_memberindex_gsp$_run_closure1$_closure3.class
gsp_rideshare_services_memberindex_gsp$_run_closure1$_closure3$_closure4.class
gsp_rideshare_services_memberindex_gsp$_run_closure2.class
gsp_rideshare_services_memberindex_gsp$_run_closure2$_closure5.class
gsp_rideshare_services_memberindex_gsp$_run_closure2$_closure6.class
gsp_rideshare_services_memberindex_gsp$_run_closure2$_closure7.class
The compiled .gsp files for all of my views seem to be here, and the source .gsp files are also in WEB-INF/classes, in the same folder structure as the project's grails-app/views directory.
So at this point I'm at a loss to understand why my views (gsp files) are not being found, although built-in views and plugin views are fine.
I will reiterate that the app runs perfectly well when run using 'grails run-app'.

Webpack and assets/dist folder

I'm using Angular5 and webpack for generate the dist folder.
For generating the assets folder i'm using the "copy-webpack-plugin". It copies the assets folder from /src/assets into /dist folder.
The problem is: all the path used by a admin console template of my src code uses relative paths starting from "assets/..." and the server include /dist/assets. All static resources will throw 404 not found.
What is the best way to include an assets folder into a dist folder?
I would recommend using the file-loader designed for this very purpose.
Once you have it configured in your webpack.config.js you can import paths to your files for use in JavaScript, as well as HTML templates if you're also using the html-loader.
const myImageUrl = require('./path/to/an/image.png')
doSomething(myImageUrl)

Grails Serve Static Contents from WebServer from ApplicationResources.groovy

We have a web server for static files (css/js/images). We obtain the war from the Grails command and delete the static web-app contents from application (in build.xml). I thought resources will be redirected to static/js/ or static/css/ but it complains that js/ or /css/ cannot be found.
We don't want to have duplicates under application and web-server. How to force Grails to look at external directory in ApplicationResources.groovy?
The Web server overrides static contents only if files are already present under Application.
Use below configuration in Config.groovy:
grails.resources.work.dir = "path/for/static/assets/in/web/server"
Refer the configuration page of resources plugin for details. However, in future, if you decide to migrate to use asset-pipeline plugin instead of resources plugin then use below configuration:
grails.assets.url = "path/for/static/assets/in/web/server"
Refer docs for details.

How does grails know where to look for property files and can I overried this location?

When I deploy my grails generated war file to jetty I get the following error. My question is how de we tell grails where to look for this config file? It works fine for grails run-app because its run from project base where this file exists but gives this error when running withing jetty.
org.codehaus.groovy.grails.commons.cfg.ConfigurationHelper - Unable to load specified config location file:./grails-app/conf/something-config.properties
If you're bundling the file in with the war, then putting it in grails-app/conf should put it in the root of the classpath. Move it to src/java if it doesn't, since that will definitely work both with run-app and in a war. Then register it as an external config in Config.groovy:
grails.config.locations = ["classpath:something-config.properties"]
If you want to deploy it separately from the war (for example to have one war that works in multiple deployments each with its own config file) then you would make the same change to Config.groovy, but copy it to somewhere in Jetty's classpath. I'm not that familiar with Jetty, but I know that Tomcat's lib dir is in its classpath, so I put files like this there. I assume there's an analagous location for Jetty where you can put jars and resources that should be loaded.
You can use externalized configurations
It's a good question. My Config.groovy has these lines commented out. I wonder if yours has a special external config you are trying to read:
// locations to search for config files that get merged into the main config
// config files can either be Java properties files or ConfigSlurper scripts
// grails.config.locations = [ "classpath:${appName}-config.properties",
// "classpath:${appName}-config.groovy",
// "file:${userHome}/.grails/${appName}-config.properties",
// "file:${userHome}/.grails/${appName}-config.groovy"]
// if(System.properties["${appName}.config.location"]) {
// grails.config.locations << "file:" + System.properties["${appName}.config.location"]
// }

How to get the root path of a pylons website

I'm using pylons to build websites. I want to write some files to a directory under the root of the project, but I don't konw how can I get the root path(project path) in the controller?
Try this:
from pylons import config
print config['pylons.paths']['root']

Resources