Grails app not loading js and css from cloud front - grails

This has to be some type of configuration issue, but I can't figure out what it is. My grails app when deployed to elastic beanstalk will load my index.gsp and show an image. Now this is a stripped down application with nothing but a single page. The page has one image and I'm trying to load the css/js/image from cloudfront. The image loads fine.
http://d55az6m6nudec.cloudfront.net/images/placeholder.jpg
Css + js do not load at all:
http://d55az6m6nudec.cloudfront.net/js/application.js
But as you can see the js comes from the same cloudfront and the URL is formatted the same. When I inspect the webpage and click on the js or css files, I get redirected through cloudfront and the browser shows it's trying to load this URL:
http://awseb-e-m-awsebloa-1bzuvx2wx2h91-492777361.us-west-2.elb.amazonaws.com/statichttp://d55az6m6nudec.cloudfront.net/js/application.js
So it's got the 'static' prefix in there, and I don't know what's going on. Here are some of the relevant parts of my Config.groovy:
grails.resources.mappers.baseurl.enabled = true
I've tried this next line both commented on/off:
grails.resources.mappers.bundle.excludes = ['**/*']
Here is my base url:
grails.resources.mappers.baseurl.default = "http://d55az6m6nudec.cloudfront.net"
I have tried this on/off:
grails.resources.uri.prefix = ""
I have tried this with/without values:
grails.resources.adhoc.patterns = []
and:
grails.resources.adhoc.patterns = ['/images/*', '/css/*', '/js/*', '/plugins/*']
I'm running out of combinations, and at this point I'm just doing trial and error which is taking forever to figure out what should work. I'm also using a load balancer.

It turns out the problem was I did not have the grails.serverURL variable set for my production environment. Now I'm not sure why this caused the error, but I redeployed with the server set and now the css and js files are loading fine.

Related

OpenLayers: Loading a TileJSON from a local file

I'm trying to implement a local copy of a TileJSON in an iOS app through Cordova. The issue I'm having is that OpenLayers doesn't seem to recognise the JSON file as valid and thus doesn't show any tiles. I have tried local and remote versions of the same TileJSON and looked at the Console Logs, the local one has a status of "error" (but no explanation as to what that error might be...).
I think the issue is down to the fact that the JSON file is being loaded using a file: URL, rather than http:. I have put the JSON file on a remote server and this not only loads fine but actually loads the tiles from the local path.
Can OpenLayers be tricked into accepting the local file as a valid JSON file? Can Cordova be forced to load local files via HTTP? I think either of these options would fix the issue.
Thanks
EDIT: Here's the code I'm using to load the TileJSON:
var mapLayer = new ol.layer.Tile({
source: new ol.source.TileJSON({
url: getPhoneGapPath() + 'tiles.json',
crossOrigin: 'anonymous'
})
});
this.map.addLayer(mapLayer);
function getPhoneGapPath() {
var path = window.location.pathname;
path = path.substr( path, path.length - 10 );
return path;
}
The getPhoneGapPath() function is used to get the path to the webroot of the Cordova app.
This is probably related to a bug in OpenLayers, https://github.com/openlayers/ol3/issues/5647. The fix will be in the next release.
Also make sure that you configure Cordova to allow access to file:// urls when the application is served from a file:// url. The equivalent option in Chrome is --allow-file-access-from-files.

Attach a pdf in asset pipeline using ActionMailer Rails 4

I'm trying to attach a file to an email. The file is in assets/downloads/product.pdf
In the mailer, I have:
attachments["product.pdf"] = File.read(ActionController::Base.helpers.asset_path("product.pdf"))
I've tried:
attachments["product.pdf"] = File.read(ActionController::Base.helpers.asset_url("product.pdf"))
...and even:
attachments["product.pdf"] = File.read(ActionController::Base.helpers.compute_asset_host("product.pdf") + ActionController::Base.helpers.compute_asset_path("product.pdf"))
I always get the same error:
EmailJob crashed!
Errno::ENOENT: No such file or directory - //localhost:3000/assets/product.pdf
...or a variation on the theme. But even when I try using asset_url in the view or just put the url in the browser it works:
http://localhost:3000/assets/product.pdf
I've also tried using straight up:
File.read("app/assets/downloads/product.pdf")
File.read("downloads/product.pdf")
...which works in dev environment but not on staging server (heroku). Error is still:
Errno::ENOENT: No such file or directory - downloads/product-market-fit-storyboard.pdf
Also tried:
File.read("/downloads/product.pdf")
File.read("http://lvh.me:3000/assets/product.pdf")
...don't work at all.
Ideas?
you should use syntex like this.it is work for me may be will work for you also.
File.open(Dir.glob("#{Rails.root}/app/assets/downloads/product.pdf"), "r")
When using mailer, you shouldn't use assets pipline. Asset pipeline would be useful if you wanted to have link to a file inside your email. When rendering an email, action mailer has access to files in app directory.
Please read about attachments in action mailer guide. As you can see, you just need to pass path to a file, not url:
attachments['filename.jpg'] = File.read('/path/to/filename.jpg')

Grails: how to remove AppName from production url

I am trying to deploy Grails 2.2.3 project to a production and having issue with URL.
I setup reverse proxy in Apache and have http://example.com -- as example.
I set following property in Config.groovy:
grails.serverURL = "http://example.com/"
and set in the application.properties following:
app.context=/
However all of my resources and links always reference to
http://example.com/AppName/...
How do I remove application name ("AppName") from resource and links reference?
Example of resources:
<r:require modules="bootstrap"/>
<g:javascript library="jquery"/>
Example of environments Config.groovy:
environments {
development {
grails.logging.jul.usebridge = true
}
production {
grails.logging.jul.usebridge = false
grails.serverURL = "http://example.com/"
grails.app.context = "/"
}
}
I think Grails application must have some sort of property that could be set!? Otherwise it seems like a pain the neck.
Please help!
P.S: If I access any resource directly (manually remove AppName from URL) I get the right stuff such as css, js and image.
Update:
I set following in application.properties:
app.context=/
serverURL=http://example.com/
and created absolute URL in grails:
<g:createLink controller="Sample" absolute="true"/>
and it creates proper URL!!! However resources use relative path and start with AppName/... It seems like a curse, I just can't get rid of that AppName.
Update:
If I run prod run-app then all URLs come out properly, however the moment I deploy to Tomcat 7, AppName appears in URLs again.
There are other options, but probably the shortest approach is to deploy it like ROOT.war.
Just change the default war filename in the BuildConfig.groovy:
grails.project.war.file = "target/ROOT.war"
Set the right grails.serverURL property for you and you're done.
Hope it helps.

Error #2044: Unhandled IOErrorEvent:. text=Error #2036: Load Never Completed

I have a .swf file which throws this error when I attempt to load it in a browser. Similar to other posts here this is the code:
var mySpite:Sprite = new Sprite();
var button:Loader = new Loader();
var url:String = "images/btnImage.png";
var urlReq:URLRequest = new URLRequest(url);
button.load(urlReq);
....
When I run the actionscript in Flex Build it works fine. Also when I double click the .swf file in the folder where it is stored, it works fine. I don't know why it only doesn't work and throws this error when I load it in a .jsp page.
It's most likely an issue with your path. Remember that the path to the image will be relative to the page which embeds the SWF.
In answer to your comment about having to change the paths when deploying, depending on how you're building and debugging your SWF, it might be possible to use Capabilities.playerType (see docs) to choose the correct path for the current environment. Another alternative might be to put all your image references in a couple of XML files (one local and one remote). As long as the path to the XML file is consistent between local and remote environments, you can update the SWF without worrying about the image paths.

Handling all website one-off root static files like favicon.ico reliably and elegantly?

Perhaps I'm missing something (I hope I am!), but it seems awfully clunky to have to have deal with the one-off root website files requested by the browser, such as favicon.ico and things like apple-touch-icon-precomposed.png (on the iPad). Right now, I'm getting a 500 server error whenever I encounter one of these for which I'm not explicitly serving up a file.
My questions:
Is there an up-to-date list of all such files for all major browsers that my webapp should be handling?
As a failsafe, is there way for the absence of any of these files to fail silently, i.e. to NOT get a 500 server error with the webapp continuing on its merry way, simply using a blank favicon or whatever?
I'm running a Django app on Heroku with gunicorn.
I never get a 500 error from invalid urls, because I use STATIC_URL and the class based RedirectView
Usually I have an app dedicated to this, and include it in the root urls.py with
#urls.py
include('oneoff.urls', name='oneoff')`
and
#oneoff/urls.py
urlpatterns = ( '',
url(r'favicon.ico$',
RedirectView.as_view(url=urlparse.urljoin(settings.STATIC_URL, "img/favicon.ico")),
name="favicon"
),
url(r'icon-precomposed.png',
RedirectView.as_view(url=urlparse.urljoin(settings.STATIC_URL, "img/iphone/icon.png")),
name="iphone"
),
)
then in the template
{% load url from future %}
<link rel="favicon" href="{% url 'oneoff:favicon' %} />

Resources