kes
kes

Reputation: 6177

How to detect application mode in Play 2.x

From within a Play 2.1 application, how would I programmatically determine which mode the application is running in (i.e., Development vs. Production)?

For example, it would be useful to be able to do something like this from inside a template:

<p>@if(__some_play_API_call__ == Dev) { <b>Development mode</b> }</p>

In the Play 2.0 API documentation, there appears to be a mode property of the play.api.Application class... however, I am unsure about how to get at the instance of the currently running application.

Upvotes: 34

Views: 14410

Answers (6)

pme
pme

Reputation: 14803

With Play 2.5, Play 2.6 and Play 2.7

You can do it like this:

import play.Environment

class MyController @Inject()(env: Environment) {

  println(s"DevMode is ${env.isDev}")
  println(s"ProdMode is ${env.isProd}")
  println(s"TestMode is ${env.isTest}")

}

Or in Play 2.6 and Play 2.7 you have also the version with play.api.Environment:

import play.api.Environment

class MyController @Inject()(env: Environment) {

  println(s"ProdMode is ${env.mode == Mode.Prod}")
  println(s"DevMode is ${env.mode == Mode.Dev}")
  println(s"TestMode is ${env.mode == Mode.Test}")
}

For both the Scala Doc states:

/**
 * The environment for the application.
 *
 * Captures concerns relating to the classloader and the filesystem for the application.
 */

Upvotes: 13

Jonik
Jonik

Reputation: 81792

In Play 2.6, inject an Environment instance and use its mode field: one of play.api.Mode enum values.

import javax.inject.Inject
import play.api.Environment
import play.api.Mode.Prod
import play.api.mvc.{AbstractController, ControllerComponents}

class TestController @Inject()(cc: ControllerComponents, env: Environment)
    extends AbstractController(cc) {

  def hello = Action {
    if (env.mode == Prod) {
      // ...
    }

    Ok(s"Hello world in ${env.mode} mode")
  }

}

At least in Play 2.6.20, the methods env.isDev, env.isProd, etc, mentioned by pme, no longer work.

Upvotes: 0

koppor
koppor

Reputation: 20531

In Play 2.5.x the play.Play.isDev() method is deprecated, one has to use dependency injection:

import javax.inject.Inject;

public class Example {

    @Inject
    private play.Environment environment;

    public void myMethod() {
        if (environment.isDev()) {
          ...
        }
    }
}

Or equivalently in Scala:

class ErrorHandler @Inject()(environment: Environment) {

  def myMethod() = {
    if (environment.isDev) {
      ...
    }
  }

}

environment.isDev() returns a Boolean, which one can easily pass to a template. No need to use implicit variables as described here.

Upvotes: 19

Juraj
Juraj

Reputation: 6638

In Play 2.5 using Scala there is a context.environment.mode value of Enumeration from play.api.Mode with one of the values Dev, Test, Prod.
For compile time dependency injection you have context available in your app loader and if you extend BuiltInComponentsFromContext then you can use (inject) directly environment.mode

Upvotes: 5

Gus
Gus

Reputation: 4517

In play 2.3.X you can also check via:

play.Play.isProd()
play.Play.isDev()
play.Play.isTest()

Upvotes: 10

maxmc
maxmc

Reputation: 4216

You can access the current Appliction via

play.api.Play.current()

to find out the mode try

play.api.Play.current().mode()

or simply use

play.api.Play.isDev(play.api.Play.current())

Upvotes: 49

Related Questions