From 85fb7c62019430f51306fce81b703254a3a39c74 Mon Sep 17 00:00:00 2001 From: Raphael Deem Date: Sat, 19 Aug 2017 00:03:58 -0700 Subject: [PATCH] fix #895 --- sanic/app.py | 5 +++-- sanic/router.py | 15 +++++++++------ 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/sanic/app.py b/sanic/app.py index d4ee8275..1f8869f4 100644 --- a/sanic/app.py +++ b/sanic/app.py @@ -28,7 +28,7 @@ class Sanic: def __init__(self, name=None, router=None, error_handler=None, load_env=True, request_class=None, - log_config=LOGGING): + log_config=LOGGING, merge_routes=True): if log_config: logging.config.dictConfig(log_config) # Only set up a default log handler if the @@ -63,6 +63,7 @@ class Sanic: self.is_request_stream = False self.websocket_enabled = False self.websocket_tasks = [] + self.merge_routes = merge_routes # Register alternative method names self.go_fast = self.run @@ -135,7 +136,7 @@ class Sanic: handler.is_stream = stream self.router.add(uri=uri, methods=methods, handler=handler, host=host, strict_slashes=strict_slashes, - version=version) + version=version, merge_routes=self.merge_routes) return handler return response diff --git a/sanic/router.py b/sanic/router.py index efc48f37..c0cb226d 100644 --- a/sanic/router.py +++ b/sanic/router.py @@ -99,7 +99,7 @@ class Router: return name, _type, pattern 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 :param uri: path to match @@ -118,7 +118,7 @@ class Router: else: uri = "/".join(["/v{}".format(str(version)), uri]) # add regular version - self._add(uri, methods, handler, host) + self._add(uri, methods, handler, host, merge_routes=merge_routes) if strict_slashes: return @@ -135,12 +135,15 @@ class Router: ) # add version with trailing slash 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 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 :param uri: path to match @@ -229,7 +232,7 @@ class Router: else: route = self.routes_all.get(uri) - if route: + if route and merge_routes: route = merge_route(route, methods, handler) else: # prefix the handler name with the blueprint name