This commit is contained in:
Raphael Deem 2017-08-19 00:03:58 -07:00
parent 750115b727
commit 85fb7c6201
2 changed files with 12 additions and 8 deletions

View File

@ -28,7 +28,7 @@ class Sanic:
def __init__(self, name=None, router=None, error_handler=None, def __init__(self, name=None, router=None, error_handler=None,
load_env=True, request_class=None, load_env=True, request_class=None,
log_config=LOGGING): log_config=LOGGING, merge_routes=True):
if log_config: if log_config:
logging.config.dictConfig(log_config) logging.config.dictConfig(log_config)
# Only set up a default log handler if the # Only set up a default log handler if the
@ -63,6 +63,7 @@ class Sanic:
self.is_request_stream = False self.is_request_stream = False
self.websocket_enabled = False self.websocket_enabled = False
self.websocket_tasks = [] self.websocket_tasks = []
self.merge_routes = merge_routes
# Register alternative method names # Register alternative method names
self.go_fast = self.run self.go_fast = self.run
@ -135,7 +136,7 @@ class Sanic:
handler.is_stream = stream handler.is_stream = stream
self.router.add(uri=uri, methods=methods, handler=handler, self.router.add(uri=uri, methods=methods, handler=handler,
host=host, strict_slashes=strict_slashes, host=host, strict_slashes=strict_slashes,
version=version) version=version, merge_routes=self.merge_routes)
return handler return handler
return response return response

View File

@ -99,7 +99,7 @@ class Router:
return name, _type, pattern return name, _type, pattern
def add(self, uri, methods, handler, host=None, strict_slashes=False, def add(self, uri, methods, handler, host=None, strict_slashes=False,
version=None): version=None, merge_routes=True):
"""Add a handler to the route list """Add a handler to the route list
:param uri: path to match :param uri: path to match
@ -118,7 +118,7 @@ class Router:
else: else:
uri = "/".join(["/v{}".format(str(version)), uri]) uri = "/".join(["/v{}".format(str(version)), uri])
# add regular version # add regular version
self._add(uri, methods, handler, host) self._add(uri, methods, handler, host, merge_routes=merge_routes)
if strict_slashes: if strict_slashes:
return return
@ -135,12 +135,15 @@ class Router:
) )
# add version with trailing slash # add version with trailing slash
if slash_is_missing: if slash_is_missing:
self._add(uri + '/', methods, handler, host) self._add(uri + '/', methods, handler, host,
merge_routes=merge_routes)
# add version without trailing slash # add version without trailing slash
elif without_slash_is_missing: elif without_slash_is_missing:
self._add(uri[:-1], methods, handler, host) self._add(uri[:-1], methods, handler, host,
merge_routes=merge_routes)
def _add(self, uri, methods, handler, host=None): def _add(self, uri, methods, handler, host=None,
merge_routes=True):
"""Add a handler to the route list """Add a handler to the route list
:param uri: path to match :param uri: path to match
@ -229,7 +232,7 @@ class Router:
else: else:
route = self.routes_all.get(uri) route = self.routes_all.get(uri)
if route: if route and merge_routes:
route = merge_route(route, methods, handler) route = merge_route(route, methods, handler)
else: else:
# prefix the handler name with the blueprint name # prefix the handler name with the blueprint name