Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

play framework route that matches all

I'm working on an angular app using play framework for my rest-services. Everything in the public folder is an angular app (stylesheets, javascripts, images and html). I want every request that is not for something in the stylesheets, javascripts, templates or images folder to be routed to the index.html page. This is so that angular routing can take over from there...

As a side note i can mention that I am going to place every restservice under /services/ which links to my own java controllers.

Is it possible in play framework 2.3.4 to define a route that catches all without having to use the matching elements?

This is my route defs so far:

GET     /                       controllers.Assets.at(path="/public", file="index.html")
GET     /stylesheets/*file      controllers.Assets.at(path="/public/stylesheets", file)
GET     /javascripts/*file      controllers.Assets.at(path="/public/javascripts", file)
GET     /templates/*file        controllers.Assets.at(path="/public/templates", file)
GET     /images/*file           controllers.Assets.at(path="/public/images", file)

#this line fails
GET     /*                      controllers.Assets.at(path="/public", file="index.html")
like image 570
Runar Halse Avatar asked Sep 05 '14 13:09

Runar Halse


1 Answers

It's not possible to omit usage of matching elements but you can route a client via controller. The route definition looks like this:

GET         /*path               controllers.Application.matchAll(path)

And the corresponding controller can be implemented as follows:

public class Application extends Controller {

    public static Result matchAll(String path) {
        return redirect(controllers.routes.Assets.at("index.html"));
    }

}

Update

If you don't want to redirect a client you can return a static resource as a stream. In this case a response MIME type is required.

public class Application extends Controller {

    public static Result matchAll(String path) {
        return ok(Application.class.getResourceAsStream("/public/index.html")).as("text/html");
    }

}
like image 194
Daniel Olszewski Avatar answered Oct 14 '22 22:10

Daniel Olszewski