Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading Files from public folder in play framework in production

My play app is in 2.4.2. In Developer Mode,I used to read files from public folder at back-end controllers using Source.fromFile("./public/files/abc.json")

When I converted the application to production mode, I am getting File Not Found Exceptions. I found that the public folder is packed in an assets jar in production mode.What can i do so that it works in both development and production mode??

like image 584
user189107 Avatar asked Apr 28 '16 05:04

user189107


2 Answers

You can use this method:

Play.application().getFile("/public/foobar.baz");

Method Doc:

Get a file relative to the application root path.

like image 83
jsonmurphy Avatar answered Oct 14 '22 06:10

jsonmurphy


Have you tried out this Play Documentation https://www.playframework.com/documentation/2.5.x/AssetsOverview ? This is Ok with Play 2.4 and even with 2.3 also, I have tried out.

In there you can find this, you can simply do this in your conf/routes file.

GET  /assets/*file        controllers.Assets.at(path="/public", file)

file -> refers to the file name. Ex: myFile.json

To make this work in production mode you have to do some little more work. As explained in this answer, add these lines into your /build.sbt file.

import com.typesafe.sbt.packager.MappingsHelper._
    mappings in Universal ++= directory(baseDirectory.value / "public")

That would include your 'public' directory inside the dist file (You can include any directory like that way). Then your application would work in the production environment.

like image 20
Supun Wijerathne Avatar answered Oct 14 '22 06:10

Supun Wijerathne