librelist archives

« back to archive

different domain for a url shortener within larger application

different domain for a url shortener within larger application

From:
Stephen Fuhry
Date:
2014-11-05 @ 04:39
I'm working on an application which has its own url shortening service as
an internal feature.

Ideally, i'd like to do something like:

@app.route('/<path:path>', host='my-shor-turl-domain.com')
def short_url(path):
   redirect(lookup_url(path))

However, this only works if I set:

app.url_map.host_matching = True

This solution seems like overkill, though. with host_matching, I have to
set host='*.mysite.com' for every other view.

All I really want to say is:

if request.headers['Host'] == 'myshortdomain.com':
  # direct all requests to url_for('short_url')
else:
  # pretend I never asked

The alternatives seem to be:

   1. Run a completely separate instance of the application (would like to
   avoid the extra overhead and management)
   2. Run some sort of application dispatcher
   <http://flask.pocoo.org/docs/0.10/patterns/appdispatch/> (similar to the
   posted Dispatch by Subdomain solution) that switches applications based on
   domain (possibly most sane solution, however it does require storing two
   application instances in memory, which seems unnecessary)
   3. Namespace all paths of everything in my application, i.e. /home
   becomes /not-the-shortener/home (obviously this would be silly)


Is there a simpler way to accomplish what I'm trying to do? Does anyone
have any recommendations on what the easiest way to do this is?

Re: different domain for a url shortener within larger application

From:
Stephen Fuhry
Date:
2014-11-05 @ 14:44
Realized this is easier to solve by just doing a URL rewrite, i.e. in nginx:

rewrite ^/(.*) /shorturl/$1 break;



Thanks!

On Tue, Nov 4, 2014 at 11:39 PM, Stephen Fuhry <fuhrysteve@gmail.com> wrote:

> I'm working on an application which has its own url shortening service as
> an internal feature.
>
> Ideally, i'd like to do something like:
>
> @app.route('/<path:path>', host='my-shor-turl-domain.com')
> def short_url(path):
>    redirect(lookup_url(path))
>
> However, this only works if I set:
>
> app.url_map.host_matching = True
>
> This solution seems like overkill, though. with host_matching, I have to
> set host='*.mysite.com' for every other view.
>
> All I really want to say is:
>
> if request.headers['Host'] == 'myshortdomain.com':
>   # direct all requests to url_for('short_url')
> else:
>   # pretend I never asked
>
> The alternatives seem to be:
>
>    1. Run a completely separate instance of the application (would like
>    to avoid the extra overhead and management)
>    2. Run some sort of application dispatcher
>    <http://flask.pocoo.org/docs/0.10/patterns/appdispatch/> (similar to
>    the posted Dispatch by Subdomain solution) that switches applications based
>    on domain (possibly most sane solution, however it does require storing two
>    application instances in memory, which seems unnecessary)
>    3. Namespace all paths of everything in my application, i.e. /home
>    becomes /not-the-shortener/home (obviously this would be silly)
>
>
> Is there a simpler way to accomplish what I'm trying to do? Does anyone
> have any recommendations on what the easiest way to do this is?
>