From eb1e931b89638e063898c4737e175a1159c22d12 Mon Sep 17 00:00:00 2001 From: thosakwe Date: Sun, 15 Jan 2017 14:52:14 -0500 Subject: [PATCH] 47 --- .idea/libraries/Dart_Packages.xml | 60 ++++---- .idea/workspace.xml | 236 +++++++++++++++-------------- README.md | 2 +- lib/src/http/controller.dart | 26 +++- lib/src/http/response_context.dart | 6 +- lib/src/http/server.dart | 5 + pubspec.yaml | 2 +- 7 files changed, 185 insertions(+), 152 deletions(-) diff --git a/.idea/libraries/Dart_Packages.xml b/.idea/libraries/Dart_Packages.xml index 36d08e55..11a357af 100644 --- a/.idea/libraries/Dart_Packages.xml +++ b/.idea/libraries/Dart_Packages.xml @@ -5,14 +5,14 @@ - - @@ -40,7 +40,7 @@ - @@ -61,7 +61,7 @@ - @@ -103,7 +103,7 @@ - @@ -124,7 +124,7 @@ - @@ -145,14 +145,7 @@ - - - - - - - @@ -208,7 +201,7 @@ - @@ -236,7 +229,7 @@ - @@ -264,21 +257,21 @@ - - - @@ -306,7 +299,7 @@ - @@ -348,28 +341,27 @@ - - + + - + - + - + - + - - + @@ -377,21 +369,21 @@ - + - + - - - + + + - + diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 34d3d406..721e5e7e 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -2,6 +2,7 @@ + @@ -38,8 +39,8 @@ - - + + @@ -48,22 +49,19 @@ - - + + - - + + - - - - - - + + + @@ -71,15 +69,9 @@ - - - - - - - - - + + + @@ -107,7 +99,7 @@ - + @@ -119,9 +111,11 @@ - - - + + + + + @@ -158,7 +152,6 @@ @@ -177,7 +171,7 @@ @@ -199,8 +193,6 @@ - - @@ -283,6 +275,8 @@ + + @@ -381,6 +375,7 @@ + 1481237183504 @@ -460,7 +455,7 @@ - @@ -472,29 +467,29 @@ - + + - - - + + @@ -517,11 +512,36 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + - @@ -529,7 +549,6 @@ - @@ -546,7 +565,6 @@ - @@ -566,7 +584,7 @@ - + @@ -609,7 +627,9 @@ - + + + @@ -617,7 +637,6 @@ - @@ -643,7 +662,7 @@ - + @@ -693,7 +712,9 @@ - + + + @@ -730,7 +751,9 @@ - + + + @@ -738,7 +761,6 @@ - @@ -764,20 +786,11 @@ - - - - - - - - - @@ -795,7 +808,6 @@ - @@ -809,6 +821,41 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -817,6 +864,26 @@ + + + + + + + + + + + + + + + + + + + + @@ -827,72 +894,21 @@ - - - - - - - - - - - - - - - - - - + + - + - - - - - - - - - - - - - - - - - - - - - - - - - - + + - - - - - - - - - - - \ No newline at end of file diff --git a/README.md b/README.md index 0b83171b..276b7ea6 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # angel_framework -[![pub 1.0.0-dev.46](https://img.shields.io/badge/pub-1.0.0--dev.46-red.svg)](https://pub.dartlang.org/packages/angel_framework) +[![pub 1.0.0-dev.47](https://img.shields.io/badge/pub-1.0.0--dev.47-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. diff --git a/lib/src/http/controller.dart b/lib/src/http/controller.dart index 7f2fad0a..d0f0f401 100644 --- a/lib/src/http/controller.dart +++ b/lib/src/http/controller.dart @@ -19,6 +19,9 @@ import 'server.dart' show Angel, preInject; class InjectionRequest { /// A list of the data required for a DI-enabled method to run. final List required = []; + + /// A list of the data that can be null in a DI-enabled method. + final List optional = []; } /// Supports grouping routes with shared functionality. @@ -95,10 +98,13 @@ class Controller { return; } - routeMappings[name] = routable.addRoute( - exposeDecl.method, - exposeDecl.path, - handleContained(reflectedMethod, preInject(reflectedMethod)), + var injection = preInject(reflectedMethod); + + if (exposeDecl?.allowNull?.isNotEmpty == true) + injection.optional?.addAll(exposeDecl.allowNull); + + routeMappings[name] = routable.addRoute(exposeDecl.method, + exposeDecl.path, handleContained(reflectedMethod, injection), middleware: middleware); } }; @@ -114,6 +120,16 @@ class Controller { .firstWhere((r) => r is Expose, orElse: () => null); } +/// Shortcut for calling [preInject], and then [handleContained]. +/// +/// Use this to instantly create a request handler for a DI-enabled method. +RequestHandler createDynamicHandler(handler, + {Iterable optional: const []}) { + var injection = preInject(handler); + injection.optional.addAll(optional ?? []); + return handleContained(handler, injection); +} + /// Handles a request with a DI-enabled handler. RequestHandler handleContained(handler, InjectionRequest injection) { return (RequestContext req, ResponseContext res) async { @@ -129,6 +145,8 @@ RequestHandler handleContained(handler, InjectionRequest injection) { args.add(req.params[requirement]); } else if (req.injections.containsKey(requirement)) args.add(req.injections[requirement]); + else if (injection.optional.contains(requirement)) + args.add(null); else { throw new ArgumentError( "Cannot resolve parameter '$requirement' within handler."); diff --git a/lib/src/http/response_context.dart b/lib/src/http/response_context.dart index 17682c55..933b9f13 100644 --- a/lib/src/http/response_context.dart +++ b/lib/src/http/response_context.dart @@ -32,7 +32,7 @@ class ResponseContext extends Extensible { final List cookies = []; /// Headers that will be sent to the user. - final Map headers = {}; + final Map headers = {HttpHeaders.SERVER: 'angel'}; /// Serializes response data into a String. /// @@ -230,7 +230,9 @@ class ResponseContext extends Extensible { /// /// [contentType] can be either a [String], or a [ContentType]. void serialize(value, {contentType}) { - write(serializer(value)); + var text = serializer(value); + write(text); + headers[HttpHeaders.CONTENT_LENGTH] = text.length.toString(); if (contentType is String) headers[HttpHeaders.CONTENT_TYPE] = contentType; diff --git a/lib/src/http/server.dart b/lib/src/http/server.dart index 1a54a982..3656276c 100644 --- a/lib/src/http/server.dart +++ b/lib/src/http/server.dart @@ -302,6 +302,10 @@ class Angel extends AngelBase { } void _walk(Router router) { + if (router is Angel) { + router..before.forEach(_add)..after.forEach(_add); + } + router.requestMiddleware.forEach((k, v) => _add(v)); router.middleware.forEach(_add); router.routes @@ -365,6 +369,7 @@ class Angel extends AngelBase { if (routable is Angel) { _children.add(routable.._parent = this); + _preContained.addAll(routable._preContained); if (routable.before.isNotEmpty) { all(path, (req, res) { diff --git a/pubspec.yaml b/pubspec.yaml index c1619e2a..8a3becd1 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,5 +1,5 @@ name: angel_framework -version: 1.0.0-dev.46 +version: 1.0.0-dev.47 description: Core libraries for the Angel framework. author: Tobe O homepage: https://github.com/angel-dart/angel_framework