JOB POST: set up routes for one web2py instance, several top level domains, and two apps per domain

44 views
Skip to first unread message

Lisandro

unread,
Apr 1, 2017, 9:52:54 AM4/1/17
to web2py-users
Originally this was a question, but considering is something I need for my business, I added "JOB POST" to the title. 
If you consider that you know how to achieve what I need, I'm willing to pay for the work. 
Sorry if this is not the place to ask for a web2py developer (in that case, the post is still a question to the group).


To help you understand, first let me tell what I have right now:
  • I have one web2py instance running. 
  • I developed two web2py apps that, together, they allow you to have your website (like a blog) and manage it yourself:
    • the first app is the public website, and right now it is accessible through the top level domain: mywebsite.com
    • one of the apps is the control panel, where you can post/edit new articles, etc, and it is accessible through a subdomain: panel.mywebsite.com
  • I have several websites running, each one with its top level domain.
I got all this working using parameter-based routing:

routers = dict(
    BASE
=dict(
        default_controller
='default',
        default_function
='index',
        domains
={
           
# one website
           
'recipes.com': 'recipes',
           
'panel.recipes.com': 'recipes_panel',
           
# another website
           
'traveler.com': 'traveler',
           
'panel.traveler.com': 'traveler_panel',
           
# ... several more websites ...
       
},
        root_static
=['robots.txt'],
        map_static
=True,
        exclusive_domain
=True,
   
)
)

Note that with exclusive_domain=True, each app is accessible only through one specific domain, and it's not possible to access it from another domain.

However, consider this: what if I want to setup SSL for a website? 
I would have to buy a wildcard SSL certificate, because I have the website divided in two parts, one of them (the control panel) in a subdomain. 
Wildcard SSL certificates are usually more expensive, and I don't want to force that.




Considering all that, here is what I want to achieve:
  • Each website still would be formed by two web2py apps, so in our example, we would still have these four web2py apps: 
    • applications/recipes
    • applications/recipes_panel
    • applications/traveler
    • applications/traveler_panel

  • The public portion of a website would still have to be served in the top level domain, and the default controller and default function would be 'default' and 'index' respectively, so:
  • The control panel (and this is how it gets tricky) would have to be served through /panel (notice that the app name is different), so:
  • Each domain would allow to access only the couple of apps regarding that website, that is:
    • "recipes" and "recipes_panel" apps would only be accessible trough recipes.com domain
    • "traveler" and "traveler_panel" apps would only be accessible trough traveler.com domain


I think I need to use pattern-based routing system, but I've never used python's regular expressions at all. I'm reading about it and doing some tests, but I'm having a hard time to figure out how should I do it.
Remember that I'm willing to pay for the job if you consider that you know how to do it. 

Thanks in advance!
Best regards,
Lisandro.

Lisandro

unread,
Apr 3, 2017, 12:24:28 PM4/3/17
to web2py-users
Well, after a few hours of testing with pattern-based routing system, I've achieved what I was looking for. Oh yeah :D
Here is what I did, in case someone is trying to do the same (continuing the example of the first post).

In the web2py main folder, this is routes.py:

routes_app = [
   
(r'.*?://recipes.com:\w* /$anything', r'recipes'),
   
(r'.*?://traveler.com:\w* /$anything', r'traveler'),
]

Notice that I will serve two apps per domain, but in the routes_app I specify only the main app.
This allows me to define a routes.py for the main app of each domain.

In my case, this is applications/recipes/routes.py:

 ('/', '/recipes/default/index'),
 
('/robots.txt', '/recipes/static/robots.txt'),
 
('/favicon.png', '/recipes/static/custom/favicon.png'),
 
('/download$anything', '/recipes/default/download$anything'),
 
('/static$anything', '/recipes/static$anything'),

 
('/panel/static$anything', '/recipes_panel/static$anything'),
 
('/panel', '/recipes_panel/default/index'),
 
('/panel$anything', '/recipes_panel$anything'),
]

# for every function in the default controller
for key in ('contact', 'other_function', 'aboutus', 'myfunction'):  
    routes_in
.append(('/%s$anything' % key, '/recipes/default/%s$anything' % key))

routes_in
.append(('/$anything', '/recipes/default/index/$anything'))

routes_out
= [(x, y) for (y, x) in routes_in]


Then I have applications/traveler/routes.py, which has the same content that the file before, but replacing "recipes" with "traveler".
And that's it, works like a charm.


Now there is one thing that I couldn't achieve: how to avoid all the apps being accessed from any domain?
I'll close this question and post a new question about this.
Reply all
Reply to author
Forward
0 new messages