Scala Play 2.5 Controller class to serve static HTML

387 Views Asked by At

I want to serve a static file from a Scala Play controller. I am looking for something that would allow me to do something like this example below.

NOTE: This obviously does not work. It is very possible that I am look at the problem in the wrong way, I however do NOT want to redirect to the app.html

def loadApplication(): EssentialAction = Action.sync { request =>
  val contents = Assets.contentsOf("/public/assets/app.html") //This doesnot return the contents, but that is what I want
  Ok(contents)
}
1

There are 1 best solutions below

0
On BEST ANSWER

You can just use the Assets and return contents via that. You might have to tweak the path though:

class MyController @Inject() (assets: Assets) extends Controller {

  def loadApplication(): Action[AnyContent] = Action.async { request =>
    assets.at("/public/assets/", "app.html").apply(request)
  }

}

More information may be found in the documentation: https://www.playframework.com/documentation/2.5.x/AssetsOverview#The-Assets-controller

Also note that you can map a route to your assets instead of statically referencing the file from the controller, like so:

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