Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Play! Framework: Assets not accessible in Production Mode

So I researched and found out that Play's Production mode has a different behavior when dealing with assets than it does in Development mode.

I have a site where the user uploads an image and the image is displayed immediately on the page after the upload. In development mode, the image displays fine. But in production mode, the image could not be found. I understand that in production mode, the code hasn't recognize the new file written.

A user with a similar issue wrote a solution but it is in Scala. I am writing in Java and don't know exactly what this user's solution is doing: Play! Framework: File not served after upload until play clean

It seems as though this solution is serving the file as a download? Because if it is, it's not what I need. I want to access the file to display it using html like this:

<img src='@routes.Assets.at("images/fileName")'></img>

What can I do to access newly uploaded asset file in Production mode?

like image 378
cYn Avatar asked Aug 12 '13 20:08

cYn


1 Answers

So finding out that you can't serve newly added files after Play has compile the code, the next best option is to use a front-end HTTP server like many have suggested. I ended up using nginx. Since I am only using nginx to serve files and nothing else, I'll post the steps that I took to get this to work.

I am using Mac so I installed Homebrew by calling $ brew install wget in the terminal. Homebrew is good because you can install nginx by simply calling $ brew install nginx

I used this site to guide me through on how to modify nginx's config file: http://learnaholic.me/2012/10/10/installing-nginx-in-mac-os-x-mountain-lion/

Then I simply modify the config file from

location / {
    root   html;
    index  index.html index.htm;
}

to

location / {
        root   /Users/myName/playProject/public;
    }

where public refers to the folder in the Play project. I did it this way so that I don't have to rearrange my code. So now instead of

<img src='@routes.Assets.at("images/fileName")'></img>

I use

<img src='http://localhost:8080/images/fileName'></img>

Where port 8080 is my nginx server

like image 167
cYn Avatar answered Oct 21 '22 02:10

cYn