diff --git a/.idea/workspace.xml b/.idea/workspace.xml index d9596f4e..34d3d406 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -2,11 +2,7 @@ - - - - @@ -62,8 +58,8 @@ - - + + @@ -384,7 +380,7 @@ - + 1481237183504 @@ -421,7 +417,14 @@ - @@ -457,7 +460,7 @@ - @@ -479,7 +482,7 @@ - + @@ -506,7 +509,8 @@ - @@ -881,8 +885,8 @@ - - + + diff --git a/README.md b/README.md index 8886dff5..991942ff 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,22 @@ # angel_framework -[![pub 1.0.0-dev.40](https://img.shields.io/badge/pub-1.0.0--dev.40-red.svg)](https://pub.dartlang.org/packages/angel_framework) +[![pub 1.0.0-dev.42](https://img.shields.io/badge/pub-1.0.0--dev.42-red.svg)](https://pub.dartlang.org/packages/angel_framework) [![build status](https://travis-ci.org/angel-dart/framework.svg)](https://travis-ci.org/angel-dart/framework) -Core libraries for the Angel Framework. \ No newline at end of file +Core libraries for the Angel Framework. + +```dart +import 'package:angel_framework/angel_framework.dart'; + +main() async { + var app = new Angel(); + + app + ..get('/hello', (req, res) { + res.write('world!'); + }) + ..post('/date', () => new DateTime.now().toString()); + + await app.startServer(); +} +``` \ No newline at end of file diff --git a/lib/src/http/controller.dart b/lib/src/http/controller.dart index 9fe76699..7f2fad0a 100644 --- a/lib/src/http/controller.dart +++ b/lib/src/http/controller.dart @@ -45,7 +45,6 @@ class Controller { } var routable = new Routable(debug: debug); - app.use(exposeDecl.path, routable); TypeMirror typeMirror = reflectType(this.runtimeType); String name = exposeDecl.as?.isNotEmpty == true @@ -79,21 +78,23 @@ class Controller { var reflectedMethod = instanceMirror.getField(methodName).reflectee; var middleware = []..addAll(handlers)..addAll(exposeDecl.middleware); + String name = exposeDecl.as?.isNotEmpty == true + ? exposeDecl.as + : MirrorSystem.getName(methodName); // Check if normal if (method.parameters.length == 2 && method.parameters[0].type.reflectedType == RequestContext && method.parameters[1].type.reflectedType == ResponseContext) { // Create a regular route - routable.addRoute(exposeDecl.method, exposeDecl.path, reflectedMethod, - middleware: middleware); + routeMappings[name] = routable + .addRoute(exposeDecl.method, exposeDecl.path, (req, res) async { + var result = await reflectedMethod(req, res); + return result is RequestHandler ? await result(req, res) : result; + }, middleware: middleware); return; } - String name = exposeDecl.as?.isNotEmpty == true - ? exposeDecl.as - : MirrorSystem.getName(methodName); - routeMappings[name] = routable.addRoute( exposeDecl.method, exposeDecl.path, @@ -119,40 +120,43 @@ RequestHandler handleContained(handler, InjectionRequest injection) { List args = []; void inject(requirement) { - for (var requirement in injection.required) { - if (requirement == RequestContext) { - args.add(req); - } else if (requirement == ResponseContext) { - args.add(res); - } else if (requirement is String) { - if (req.params.containsKey(requirement)) {} else if (req.injections - .containsKey(requirement)) - args.add(req.injections[requirement]); - else { - throw new ArgumentError( - "Cannot resolve parameter '$requirement' within handler."); - } + if (requirement == RequestContext) { + args.add(req); + } else if (requirement == ResponseContext) { + args.add(res); + } else if (requirement is String) { + if (req.params.containsKey(requirement)) { args.add(req.params[requirement]); - } else if (requirement is List) { - for (var child in requirement) { - try { - inject(child); - break; - } catch (e) { - rethrow; - } - } - } else if (requirement is Type && requirement != dynamic) { - args.add( - req.app.container.make(requirement, injecting: req.injections)); - } else { + } else if (req.injections.containsKey(requirement)) + args.add(req.injections[requirement]); + else { throw new ArgumentError( - '$requirement cannot be injected into a request handler.'); + "Cannot resolve parameter '$requirement' within handler."); } + } else if (requirement is List && + requirement.length == 2 && + requirement.first is String && + requirement.last is Type) { + String key = requirement.first; + Type type = requirement.last; + + if (req.params.containsKey(key) || req.injections.containsKey(key)) { + inject(key); + } else + inject(type); + } else if (requirement is Type && requirement != dynamic) { + if (req.injections.containsKey(requirement)) + args.add(req.injections[requirement]); + else + args.add(req.app.container.make(requirement)); + } else { + throw new ArgumentError( + '$requirement cannot be injected into a request handler.'); } } injection.required.forEach(inject); - return Function.apply(handler, args); + var result = Function.apply(handler, args); + return result is Future ? await result : result; }; } diff --git a/lib/src/http/server.dart b/lib/src/http/server.dart index 23ce0063..458fc145 100644 --- a/lib/src/http/server.dart +++ b/lib/src/http/server.dart @@ -321,10 +321,9 @@ class Angel extends AngelBase { /// Runs with DI, and *always* reflects. Prefer [runContained]. Future runReflected( Function handler, RequestContext req, ResponseContext res) async { - var injection = preInjection(handler); - - _preContained[handler] = injection; - return handleContained(handler, injection); + var h = + handleContained(handler, _preContained[handler] = preInject(handler)); + return await h(req, res); // return await closureMirror.apply(args).reflectee; } diff --git a/pubspec.yaml b/pubspec.yaml index 106f1b64..1bb182a8 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,5 +1,5 @@ name: angel_framework -version: 1.0.0-dev.41 +version: 1.0.0-dev.42 description: Core libraries for the Angel framework. author: Tobe O homepage: https://github.com/angel-dart/angel_framework