简体   繁体   中英

Multi-tenancy App using Flask and App engine

I'm implementing Multi-tenancy app using Flask and App Engine. I'm using subdomain as my namespace(App engine's namespace). Below is my design:

When user access test.domain.com, I will check whether subdomain 'test' is in SudDomain model, which is as shown below:

class SubDomain: # subdomain stored as id
    pass 

If 'test' is there in SubDomain, user will be redirected to login page of the app and current namespace of the app is set to 'test'.Otherwise, registration page is shown.

How do I implement it in both Production and Development environment? I would like to do as follows:

In production:

Using before_request decorator, I would do:

from urllib2 import urlparse
from flask import request

url_info = urlparse.urlparse(request.url) # Say, http://test.domain.com
subdomain = url_info.netloc.split(".")[0]

This subdomain can be checked against SubDomain model and will follow the remaining steps.

In development:

I don't know how to implement this scenario. What I'm thinking is: Requests will be in the form of http://localhost:8888/test/list/ , here 'test' is considered as subdomain. Which somehow in before_request decorators will be removed so that modified request would look like http://localhost:8888/list/ and this will lead to proper endpoint. Is it possible to do in this way in flask?

Then, the extracted subdomain - 'test' will be checked in SubDomain model.

Am I doing it in the right way? Is there something wrong in my method especially the one in local development environment?

BTW, its my first multi-tenancy app, please bear with me.

Thanks in advance..

I have done something similar to what you did here but in the end it was more work than simply adding things to your /etc/hosts file.

The first try that I did was like yours and implemented by changing the router depending on the environment. Then the reverse route builder creates it easily, but as it grows you'd have to support twice the routes for dev/live which is easy to do but just not worth the trouble.

So just modify your hosts file add the domains you want to test.

# on Linux/Unix at /etc/hosts
127.0.0.1 www.example.dev sub1.example.dev sub2.example.dev

# on windows its in system32\drivers\etc\

If you are using linux/mac you can also install dnsmasq where you can simply define a whole extension for your dev work. For example everything .dev on mine is pointing to localhost like www.example.dev

This way you would just have to make a variable for your domain route from .com -> .dev on production and development.

Then you can start accessing your appengine dev servers at http://www.example.dev:8888/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM