I just want to update this thread.

I liked Jonathan's idea about making path_prefix accept a regex but in 
trying to implement that idea 
I decided on a slightly different solution. The new solution adds 
`prefix_match` as a new router param. 

routers = dict(
    ...
    BASE=dict(
        ...
        prefix_match=r'^/_([^/]+)/([^/]+)'  # '^/' is not required
        ...
    ),
)

Then request.prefix_match is set to a List() which will contain the full 
match as the first item and if 
there are any groups they are set as additional elements.

Using the regex above:

# http://127.0.0.1:8000/_my_path_group_1/my_path_group_2/a/c/f

request.prefix_match(0) == '/_my_path_group_1/my_path_group_2'
request.prefix_match(1) == 'my_path_group_1'
request.prefix_match(2) == 'my_path_group_2'
request.prefix_match(3) == None


# http://127.0.0.1:8000/a/c/f

request.prefix_match(0) == None

This makes it work like request.args. If the URL doesn't have a match, then 
the path_prefix is not 
set and will generate errors as normal e.g. invalid function etc.

This just seemed to make better sense to me as it leaves `path_prefix` as 
is with the only caveat being 
if `prefix_match` is set, then it overrides any `path_prefix` that happens 
to be set.

Thoughts?

@Massimo
I haven't created a ticket yet as I was reworking the original idea.


On Saturday, December 14, 2013 6:02:22 PM UTC-5, Rob Mayhue wrote:
>
> I like this idea better.
>
> It took me a little while to figure out exactly what was going on in 
> rewrite.py to even arrive at my
> current solution, but I can see that what you suggest would be a better 
> solution as well as easier 
> to use.
>
> I chose the env var because I knew could easily get to the value in the 
> app using request.env.my_env_variable
>
> @Massimo
>
> I'll open a ticket with the general idea and mention this thread.
>
>
> On Saturday, December 14, 2013 5:37:19 PM UTC-5, Jonathan Lundell wrote:
>>
>> On 14 Dec 2013, at 2:23 PM, Massimo Di Pierro <massimo....@gmail.com> 
>> wrote:
>>
>> Please open a ticket about this proposal.
>>
>>
>> I wonder if a more general solution might not be to have the router 
>> accept a regex for path_prefix, strip it from the incoming URL, and save it 
>> (say) as request.path_prefix. The outgoing URL rewriter would use 
>> request.path_prefix in preference to the value in the router.
>>
>> The point of doing it that way would be to avoid a dependency on the host 
>> webserver.
>>
>> The original use case, iirc, was to support installations in which the 
>> domain was shared between web2py and something else, so you could for 
>> example have URLs of the form http://domain.com/web2py/app/....
>>
>>
>> On Saturday, 14 December 2013 15:07:02 UTC-6, Rob Mayhue wrote:
>>>
>>> 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)
>>
>>
>>
>>
>>

-- 
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