Running one Play instance for several hosts

As soon as you need multi tenancy support for your application, chances are high that every tenant will also run on its own host, such as http://firsttenant.yourapp.com and http://secondtenant.yourapp.com. For example, this way you can customize the application a little bit and the user who wants to log in does not have to supply any tenant information.

The source code of the example is available at examples/chapter7/virtualhost.

How to do it...

Put the following in your routes file:

GET    {tenant}.mysoftware.com/         Application.index

From then on you can use the client variable in your controller methods:

public static void index(String tenant) {
    renderText(tenant);
}

You can even configure a different handling of CDN-based content for development and production right, use mode in your routes file. Write the following in your routes file:

#{if play.Play.mode.isDev()}
   GET /public/ staticDir:public
#{/}
#{else}
   GET assets.myapp.com/ staticDir:public
#{/}

In order to access the content, you need to use the @@ controller call to resolve to the absolute URL when templates are rendered:

<img src="@@{'/js/foo.js'}">

How it works...

If you want to test the first explained feature, you can use cURL like this and add your own host header, which is used to set the virtual host being used in HTTP 1.1:

> curl --header "Host: test.example.com" 127.0.0.1:9000/

test

There's more...

Hosting an application supporting multiple tenants on the same host for different tenants is generally not a good idea except those low in traffic and you do not have strict security constraints on the data your are storing. Think about whether it is a good idea to add such a complexity to your application, or if splitting systems would just make more sense. Maintenance of course might be significantly reduced, if you only need to handle one instance.

Implementing multi tenant applications using hibernate

Erik Bakker from Lunatech Research has written an excellent write up on how to use hibernate filters to implement multi tenancy with Play. Check it out at http://www.lunatech-research.com/archives/2011/03/04/play-framework-writing-multitenancy-application-hibernate-filters.

A virtual hosting module

There is a vhost module included in the Play framework modules list. However, it suffers from the lack of documentation as it includes JNI based libnotify code, which is not guaranteed to run on every operating system. This module allows you to have a directory with subdirectories, which resembles virtual hosts including static files, but also allows each domain to have its own datasource.

Using properties to be more flexible

As you can see in the preceding routes file, a hardcoded hostname might not be a problem, but still is somewhat inflexible. The great thing is that the conf/routes file is preprocessed by the template engine, so there is absolutely no problem to use an expression like:

GET    {tenant}.${play.configuration['host']}/         Application.index

And adding this to your application.conf:

host=example.com

Just retry cURL call in the last section and check whether it works.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.21.246.223