I have an existing Python application which servers a bunch of files via a Bottle.py service.
Now I have to change the path the server should react to.
The obvious way would be to change the @route statement and prepend the new path to each route.
But I got the constraint that the old path should keep on working for a while.
As the server should react to the old requests, too, I would have to replicate each route-statement in order to have it once with the old and the new path.
So, straightforward it would be a change from:
@route('/somefile')
def doSomeStuff():
to:
@route('/somefile')
@route('/newpath/somefile')
def doSomeStuff():
But as there are a lot of routes and I don’t want to mess with all the code, I am searching for an elegant way to process the requests before the routing happens.
Is there any way to hook into the routing process?
My current approach is to serve a 301 to the browser, but I don’t like this solution as it increases the number of requests and changes the user’s URLs.
#Serve a 301 (hope the browsers remember it for a session at least)
@route('/newpath<path:path>')
def redirectNewToOld(path):
if len(path) == 0: #Catch the lazy typers
path = '/'
redirect(path, code=301)
Well, I wrote my own mod_rewrite for bottle.
Code can also be found on GitHub
Changes:
(Based on fork created today, Oct 29, 2012)
In
class Bottle(object):@Lines 754ff, one addition:
Added at the bottom
I’ve sent a pull request, but here it is in case it won’t be pulled in. 😉