Playframework route file: Separate Production routes from Dev routes
Asked Answered
P

2

25

Is there a way in Play to annotate routes to inform that a certain section/group routes is only available in dev or prod mode

Prune answered 14/7, 2011 at 21:9 Comment(0)
S
37

Well, this is not documented, so I am not sure if this is intentionally possible or not, but I have found a way to make this work. Please note however, as this is an undocumented feature, may mean it is unintended, and therefore may break in future versions of play.

You are able to achieve what you want using the following line in your routes file.

%{ if (play.mode.isDev()) }%

I created a test application with a couple of actions

public class Application extends Controller {

    public static void index() {
        render();
    }

    public static void noDev() {
        renderText("NoDev");
    }
    public static void noProd() {
        renderText("NoProd");
    }
}

I then added the following to my routes file

# Home page
GET     /                                       Application.index

# Ignore favicon requests
GET     /favicon.ico                            404
# Map static resources from the /app/public folder to the /public path
GET     /public/                                staticDir:public

%{ if (play.mode.isDev()) }%
GET     /route1                                 Application.noDev
GET     /route2                                 Application.noDev
GET     /route3                                 Application.noDev
*       /{controller}/{action}                  {controller}.{action}

%{ if (play.mode.isProd()) }%
GET     /route4                                 Application.noProd
GET     /route5                                 Application.noProd
GET     /route6                                 Application.noProd
*       /{controller}/{action}                  {controller}.{action}

So, you can see that using a simple if statement, it will execute the next group of routes only in that mode. The if statement will end when the next if statement is found.

If in Dev mode you try to access route4, you will not be able to access it, and you will see the RouteNotFound page showing that the available routes are those that you have defined for Dev only.

Shaughnessy answered 15/7, 2011 at 6:11 Comment(4)
That is super sweet. I had no idea you could do logic in the route file.Upthrow
I knew you could do some logic, as it was a solution to the war context problem, but wasn't sure how much logic was possible. So I tried it, and it worked!Shaughnessy
You can also use groovy if tags for example #{ if (play.mode.isDev()) } SOME ROUTE #{/if} if you want to end if yourself. :)Trevortrevorr
Just to reinforce @kheraud's comment above: this doesn't work in Play 2.Confounded
P
2

For play framework version 2.x:

  • You need to have another routes file lets say prod.routes in the root of your application (same directoy of original routes file), this file contains only the routes that you want for production.
  • Then you create another .conf file like prod.conf inside conf folder.
  • Now this new conf file must contain the following:

for the play framework 2.4 and newer :

include "application.conf"

play.http.router=prod.Routes

Or without new .conf file pass parameter:

-Dplay.http.router=prod.Routes

And if older than 2.4 then :

include "application.conf"

application.router=prod.Routes

And when you run the production run it with -Dconfig.file=prod.conf

Phyfe answered 15/6, 2016 at 4:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.