@@ -12,6 +12,7 @@ def create_app(**kwargs):
1212 '''
1313 from aiohttp import web
1414 from aiohttp_wsgi import WSGIHandler
15+ from aiohttp_remotes import setup , XForwardedRelaxed
1516 #
1617 from flask import Flask , Blueprint , current_app , redirect
1718 from flask_cors import CORS
@@ -53,32 +54,31 @@ def create_app(**kwargs):
5354 flask_app .config .update (config )
5455 flask_app .debug = config ['DEBUG' ]
5556 #
56- if flask_app .config ['PROXY' ]:
57- logger .info ('wsgi proxy fix...' )
58- from werkzeug .middleware .proxy_fix import ProxyFix
59- flask_app .wsgi_app = ProxyFix (flask_app .wsgi_app , x_for = 1 , x_proto = 1 , x_host = 1 , x_port = 1 )
60- #
6157 logger .info ('Registering blueprints...' )
62- flask_app .register_blueprint (core , url_prefix = flask_app . config [ 'PREFIX' ] )
58+ flask_app .register_blueprint (core )
6359 for blueprint_name , blueprint in find_blueprints (config = flask_app .config ).items ():
6460 if isinstance (blueprint , Blueprint ):
65- flask_app .register_blueprint (blueprint , url_prefix = join_routes ( flask_app . config [ 'PREFIX' ], blueprint_name ))
61+ flask_app .register_blueprint (blueprint , url_prefix = '/' + blueprint_name . strip ( '/' ))
6662 elif callable (blueprint ):
67- blueprint (flask_app , url_prefix = join_routes ( flask_app . config [ 'PREFIX' ], blueprint_name ), DATA_DIR = flask_app .config ['DATA_DIR' ])
63+ blueprint (flask_app , url_prefix = '/' + blueprint_name . strip ( '/' ), DATA_DIR = flask_app .config ['DATA_DIR' ])
6864 else :
6965 raise Exception ('Unrecognized blueprint type: ' + blueprint_name )
7066 #
71- if flask_app . config ['PREFIX' ].strip ('/' ):
67+ if app [ ' config' ] ['PREFIX' ].strip ('/' ):
7268 logger .info ('Registering prefix redirect' )
73- @ flask_app . route ( '/' )
74- @ flask_app . route ( '/<string: path>' )
75- def redirect_to_prefix ( path = '' ):
76- if path == flask_app . config [ 'PREFIX' ]. strip ( '/' ): path = ''
77- return redirect ( join_routes ( flask_app . config [ 'PREFIX' ], path ), code = 302 )
69+ async def redirect_to_prefix ( request ):
70+ path = request . match_info [ ' path' ]
71+ if path == app [ 'config' ][ 'PREFIX' ]. strip ( '/' ): path = ''
72+ raise web . HTTPFound ( join_routes ( app [ 'config' ][ 'PREFIX' ], path ) + '/' )
73+ app . router . add_get ( '/{path:[^/]*}' , redirect_to_prefix )
7874 #
7975 logger .info ('Registering flask with aiohttp...' )
8076 wsgi_handler = WSGIHandler (flask_app )
81- app .router .add_route ('*' , '/{path_info:.*}' , wsgi_handler )
77+ app .router .add_route ('*' , join_routes (app ['config' ]['PREFIX' ], '{path_info:.*}' ), wsgi_handler )
78+ if flask_app .config ['PROXY' ]:
79+ logger .info ('Applying proxy fix middleware...' )
80+ import asyncio
81+ asyncio .get_event_loop ().run_until_complete (setup (app , XForwardedRelaxed ()))
8282 return app
8383
8484# register flask_app with CLI
0 commit comments