librelist archives

« back to archive

Re: [flask] different domain for a url shortener within larger application

Re: [flask] different domain for a url shortener within larger application

From:
Åsmund Tokheim
Date:
2014-11-05 @ 19:02
Hi
I would serve both hosts using Nginx. Whenever someone makes a request to 
the shorturl-host, Nginx can prefix the path with /shorturl and pass it to
Flask (or uwsgi). Then you can serve shorturls using the route 
/shorturl/<path:path> and not have to worry about hosts in the application
code.
Åsmund
Date: Tue, 4 Nov 2014 23:39:51 -0500
From: fuhrysteve@gmail.com
Subject: [flask] different domain for a url shortener within larger application
To: flask@librelist.com

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:Run a completely separate instance of the 
application (would like to avoid the extra overhead and management)
Run some sort of application dispatcher (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)Namespace all paths of 
everything in my a
 pplication, 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: [flask] different domain for a url shortener within larger application

From:
Stephen Fuhry
Date:
2014-11-05 @ 19:20
Thanks, that's exactly what I ended up doing. Tried to reply earlier, but
it didn't get picked up by librelist.

i just made a new vhost in nginx and added this:

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

Effectively makes all the urls for that sitename come in as /foo and
forwards them to /shorturl/foo


On Wed, Nov 5, 2014 at 2:02 PM, Åsmund Tokheim <tokheim@outlook.com> wrote:

> Hi
>
> I would serve both hosts using Nginx. Whenever someone makes a request to
> the shorturl-host, Nginx can prefix the path with /shorturl and pass it to
> Flask (or uwsgi). Then you can serve shorturls using the route
> /shorturl/<path:path> and not have to worry about hosts in the application
> code.
>
> Åsmund
>
> ------------------------------
> Date: Tue, 4 Nov 2014 23:39:51 -0500
> From: fuhrysteve@gmail.com
> Subject: [flask] different domain for a url shortener within larger
> application
> To: flask@librelist.com
>
> 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 a pplication, 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?
>