2016-11-22 03:31:09 +00:00
|
|
|
library angel_route.src.router;
|
|
|
|
|
2017-11-27 02:21:19 +00:00
|
|
|
import 'package:combinator/combinator.dart';
|
|
|
|
import 'package:meta/meta.dart';
|
|
|
|
import 'package:string_scanner/string_scanner.dart';
|
2016-10-19 22:04:06 +00:00
|
|
|
import 'routing_exception.dart';
|
2017-10-09 13:39:14 +00:00
|
|
|
import '../string_util.dart';
|
2017-11-27 02:21:19 +00:00
|
|
|
part 'grammar.dart';
|
2016-11-25 23:22:33 +00:00
|
|
|
part 'symlink_route.dart';
|
2016-11-22 03:31:09 +00:00
|
|
|
part 'route.dart';
|
2016-11-25 23:22:33 +00:00
|
|
|
part 'routing_result.dart';
|
2016-11-22 03:31:09 +00:00
|
|
|
|
|
|
|
final RegExp _param = new RegExp(r':([A-Za-z0-9_]+)(\((.+)\))?');
|
|
|
|
final RegExp _rgxEnd = new RegExp(r'\$+$');
|
|
|
|
final RegExp _rgxStart = new RegExp(r'^\^+');
|
2016-11-23 09:13:42 +00:00
|
|
|
final RegExp _rgxStraySlashes =
|
|
|
|
new RegExp(r'(^((\\+/)|(/))+)|(((\\+/)|(/))+$)');
|
2016-11-22 03:31:09 +00:00
|
|
|
final RegExp _slashDollar = new RegExp(r'/+\$');
|
2016-10-12 17:58:32 +00:00
|
|
|
final RegExp _straySlashes = new RegExp(r'(^/+)|(/+$)');
|
|
|
|
|
2016-10-19 22:04:06 +00:00
|
|
|
/// An abstraction over complex [Route] trees. Use this instead of the raw API. :)
|
2017-08-03 16:14:21 +00:00
|
|
|
class Router {
|
2017-11-26 05:33:17 +00:00
|
|
|
final Map<String, Iterable<RoutingResult>> _cache = {};
|
|
|
|
//final List<_ChainedRouter> _chained = [];
|
2016-11-25 23:22:33 +00:00
|
|
|
final List _middleware = [];
|
|
|
|
final Map<Pattern, Router> _mounted = {};
|
|
|
|
final List<Route> _routes = [];
|
2017-11-26 05:33:17 +00:00
|
|
|
bool _useCache = false;
|
2016-10-21 03:13:13 +00:00
|
|
|
|
2016-10-19 22:04:06 +00:00
|
|
|
/// Set to `true` to print verbose debug output when interacting with this route.
|
|
|
|
bool debug = false;
|
|
|
|
|
2016-11-25 23:22:33 +00:00
|
|
|
List get middleware => new List.unmodifiable(_middleware);
|
|
|
|
|
|
|
|
Map<Pattern, Router> get mounted =>
|
|
|
|
new Map<Pattern, Router>.unmodifiable(_mounted);
|
|
|
|
|
2016-10-12 17:58:32 +00:00
|
|
|
/// Additional filters to be run on designated requests.
|
|
|
|
Map<String, dynamic> requestMiddleware = {};
|
|
|
|
|
2017-03-17 17:36:04 +00:00
|
|
|
List<Route> get routes {
|
2017-10-08 22:44:11 +00:00
|
|
|
return _routes.fold<List<Route>>([], (out, route) {
|
|
|
|
if (route is SymlinkRoute) {
|
|
|
|
var childRoutes = route.router.routes.fold<List<Route>>([], (out, r) {
|
|
|
|
return out
|
|
|
|
..add(
|
|
|
|
route.path.isEmpty ? r : new Route.join(route, r),
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
return out..addAll(childRoutes);
|
|
|
|
} else {
|
|
|
|
return out..add(route);
|
|
|
|
}
|
|
|
|
});
|
2017-03-17 17:36:04 +00:00
|
|
|
}
|
2016-10-12 17:58:32 +00:00
|
|
|
|
2016-10-19 22:04:06 +00:00
|
|
|
/// Provide a `root` to make this Router revolve around a pre-defined route.
|
|
|
|
/// Not recommended.
|
2016-11-25 23:22:33 +00:00
|
|
|
Router({this.debug: false});
|
2016-10-12 17:58:32 +00:00
|
|
|
|
2017-11-26 05:33:17 +00:00
|
|
|
/// Enables the use of a cache to eliminate the overhead of consecutive resolutions of the same path.
|
|
|
|
void enableCache() {
|
|
|
|
_useCache = true;
|
|
|
|
}
|
|
|
|
|
2016-10-12 17:58:32 +00:00
|
|
|
/// Adds a route that responds to the given path
|
|
|
|
/// for requests with the given method (case-insensitive).
|
|
|
|
/// Provide '*' as the method to respond to all methods.
|
2017-11-27 02:21:19 +00:00
|
|
|
Route addRoute(String method, String path, Object handler,
|
2016-11-25 23:22:33 +00:00
|
|
|
{List middleware: const []}) {
|
2017-11-26 05:33:17 +00:00
|
|
|
if (_useCache == true)
|
|
|
|
throw new StateError('Cannot add routes after caching is enabled.');
|
|
|
|
|
2016-11-25 23:22:33 +00:00
|
|
|
// Check if any mounted routers can match this
|
|
|
|
final handlers = [handler];
|
2016-10-19 22:04:06 +00:00
|
|
|
|
2016-11-27 23:58:55 +00:00
|
|
|
if (middleware != null) handlers.insertAll(0, middleware);
|
2016-10-19 22:04:06 +00:00
|
|
|
|
2017-11-27 02:21:19 +00:00
|
|
|
final route = new Route(path, method: method, handlers: handlers);
|
2016-11-25 23:22:33 +00:00
|
|
|
_routes.add(route);
|
2017-11-27 02:21:19 +00:00
|
|
|
return route;
|
2016-10-12 17:58:32 +00:00
|
|
|
}
|
|
|
|
|
2016-11-27 22:24:30 +00:00
|
|
|
/// Prepends the given middleware to any routes created
|
|
|
|
/// by the resulting router.
|
2017-03-17 17:36:04 +00:00
|
|
|
///
|
2017-01-12 01:57:26 +00:00
|
|
|
/// [middleware] can be either an `Iterable`, or a single object.
|
2016-11-27 22:24:30 +00:00
|
|
|
///
|
|
|
|
/// The resulting router can be chained, too.
|
2017-03-17 17:36:04 +00:00
|
|
|
_ChainedRouter chain(middleware) {
|
|
|
|
var piped = new _ChainedRouter(this, middleware);
|
2017-11-27 02:21:19 +00:00
|
|
|
var route = new SymlinkRoute('/', piped);
|
2017-10-08 22:44:11 +00:00
|
|
|
_routes.add(route);
|
|
|
|
return piped;
|
2017-03-17 17:36:04 +00:00
|
|
|
}
|
2016-11-27 22:24:30 +00:00
|
|
|
|
2016-11-23 18:58:34 +00:00
|
|
|
/// Returns a [Router] with a duplicated version of this tree.
|
2016-11-25 23:22:33 +00:00
|
|
|
Router clone() {
|
2016-11-23 18:58:34 +00:00
|
|
|
final router = new Router(debug: debug);
|
2016-12-21 23:28:00 +00:00
|
|
|
final newMounted = new Map<Pattern, Router>.from(mounted);
|
2016-11-25 23:22:33 +00:00
|
|
|
|
|
|
|
for (Route route in routes) {
|
|
|
|
if (route is! SymlinkRoute) {
|
|
|
|
router._routes.add(route.clone());
|
|
|
|
} else if (route is SymlinkRoute) {
|
2016-11-27 04:04:47 +00:00
|
|
|
final newRouter = route.router.clone();
|
|
|
|
newMounted[route.path] = newRouter;
|
2017-11-27 02:21:19 +00:00
|
|
|
final symlink = new SymlinkRoute(route.path, newRouter);
|
2016-11-27 04:04:47 +00:00
|
|
|
router._routes.add(symlink);
|
2016-11-25 23:22:33 +00:00
|
|
|
}
|
2016-11-23 18:58:34 +00:00
|
|
|
}
|
|
|
|
|
2016-11-25 23:22:33 +00:00
|
|
|
return router.._mounted.addAll(newMounted);
|
2016-11-23 18:58:34 +00:00
|
|
|
}
|
|
|
|
|
2016-10-12 17:58:32 +00:00
|
|
|
/// Creates a visual representation of the route hierarchy and
|
|
|
|
/// passes it to a callback. If none is provided, `print` is called.
|
|
|
|
void dumpTree(
|
2016-11-23 09:13:42 +00:00
|
|
|
{callback(String tree),
|
2016-11-25 23:22:33 +00:00
|
|
|
String header: 'Dumping route tree:',
|
2017-11-27 02:21:19 +00:00
|
|
|
String tab: ' '}) {
|
2016-10-12 17:58:32 +00:00
|
|
|
final buf = new StringBuffer();
|
2016-11-25 23:22:33 +00:00
|
|
|
int tabs = 0;
|
2016-10-12 17:58:32 +00:00
|
|
|
|
2016-11-25 23:22:33 +00:00
|
|
|
if (header != null && header.isNotEmpty) {
|
|
|
|
buf.writeln(header);
|
|
|
|
}
|
2016-10-20 09:21:59 +00:00
|
|
|
|
2016-11-27 04:04:47 +00:00
|
|
|
buf.writeln('<root>');
|
|
|
|
|
2016-11-25 23:22:33 +00:00
|
|
|
indent() {
|
|
|
|
for (int i = 0; i < tabs; i++) buf.write(tab);
|
|
|
|
}
|
2016-10-12 17:58:32 +00:00
|
|
|
|
2016-11-25 23:22:33 +00:00
|
|
|
dumpRouter(Router router) {
|
|
|
|
indent();
|
|
|
|
tabs++;
|
2016-10-12 17:58:32 +00:00
|
|
|
|
2016-11-25 23:22:33 +00:00
|
|
|
for (Route route in router.routes) {
|
|
|
|
indent();
|
2017-11-27 02:21:19 +00:00
|
|
|
buf.write('- ');
|
|
|
|
if (route is! SymlinkRoute) buf.write('${route.method} ');
|
|
|
|
buf.write('${route.path.isNotEmpty ? route.path : '/'}');
|
2016-10-12 17:58:32 +00:00
|
|
|
|
2016-11-25 23:22:33 +00:00
|
|
|
if (route is SymlinkRoute) {
|
|
|
|
buf.writeln();
|
|
|
|
dumpRouter(route.router);
|
|
|
|
} else {
|
2016-10-23 00:52:28 +00:00
|
|
|
buf.writeln(' => ${route.handlers.length} handler(s)');
|
2016-11-25 23:22:33 +00:00
|
|
|
}
|
2016-10-23 00:52:28 +00:00
|
|
|
}
|
2016-10-12 17:58:32 +00:00
|
|
|
|
|
|
|
tabs--;
|
|
|
|
}
|
|
|
|
|
2016-11-25 23:22:33 +00:00
|
|
|
dumpRouter(this);
|
2016-10-12 17:58:32 +00:00
|
|
|
|
2016-10-19 22:04:06 +00:00
|
|
|
(callback ?? print)(buf.toString());
|
2016-10-12 17:58:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Creates a route, and allows you to add child routes to it
|
|
|
|
/// via a [Router] instance.
|
|
|
|
///
|
|
|
|
/// Returns the created route.
|
|
|
|
/// You can also register middleware within the router.
|
2017-11-27 02:21:19 +00:00
|
|
|
SymlinkRoute group(String path, void callback(Router router),
|
2016-10-12 17:58:32 +00:00
|
|
|
{Iterable middleware: const [],
|
|
|
|
String name: null,
|
|
|
|
String namespace: null}) {
|
2016-11-27 23:39:03 +00:00
|
|
|
final router = new Router().._middleware.addAll(middleware);
|
|
|
|
callback(router..debug = debug);
|
2017-11-27 02:21:19 +00:00
|
|
|
return mount(path, router, namespace: namespace)..name = name;
|
2016-11-25 23:22:33 +00:00
|
|
|
}
|
2016-10-12 17:58:32 +00:00
|
|
|
|
2016-11-25 23:22:33 +00:00
|
|
|
/// Generates a URI string based on the given input.
|
|
|
|
/// Handy when you have named routes.
|
|
|
|
///
|
|
|
|
/// Each item in `linkParams` should be a [Route],
|
|
|
|
/// `String` or `Map<String, dynamic>`.
|
|
|
|
///
|
|
|
|
/// Strings should be route names, namespaces, or paths.
|
|
|
|
/// Maps should be parameters, which will be filled
|
|
|
|
/// into the previous route.
|
|
|
|
///
|
|
|
|
/// Paths and segments should correspond to the way
|
|
|
|
/// you declared them.
|
|
|
|
///
|
|
|
|
/// For example, if you declared a route group on
|
|
|
|
/// `'users/:id'`, it would not be resolved if you
|
|
|
|
/// passed `'users'` in [linkParams].
|
|
|
|
///
|
|
|
|
/// Leading and trailing slashes are automatically
|
|
|
|
/// removed.
|
|
|
|
///
|
|
|
|
/// Set [absolute] to `true` to insert a forward slash
|
|
|
|
/// before the generated path.
|
|
|
|
///
|
|
|
|
/// Example:
|
|
|
|
/// ```dart
|
|
|
|
/// router.navigate(['users/:id', {'id': '1337'}, 'profile']);
|
|
|
|
/// ```
|
2016-11-27 22:24:30 +00:00
|
|
|
String navigate(Iterable linkParams, {bool absolute: true}) {
|
2016-11-25 23:22:33 +00:00
|
|
|
final List<String> segments = [];
|
|
|
|
Router search = this;
|
|
|
|
Route lastRoute;
|
|
|
|
|
|
|
|
for (final param in linkParams) {
|
|
|
|
bool resolved = false;
|
|
|
|
|
|
|
|
if (param is String) {
|
|
|
|
// Search by name
|
|
|
|
for (Route route in search.routes) {
|
|
|
|
if (route.name == param) {
|
|
|
|
segments.add(route.path.replaceAll(_straySlashes, ''));
|
|
|
|
lastRoute = route;
|
|
|
|
|
|
|
|
if (route is SymlinkRoute) {
|
|
|
|
search = route.router;
|
|
|
|
}
|
|
|
|
|
|
|
|
resolved = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Search by path
|
2017-11-27 02:21:19 +00:00
|
|
|
if (!resolved) {
|
|
|
|
var scanner = new SpanScanner(param.replaceAll(_straySlashes, ''));
|
|
|
|
for (Route route in search.routes) {
|
|
|
|
int pos = scanner.position;
|
|
|
|
if (route.parser
|
|
|
|
.parse(scanner)
|
|
|
|
.successful && scanner.isDone) {
|
|
|
|
segments.add(route.path.replaceAll(_straySlashes, ''));
|
|
|
|
lastRoute = route;
|
|
|
|
|
|
|
|
if (route is SymlinkRoute) {
|
|
|
|
search = route.router;
|
|
|
|
}
|
|
|
|
|
|
|
|
resolved = true;
|
|
|
|
break;
|
|
|
|
} else
|
|
|
|
scanner.position = pos;
|
2016-11-25 23:22:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!resolved) {
|
|
|
|
throw new RoutingException(
|
|
|
|
'Cannot resolve route for link param "$param".');
|
|
|
|
}
|
|
|
|
} else if (param is Route) {
|
|
|
|
segments.add(param.path.replaceAll(_straySlashes, ''));
|
|
|
|
} else if (param is Map<String, dynamic>) {
|
|
|
|
if (lastRoute == null) {
|
|
|
|
throw new RoutingException(
|
|
|
|
'Maps in link params must be preceded by a Route or String.');
|
|
|
|
} else {
|
|
|
|
segments.removeLast();
|
|
|
|
segments.add(lastRoute.makeUri(param).replaceAll(_straySlashes, ''));
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
throw new RoutingException(
|
|
|
|
'Link param $param is not Route, String, or Map<String, dynamic>.');
|
2016-10-12 17:58:32 +00:00
|
|
|
}
|
|
|
|
|
2016-11-25 23:22:33 +00:00
|
|
|
return absolute
|
|
|
|
? '/${segments.join('/').replaceAll(_straySlashes, '')}'
|
|
|
|
: segments.join('/');
|
2016-10-12 17:58:32 +00:00
|
|
|
}
|
|
|
|
|
2017-11-18 05:29:08 +00:00
|
|
|
/// Manually assign via [requestMiddleware] instead.
|
|
|
|
@deprecated
|
2016-10-12 17:58:32 +00:00
|
|
|
registerMiddleware(String name, middleware) {
|
|
|
|
requestMiddleware[name] = middleware;
|
|
|
|
}
|
|
|
|
|
2016-11-22 03:31:09 +00:00
|
|
|
/// Finds the first [Route] that matches the given path,
|
|
|
|
/// with the given method.
|
2017-11-25 00:26:06 +00:00
|
|
|
bool resolve(String absolute, String relative, List<RoutingResult> out,
|
|
|
|
{String method: 'GET', bool strip: true}) {
|
|
|
|
final cleanRelative =
|
|
|
|
strip == false ? relative : stripStraySlashes(relative);
|
2017-11-27 02:21:19 +00:00
|
|
|
var scanner = new SpanScanner(cleanRelative);
|
2016-11-22 03:31:09 +00:00
|
|
|
|
2017-11-27 02:21:19 +00:00
|
|
|
bool crawl(Router r) {
|
|
|
|
bool success = false;
|
|
|
|
|
|
|
|
for (Route route in r.routes) {
|
|
|
|
int pos = scanner.position;
|
|
|
|
|
|
|
|
if (route is SymlinkRoute) {
|
|
|
|
if (route.parser.parse(scanner).successful) {
|
|
|
|
var s = crawl(route.router);
|
|
|
|
if (s) success = true;
|
2016-11-27 22:24:30 +00:00
|
|
|
}
|
2017-11-27 02:21:19 +00:00
|
|
|
|
|
|
|
scanner.position = pos;
|
|
|
|
} else if (route.method == '*' || route.method == method) {
|
|
|
|
var parseResult = route.parser.parse(scanner);
|
|
|
|
|
|
|
|
if (parseResult.successful && scanner.isDone) {
|
|
|
|
var result = new RoutingResult(
|
|
|
|
parseResult: parseResult,
|
|
|
|
params: parseResult.value,
|
|
|
|
shallowRoute: route,
|
|
|
|
shallowRouter: this);
|
|
|
|
out.add(result);
|
|
|
|
success = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
scanner.position = pos;
|
2016-11-23 09:13:42 +00:00
|
|
|
}
|
2016-11-27 22:24:30 +00:00
|
|
|
}
|
2017-11-27 02:21:19 +00:00
|
|
|
|
|
|
|
return success;
|
2016-11-22 03:31:09 +00:00
|
|
|
}
|
|
|
|
|
2017-11-27 02:21:19 +00:00
|
|
|
return crawl(this);
|
2016-11-23 18:58:34 +00:00
|
|
|
}
|
|
|
|
|
2016-11-27 22:24:30 +00:00
|
|
|
/// Returns the result of [resolve] with [path] passed as
|
|
|
|
/// both `absolute` and `relative`.
|
2017-11-25 00:26:06 +00:00
|
|
|
Iterable<RoutingResult> resolveAbsolute(String path,
|
|
|
|
{String method: 'GET', bool strip: true}) =>
|
|
|
|
resolveAll(path, path, method: method, strip: strip);
|
2016-11-27 22:24:30 +00:00
|
|
|
|
2016-11-25 23:22:33 +00:00
|
|
|
/// Finds every possible [Route] that matches the given path,
|
|
|
|
/// with the given method.
|
2016-11-27 14:46:13 +00:00
|
|
|
Iterable<RoutingResult> resolveAll(String absolute, String relative,
|
2017-11-25 00:26:06 +00:00
|
|
|
{String method: 'GET', bool strip: true}) {
|
2017-11-26 05:33:17 +00:00
|
|
|
if (_useCache == true) {
|
|
|
|
return _cache.putIfAbsent(absolute,
|
|
|
|
() => _resolveAll(absolute, relative, method: method, strip: strip));
|
|
|
|
}
|
|
|
|
|
|
|
|
return _resolveAll(absolute, relative, method: method, strip: strip);
|
|
|
|
}
|
|
|
|
|
|
|
|
Iterable<RoutingResult> _resolveAll(String absolute, String relative,
|
|
|
|
{String method: 'GET', bool strip: true}) {
|
2016-11-25 23:22:33 +00:00
|
|
|
final List<RoutingResult> results = [];
|
2017-11-25 00:26:06 +00:00
|
|
|
resolve(absolute, relative, results, method: method, strip: strip);
|
2016-11-22 03:31:09 +00:00
|
|
|
|
2017-06-06 12:48:55 +00:00
|
|
|
// _printDebug(
|
|
|
|
// 'Results of $method "/${absolute.replaceAll(_straySlashes, '')}": ${results.map((r) => r.route).toList()}');
|
2016-11-25 23:22:33 +00:00
|
|
|
return results;
|
|
|
|
}
|
|
|
|
|
2016-10-12 17:58:32 +00:00
|
|
|
/// Incorporates another [Router]'s routes into this one's.
|
|
|
|
///
|
|
|
|
/// If `hooked` is set to `true` and a [Service] is provided,
|
|
|
|
/// then that service will be wired to a [HookedService] proxy.
|
|
|
|
/// If a `namespace` is provided, then any middleware
|
|
|
|
/// from the provided [Router] will be prefixed by that namespace,
|
|
|
|
/// with a dot.
|
|
|
|
/// For example, if the [Router] has a middleware 'y', and the `namespace`
|
|
|
|
/// is 'x', then that middleware will be available as 'x.y' in the main router.
|
|
|
|
/// These namespaces can be nested.
|
2017-11-27 02:21:19 +00:00
|
|
|
SymlinkRoute mount(String path, Router router,
|
2016-10-12 17:58:32 +00:00
|
|
|
{bool hooked: true, String namespace: null}) {
|
|
|
|
// Let's copy middleware, heeding the optional middleware namespace.
|
|
|
|
String middlewarePrefix = namespace != null ? "$namespace." : "";
|
|
|
|
|
|
|
|
Map copiedMiddleware = new Map.from(router.requestMiddleware);
|
|
|
|
for (String middlewareName in copiedMiddleware.keys) {
|
|
|
|
requestMiddleware["$middlewarePrefix$middlewareName"] =
|
2016-11-23 09:13:42 +00:00
|
|
|
copiedMiddleware[middlewareName];
|
2016-11-22 03:31:09 +00:00
|
|
|
}
|
|
|
|
|
2017-11-27 02:21:19 +00:00
|
|
|
final route = new SymlinkRoute(path, router);
|
2016-11-27 04:04:47 +00:00
|
|
|
_mounted[route.path] = router;
|
2016-11-25 23:22:33 +00:00
|
|
|
_routes.add(route);
|
2017-11-27 02:21:19 +00:00
|
|
|
//route._head = new RegExp(route.matcher.pattern.replaceAll(_rgxEnd, ''));
|
2016-11-22 03:31:09 +00:00
|
|
|
|
2017-11-27 02:21:19 +00:00
|
|
|
return route..name = namespace;
|
2016-10-12 17:58:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Adds a route that responds to any request matching the given path.
|
2017-11-27 02:21:19 +00:00
|
|
|
Route all(String path, Object handler, {List middleware}) {
|
2016-10-12 17:58:32 +00:00
|
|
|
return addRoute('*', path, handler, middleware: middleware);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Adds a route that responds to a DELETE request.
|
2017-11-27 02:21:19 +00:00
|
|
|
Route delete(String path, Object handler, {List middleware}) {
|
2016-10-12 17:58:32 +00:00
|
|
|
return addRoute('DELETE', path, handler, middleware: middleware);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Adds a route that responds to a GET request.
|
2017-11-27 02:21:19 +00:00
|
|
|
Route get(String path, Object handler, {List middleware}) {
|
2016-10-12 17:58:32 +00:00
|
|
|
return addRoute('GET', path, handler, middleware: middleware);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Adds a route that responds to a HEAD request.
|
2017-11-27 02:21:19 +00:00
|
|
|
Route head(String path, Object handler, {List middleware}) {
|
2016-10-12 17:58:32 +00:00
|
|
|
return addRoute('HEAD', path, handler, middleware: middleware);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Adds a route that responds to a OPTIONS request.
|
2017-11-27 02:21:19 +00:00
|
|
|
Route options(String path, Object handler, {List middleware}) {
|
2016-10-12 17:58:32 +00:00
|
|
|
return addRoute('OPTIONS', path, handler, middleware: middleware);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Adds a route that responds to a POST request.
|
2017-11-27 02:21:19 +00:00
|
|
|
Route post(String path, Object handler, {List middleware}) {
|
2016-10-12 17:58:32 +00:00
|
|
|
return addRoute('POST', path, handler, middleware: middleware);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Adds a route that responds to a PATCH request.
|
2017-11-27 02:21:19 +00:00
|
|
|
Route patch(String path, Object handler, {List middleware}) {
|
2016-10-12 17:58:32 +00:00
|
|
|
return addRoute('PATCH', path, handler, middleware: middleware);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Adds a route that responds to a PUT request.
|
2017-11-27 02:21:19 +00:00
|
|
|
Route put(String path, Object handler, {List middleware}) {
|
2016-10-12 17:58:32 +00:00
|
|
|
return addRoute('PUT', path, handler, middleware: middleware);
|
|
|
|
}
|
|
|
|
}
|
2016-11-27 22:24:30 +00:00
|
|
|
|
|
|
|
class _ChainedRouter extends Router {
|
|
|
|
final List _handlers = [];
|
|
|
|
Router _root;
|
|
|
|
|
|
|
|
_ChainedRouter.empty();
|
|
|
|
|
|
|
|
_ChainedRouter(Router root, middleware) {
|
|
|
|
this._root = root;
|
2017-01-12 01:57:26 +00:00
|
|
|
_handlers.addAll(middleware is Iterable ? middleware : [middleware]);
|
2016-11-27 22:24:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
2017-11-27 02:21:19 +00:00
|
|
|
Route addRoute(String method, String path, handler,
|
2016-11-27 22:24:30 +00:00
|
|
|
{List middleware: const []}) {
|
2017-10-08 22:44:11 +00:00
|
|
|
var route = super.addRoute(method, path, handler,
|
2016-11-27 22:24:30 +00:00
|
|
|
middleware: []..addAll(_handlers)..addAll(middleware ?? []));
|
2017-10-08 22:44:11 +00:00
|
|
|
//_root._routes.add(route);
|
|
|
|
return route;
|
2016-11-27 22:24:30 +00:00
|
|
|
}
|
|
|
|
|
2017-11-27 02:21:19 +00:00
|
|
|
SymlinkRoute group(String path, void callback(Router router),
|
2017-03-17 17:36:04 +00:00
|
|
|
{Iterable middleware: const [],
|
|
|
|
String name: null,
|
|
|
|
String namespace: null}) {
|
|
|
|
final router =
|
|
|
|
new _ChainedRouter(_root, []..addAll(_handlers)..addAll(middleware));
|
|
|
|
callback(router..debug = debug);
|
2017-11-27 02:21:19 +00:00
|
|
|
return mount(path, router, namespace: namespace)..name = name;
|
2017-03-17 17:36:04 +00:00
|
|
|
}
|
|
|
|
|
2016-11-27 22:24:30 +00:00
|
|
|
@override
|
2017-11-27 02:21:19 +00:00
|
|
|
SymlinkRoute mount(String path, Router router,
|
2016-11-27 22:24:30 +00:00
|
|
|
{bool hooked: true, String namespace: null}) {
|
|
|
|
final route =
|
|
|
|
super.mount(path, router, hooked: hooked, namespace: namespace);
|
|
|
|
route.router._middleware.insertAll(0, _handlers);
|
2017-10-08 22:44:11 +00:00
|
|
|
//_root._routes.add(route);
|
2016-11-27 22:24:30 +00:00
|
|
|
return route;
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
_ChainedRouter chain(middleware) {
|
|
|
|
final piped = new _ChainedRouter.empty().._root = _root;
|
|
|
|
piped._handlers.addAll([]
|
|
|
|
..addAll(_handlers)
|
2017-01-12 01:57:26 +00:00
|
|
|
..addAll(middleware is Iterable ? middleware : [middleware]));
|
2017-11-27 02:21:19 +00:00
|
|
|
var route = new SymlinkRoute('/', piped);
|
2017-10-08 22:44:11 +00:00
|
|
|
_routes.add(route);
|
|
|
|
return piped;
|
2016-11-27 22:24:30 +00:00
|
|
|
}
|
|
|
|
}
|
2017-11-26 05:33:17 +00:00
|
|
|
|
|
|
|
/// Optimizes a router by condensing all its routes into one level.
|
|
|
|
Router flatten(Router router) {
|
|
|
|
var flattened = new Router(debug: router.debug == true)
|
|
|
|
..requestMiddleware.addAll(router.requestMiddleware)
|
|
|
|
..enableCache();
|
|
|
|
|
|
|
|
for (var route in router.routes) {
|
|
|
|
if (route is SymlinkRoute) {
|
|
|
|
var base = route.path.replaceAll(_straySlashes, '');
|
|
|
|
var child = flatten(route.router);
|
|
|
|
flattened.requestMiddleware.addAll(child.requestMiddleware);
|
|
|
|
|
|
|
|
for (var route in child.routes) {
|
|
|
|
var path = route.path.replaceAll(_straySlashes, '');
|
|
|
|
var joined = '$base/$path'.replaceAll(_straySlashes, '');
|
|
|
|
flattened.addRoute(route.method, joined.replaceAll(_straySlashes, ''),
|
|
|
|
route.handlers.last,
|
|
|
|
middleware:
|
|
|
|
route.handlers.take(route.handlers.length - 1).toList());
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
flattened.addRoute(route.method, route.path, route.handlers.last,
|
|
|
|
middleware: route.handlers.take(route.handlers.length - 1).toList());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return flattened;
|
|
|
|
}
|