0
votes

I have 3 route files, namely web.route, admin.routes and swift.routes respectively. Each of this routes file is represent a subdomain and should be changed based on the URI subdomain path. Play Java pls!

public Handler onRouteRequest(RequestHeader requestHeader) {
    String host = requestHeader.getHeader(Http.HeaderNames.HOST);
    Pattern pattern = Pattern.compile("^([a-z0-9]+)\\.([a-zA-Z0-9_%*:.]+)");
    Matcher matcher = pattern.matcher(host);
    if (matcher.matches()) {
        String subDomain = matcher.group(1);
        if (subDomain.equalsIgnoreCase("swift")) {
            //use swift routes file
        } else if(subdomain.equalsIgnoreCase("admin")) {
            //use the admin routes file
        } else {
            //use the web route for all other subdomain or non-subdomain
        }

    }
    return super.onRouteRequest(requestHeader);
}

I have seen number of question online that discuss about this, but none was detailed enough and perhaps this is play framework 2.4 all the one I have seen are play 1 and play scala. I also understand the best approach to get this done is via SbtProject https://www.playframework.com/documentation/2.2.x/SBTSubProjects However I don't have the luxury to go through that approach. Any help will be appreciated.

1

1 Answers

1
votes

I think you have two problems here.

The first one is how can one use mutliple routes files without actually using subprojects: it's actually very simple, please see this question Play Framework: split routes in multiple files without sub projects. As you can see, you can split a route file by using a url prefix.

The second one is how can one make use of subdomains in Play Framework for routing purposes. AFAIK, Play Framework 2.x doesn't support subdomains out of the box. So subdomain1.mydomain.com/action/hello and subdomain2.mydomain.com/action/hello will be treated equally. What you can do however is some url rewriting. The goal is to transform subdomain1.mydomain.com/action/hello into an URL with a prefix, something the framework can route, such as mydomain.com/subdomain1/action/hello.

I know that you can do this by using an http server that support url rewriting such as Nginx (see the Play documentation HTTP Server, and Url rewrite module). But maybe you can do this directly in Play in your onRouteRequest method.

I hope it helps, cheers.