Question

Would it be feasible to run multiple sites off a single Heroku app? The reason I ask is because launching multiple Heroku apps when they all reuse the same components for the most part seems silly and unnecessary. It also makes sense to share the same Postgresql database since Django seems to support that kind of behavior quite well out of the box with its "sites" framework.

As an example of what I'm trying to achieve, let's say I have a single Heroku app. On that app, I have a bunch of Django applications like "blog", "survey", "forums", etc. However, I have two projects named "Project A" and "Project B" which both are using many of the same apps and the same Postgresql database.

Would this be possible without hackish solutions?

Était-ce utile?

La solution

The general principle is to have multiple domains, or sub-paths, pointing to the same back-end code. E.g.

 http://customer1.myapp.com/
 http://customer2.myapp.com/

or

http://myapp.com/customer1/
http://myapp.com/customer2/

You then have your application code use the URL (hostname, path, etc) to make decisions about what content to show. One popular approach with PostgreSQL is to SET search_path to reflect the currently active customer and have clones of all the customer-specific tables. Personally, I think it's rather cleaner to instead add a WHERE clause term that filters rows by customer; this avoids all the duplicate table definitions and the resulting hassle when making DDL changes, and it lets you partition tables in more flexible ways.

If you're doing it with search_path you've got to watch out for application-level caches that might not understand that the customer_user table it's looking at now isn't the one it saw last request, for a different customer. This is another good reason to use WHERE clause based partitioning instead.

I suggest searching for information on "django multi-tenancy" or "django multi-tenant" for the Django-specific bits; I really do the database backend side and a bit of general application front-end work in a variety of tools (not currently including Django).

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top