Routable.use only accepts a Service

This commit is contained in:
Tobe O 2018-08-20 20:53:44 -04:00
parent 306549dd53
commit ecb15293a9
4 changed files with 20 additions and 112 deletions

View file

@ -45,4 +45,5 @@ type-safe manner.
* `RequestContext.params` is now a `Map<String, dynamic>`, rather than just a `Map`.
* Removed `RequestContext.grab`.
* Removed `RequestContext.properties`.
* Removed the defunct `debug` property where it still existed.
* Removed the defunct `debug` property where it still existed.
* `Routable.use` now only accepts a `Service`.

View file

@ -29,7 +29,7 @@ main() async {
);
// Simple fallback to throw a 404 on unknown paths.
app.use((RequestContext req) async {
app.fallback((req, res) {
throw new AngelHttpException.notFound(
message: 'Unknown path: "${req.uri.path}"',
);

View file

@ -9,7 +9,6 @@ import 'hooked_service.dart';
import 'metadata.dart';
import 'request_context.dart';
import 'response_context.dart';
import 'server.dart';
import 'service.dart';
final RegExp _straySlashes = new RegExp(r'(^/+)|(/+$)');
@ -89,70 +88,20 @@ class Routable extends Router<RequestHandler> {
middleware: handlerSequence);
}
/// Mounts the given [router] on this instance.
/// Mounts a [service] at the given [path].
///
/// The [router] may only omitted when called via
/// an [Angel] instance.
///
/// Returns a [HookedService] (if one was mounted).
HookedService use(path, [Router<RequestHandler> router, String namespace = null]) {
Router<RequestHandler> _router = router;
HookedService service;
// If we need to hook this service, do it here. It has to be first, or
// else all routes will point to the old service.
if (router is Service) {
_router = service = new HookedService(router);
_services[path
.toString()
.trim()
.replaceAll(new RegExp(r'(^/+)|(/+$)'), '')] = service;
service.addRoutes();
if (_router is HookedService && _router != router)
router.onHooked(_router);
}
final handlers = [];
if (_router is Angel) {
handlers.add((RequestContext req, ResponseContext res) {
req.app = _router as Angel;
res.app = _router as Angel;
return true;
});
}
// Let's copy middleware, heeding the optional middleware namespace.
String middlewarePrefix = namespace != null ? "$namespace." : "";
// Also copy properties...
if (router is Routable) {
Map copiedProperties = new Map.from(router.configuration);
for (String propertyName in copiedProperties.keys) {
configuration.putIfAbsent("$middlewarePrefix$propertyName",
() => copiedProperties[propertyName]);
}
}
// _router.dumpTree(header: 'Mounting on "$path":');
// root.child(path, debug: debug, handlers: handlers).addChild(router.root);
var mounted = mount(path.toString(), _router);
if (_router is Routable) {
// Copy services, too. :)
for (Pattern servicePath in _router._services.keys) {
String newServicePath =
path.toString().trim().replaceAll(new RegExp(r'(^/+)|(/+$)'), '') +
'/$servicePath';
_services[newServicePath] = _router._services[servicePath];
}
}
if (service != null) {
if (_onService.hasListener) _onService.add(service);
}
return service;
/// Returns a [HookedService] that can be used to hook into
/// events dispatched by this service.
HookedService use(String path, Service service) {
var hooked = new HookedService(service);
_services[path
.toString()
.trim()
.replaceAll(new RegExp(r'(^/+)|(/+$)'), '')] = hooked;
hooked.addRoutes();
mount(path.toString(), hooked);
service.onHooked(hooked);
_onService.add(hooked);
return hooked;
}
}

View file

@ -367,52 +367,10 @@ class Angel extends Routable {
return all('*', handler);
}
/// Mounts the child on this router. If [routable] is `null`,
/// then this method will add a handler as a global middleware instead.
///
/// If the router is an [Angel] instance, all controllers
/// will be copied, as well as services and response finalizers.
///
/// [before] and [after] will be preserved.
///
/// NOTE: The above will not be properly copied if [path] is
/// a [RegExp].
@override
HookedService use(path, [Router<RequestHandler> routable, String namespace = null]) {
var head = path.toString().replaceAll(_straySlashes, '');
if (routable is Angel) {
_children.add(routable.._parent = this);
_preContained.addAll(routable._preContained);
if (routable.responseFinalizers.isNotEmpty) {
responseFinalizers.add((req, res) {
if (req.path.replaceAll(_straySlashes, '').startsWith(head)) {
for (var finalizer in routable.responseFinalizers)
finalizer(req, res);
}
return new Future.value(true);
});
}
routable._controllers.forEach((k, v) {
var tail = k.toString().replaceAll(_straySlashes, '');
_controllers['$head/$tail'.replaceAll(_straySlashes, '')] = v;
});
routable.services.forEach((k, v) {
var tail = k.toString().replaceAll(_straySlashes, '');
services['$head/$tail'.replaceAll(_straySlashes, '')] = v;
});
}
if (routable is Service) {
routable.app = this;
}
return super.use(path, routable, namespace);
HookedService use(String path, Service service) {
service.app = this;
return super.use(path, service)..app = this;
}
Angel({@required Reflector reflector}) {