Proposal - use the value of an environment variable as a path_prefix

When using the parametric router there is an option called `path_prefix` 
that's 
described as "a path fragment that is prefixed to all outgoing URLs and 
stripped 
from all incoming URLs". I'm not sure what the original use case was for 
this option 
but the "path fragment" is a static value, a string that's added to BASE in 
routes.py 
like so:

routers = dict(
    ...
    BASE=dict(
        ...
        path_prefix='PATH_FRAGMENT',  # or even 'PATH/FRAGMENT'
        ...
    ),
)


This allows things like 'http://domain-name.tld/PATH_FRAGMENT/a/c/f'

In my use case I wanted the simplicity of the parametric router but I 
needed to be 
able to pass in the path fragment on the incoming URL so that it could 
change and 
still be "prefixed to all outgoing URLs and stripped from all incoming 
URLs", or 
possibly not even exist at all.

I made some changes to gluon/rewrite.py to allow the value of an 
environment variable 
to be used as the path fragment if it exists. To use it I prefix the string 
assigned 
to `path_prefix` with a '$' like this:

routers = dict(
    ...
    BASE=dict(
        ...
        path_prefix='$MY_ENV_VARIABLE',
        ...
    ),
)


Leaving off the '$' causes `path_prefix` to work normally.

Then in the server config I use a rewrite rule and get the path fragment 
via a 
regex and create the environment variable. The following is an apache 
rewrite rule, 
but this could be nginx etc as well. This rule gets anything starting with 
an 
underscore (e.g. http://domain-name.tld/_MY_VALUE).

RewriteRule ^/(_[^/]+) - [E=MY_ENV_VARIABLE:$1]


After that we have an environment variable with 'MY_ENV_VARIABLE' as its 
key and 
'_MY_VALUE' as its value.

Basically the changes I made to gluon/rewrite.py allow the value of that 
environment 
variable to be used as the path fragment allowing you to change it by 
passing in a new 
path fragment on the URL and it acts just like the original `path_prefix` 
option.

In my use case I'm planning to use the "path fragment" for multi-tenancy. 
Maybe there 
is a better way to do this using the pattern based router and I just didn't 
see it. 

I wanted to toss this onto the list and get some feedback, and if there is 
an easier 
way, be shown the light :)

-- 
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
--- 
You received this message because you are subscribed to the Google Groups 
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to web2py+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to