@ -65,27 +65,23 @@ from flask_debugtoolbar import DebugToolbarExtension
from functools import wraps
class PrefixMiddleware ( object ) :
def __init__ ( self , app , prefix = ' ' ) :
self . app = app
self . prefix = prefix
def __call__ ( self , environ , start_response ) :
if environ [ ' PATH_INFO ' ] . startswith ( self . prefix ) :
environ [ ' PATH_INFO ' ] = environ [ ' PATH_INFO ' ] [ len ( self . prefix ) : ]
environ [ ' SCRIPT_NAME ' ] = self . prefix
return self . app ( environ , start_response )
else :
start_response ( ' 404 ' , [ ( ' Content-Type ' , ' text/plain ' ) ] )
return [ " This url does not belong to the app. " . encode ( ) ]
def prefix_route ( route_function , prefix = ' ' , mask = ' {0} {1} ' ) :
# Defines a new route function with a prefix.
# The mask argument is a `format string` formatted with, in that order: prefix, route
def newroute ( route , * args , * * kwargs ) :
# New function to prefix the route
return route_function ( mask . format ( prefix , route ) , * args , * * kwargs )
return newroute
# Flask Setup
app = Flask ( __name__ ,
template_folder = os . path . join ( os . path . dirname ( __file__ ) , ' .. ' , ' views ' ) ,
static_folder = os . path . join ( os . path . dirname ( __file__ ) , ' .. ' , ' static ' ) )
app . wsgi_app = PrefixMiddleware ( app . wsgi_app , prefix = base_url )
app . route = prefix_route ( app . route , base_url . rstrip ( ' / ' ) )
@app.errorhandler ( 404 )
def http_error_handler ( error ) :
return redirect ( base_url . rstrip ( ' / ' ) ) , 302
app . config [ " SECRET_KEY " ] = ' test '