Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling into Play framework app from the Scala console

I have a Play Framework 2.3 app. I can drop into a Scala console with activator console. However, when I try to call into code from my app, specifically some helper function which uses WS, which uses the implicit import play.api.Play.current to retrieve the currently running app, I get the error message java.lang.RuntimeException: There is no started application.

What steps do I have to take to be able to load my app into the current console session?

There is a similar existing question, but the accepted answer appears to be using a mock app from the framework's test helpers. Preferably, I would like to run in the context of my actual app. If I must use a fake app, would it be possible to make it match my development environment (what I get when running activator run) rather than my test environment (what I get when running the unit tests)?

Thanks in advance!

like image 584
Ming Avatar asked Mar 25 '15 07:03

Ming


People also ask

How do I run a play framework?

To run a Play application: Create a new Run Configuration – From the main menu, select Run -> Edit Configurations. Click on the + to add a new configuration. From the list of configurations, choose “sbt Task”

What is play framework in Scala?

Play Framework makes it easy to build web applications with Java & Scala. Play is based on a lightweight, stateless, web-friendly architecture. Built on Akka, Play provides predictable and minimal resource consumption (CPU, memory, threads) for highly-scalable applications.

How do I run a debug in play framework?

To debug, start your application with activator -jvm-debug 9999 run and in Eclipse right-click on the project and select Debug As, Debug Configurations. In the Debug Configurations dialog, right-click on Remote Java Application and select New. Change Port to 9999 and click Apply.


1 Answers

In this specific case you can just create an Application instance and use it instead of the implicit one:

// Tested in 2.3.7
import play.api.{Play, Mode, DefaultApplication}
import java.io.File
import play.api.libs.ws.WS

val application = new DefaultApplication(
    new File("."),
    Thread.currentThread().getContextClassLoader(),
    None,
    Mode.Dev
)

import scala.concurrent.ExecutionContext.Implicits.global

WS.client(application).url("http://www.google.com").get().map((x) => println(x.body))
like image 109
Salem Avatar answered Sep 27 '22 18:09

Salem