Removed debug property
This commit is contained in:
parent
bf9e297a92
commit
81745ebef5
10 changed files with 41 additions and 70 deletions
|
@ -42,4 +42,7 @@ take routes in the form of `FutureOr myFunc(RequestContext, ResponseContext res)
|
||||||
* `@Expose.middleware` now takes `Iterable<RequestHandler>`, instead of just `List`.
|
* `@Expose.middleware` now takes `Iterable<RequestHandler>`, instead of just `List`.
|
||||||
* `createDynamicHandler` was renamed to `ioc`, and is now used to run IoC-aware handlers in a
|
* `createDynamicHandler` was renamed to `ioc`, and is now used to run IoC-aware handlers in a
|
||||||
type-safe manner.
|
type-safe manner.
|
||||||
* `RequestContext.params` is now a `Map<String, dynamic>`, rather than just a `Map`.
|
* `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.
|
|
@ -1,12 +1,11 @@
|
||||||
part of angel_framework.http.request_context;
|
part of angel_framework.http.request_context;
|
||||||
|
|
||||||
const List<Type> _primitiveTypes = [String, int, num, double, Null];
|
const List<Type> _primitiveTypes = [String, int, num, double, Null];
|
||||||
|
|
||||||
/// Shortcut for calling [preInject], and then [handleContained].
|
/// Shortcut for calling [preInject], and then [handleContained].
|
||||||
///
|
///
|
||||||
/// Use this to instantly create a request handler for a DI-enabled method.
|
/// Use this to instantly create a request handler for a DI-enabled method.
|
||||||
RequestHandler ioc(Function handler,
|
RequestHandler ioc(Function handler, {Iterable<String> optional: const []}) {
|
||||||
{Iterable<String> optional: const []}) {
|
|
||||||
var injection = preInject(handler);
|
var injection = preInject(handler);
|
||||||
injection.optional.addAll(optional ?? []);
|
injection.optional.addAll(optional ?? []);
|
||||||
return handleContained(handler, injection);
|
return handleContained(handler, injection);
|
||||||
|
@ -29,11 +28,7 @@ resolveInjection(requirement, InjectionRequest injection, RequestContext req,
|
||||||
} else if (requirement is String) {
|
} else if (requirement is String) {
|
||||||
if (req.params.containsKey(requirement)) {
|
if (req.params.containsKey(requirement)) {
|
||||||
return req.params[requirement];
|
return req.params[requirement];
|
||||||
} else if (req._injections.containsKey(requirement))
|
} else if ((propFromApp = req.app.findProperty(requirement)) != null)
|
||||||
return req._injections[requirement];
|
|
||||||
else if (req.properties.containsKey(requirement))
|
|
||||||
return req.properties[requirement];
|
|
||||||
else if ((propFromApp = req.app.findProperty(requirement)) != null)
|
|
||||||
return propFromApp;
|
return propFromApp;
|
||||||
else if (injection.optional.contains(requirement))
|
else if (injection.optional.contains(requirement))
|
||||||
return null;
|
return null;
|
||||||
|
@ -48,18 +43,13 @@ resolveInjection(requirement, InjectionRequest injection, RequestContext req,
|
||||||
String key = requirement.first;
|
String key = requirement.first;
|
||||||
Type type = requirement.last;
|
Type type = requirement.last;
|
||||||
if (req.params.containsKey(key) ||
|
if (req.params.containsKey(key) ||
|
||||||
req._injections.containsKey(key) ||
|
|
||||||
req.properties.containsKey(key) ||
|
|
||||||
req.app.configuration.containsKey(key) ||
|
req.app.configuration.containsKey(key) ||
|
||||||
_primitiveTypes.contains(type)) {
|
_primitiveTypes.contains(type)) {
|
||||||
return resolveInjection(key, injection, req, res, throwOnUnresolved);
|
return resolveInjection(key, injection, req, res, throwOnUnresolved);
|
||||||
} else
|
} else
|
||||||
return resolveInjection(type, injection, req, res, throwOnUnresolved);
|
return resolveInjection(type, injection, req, res, throwOnUnresolved);
|
||||||
} else if (requirement is Type && requirement != dynamic) {
|
} else if (requirement is Type && requirement != dynamic) {
|
||||||
if (req._injections.containsKey(requirement))
|
return req.app.container.make(requirement);
|
||||||
return req._injections[requirement];
|
|
||||||
else
|
|
||||||
return req.app.container.make(requirement);
|
|
||||||
} else if (throwOnUnresolved) {
|
} else if (throwOnUnresolved) {
|
||||||
throw new ArgumentError(
|
throw new ArgumentError(
|
||||||
'$requirement cannot be injected into a request handler.');
|
'$requirement cannot be injected into a request handler.');
|
||||||
|
|
|
@ -24,8 +24,6 @@ abstract class RequestContext<RawRequest> {
|
||||||
BodyParseResult _body;
|
BodyParseResult _body;
|
||||||
Map _provisionalQuery;
|
Map _provisionalQuery;
|
||||||
|
|
||||||
final Map properties = {};
|
|
||||||
|
|
||||||
/// The underlying [RawRequest] provided by the driver.
|
/// The underlying [RawRequest] provided by the driver.
|
||||||
RawRequest get rawRequest;
|
RawRequest get rawRequest;
|
||||||
|
|
||||||
|
@ -87,31 +85,6 @@ abstract class RequestContext<RawRequest> {
|
||||||
/// Includes the leading `.`, if there is one.
|
/// Includes the leading `.`, if there is one.
|
||||||
String get extension => _extensionCache ??= p.extension(uri.path);
|
String get extension => _extensionCache ??= p.extension(uri.path);
|
||||||
|
|
||||||
/// Grabs an object by key or type from [params], [_injections], or
|
|
||||||
/// [app].container. Use this to perform dependency injection
|
|
||||||
/// within a service hook.
|
|
||||||
T grab<T>(key) {
|
|
||||||
if (params.containsKey(key))
|
|
||||||
return params[key] as T;
|
|
||||||
else if (_injections.containsKey(key))
|
|
||||||
return _injections[key] as T;
|
|
||||||
else if (properties.containsKey(key))
|
|
||||||
return properties[key] as T;
|
|
||||||
else {
|
|
||||||
var prop = app?.findProperty(key);
|
|
||||||
if (prop != null)
|
|
||||||
return prop as T;
|
|
||||||
else if (key is Type) {
|
|
||||||
try {
|
|
||||||
return app.container.make(key) as T;
|
|
||||||
} catch (e) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
} else
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Returns `true` if the client's `Accept` header indicates that the given [contentType] is considered a valid response.
|
/// Returns `true` if the client's `Accept` header indicates that the given [contentType] is considered a valid response.
|
||||||
///
|
///
|
||||||
/// You cannot provide a `null` [contentType].
|
/// You cannot provide a `null` [contentType].
|
||||||
|
@ -148,7 +121,8 @@ abstract class RequestContext<RawRequest> {
|
||||||
Future<Map> parseBody() => parse().then((b) => b.body);
|
Future<Map> parseBody() => parse().then((b) => b.body);
|
||||||
|
|
||||||
/// Retrieves a list of all uploaded files if it has already been parsed, or lazy-parses it before returning the files.
|
/// Retrieves a list of all uploaded files if it has already been parsed, or lazy-parses it before returning the files.
|
||||||
Future<List<FileUploadInfo>> parseUploadedFiles() => parse().then((b) => b.files);
|
Future<List<FileUploadInfo>> parseUploadedFiles() =>
|
||||||
|
parse().then((b) => b.files);
|
||||||
|
|
||||||
/// Retrieves the original request buffer if it has already been parsed, or lazy-parses it before returning the buffer..
|
/// Retrieves the original request buffer if it has already been parsed, or lazy-parses it before returning the buffer..
|
||||||
///
|
///
|
||||||
|
@ -185,8 +159,6 @@ abstract class RequestContext<RawRequest> {
|
||||||
_acceptsAllCache = null;
|
_acceptsAllCache = null;
|
||||||
_acceptHeaderCache = null;
|
_acceptHeaderCache = null;
|
||||||
_provisionalQuery?.clear();
|
_provisionalQuery?.clear();
|
||||||
properties.clear();
|
|
||||||
_injections.clear();
|
|
||||||
serviceParams.clear();
|
serviceParams.clear();
|
||||||
params.clear();
|
params.clear();
|
||||||
return new Future.value();
|
return new Future.value();
|
||||||
|
|
|
@ -9,7 +9,6 @@ import 'package:angel_route/angel_route.dart';
|
||||||
import 'package:file/file.dart';
|
import 'package:file/file.dart';
|
||||||
import 'package:http_parser/http_parser.dart';
|
import 'package:http_parser/http_parser.dart';
|
||||||
import 'package:mime/mime.dart';
|
import 'package:mime/mime.dart';
|
||||||
import 'package:pool/pool.dart';
|
|
||||||
|
|
||||||
import '../http/http.dart';
|
import '../http/http.dart';
|
||||||
import 'request_context.dart';
|
import 'request_context.dart';
|
||||||
|
@ -18,7 +17,8 @@ import 'server.dart' show Angel;
|
||||||
final RegExp _straySlashes = new RegExp(r'(^/+)|(/+$)');
|
final RegExp _straySlashes = new RegExp(r'(^/+)|(/+$)');
|
||||||
|
|
||||||
/// A convenience wrapper around an outgoing HTTP request.
|
/// A convenience wrapper around an outgoing HTTP request.
|
||||||
abstract class ResponseContext<RawResponse> implements StreamSink<List<int>>, StringSink {
|
abstract class ResponseContext<RawResponse>
|
||||||
|
implements StreamSink<List<int>>, StringSink {
|
||||||
final Map properties = {};
|
final Map properties = {};
|
||||||
final BytesBuilder _buffer = new _LockableBytesBuilder();
|
final BytesBuilder _buffer = new _LockableBytesBuilder();
|
||||||
final Map<String, String> _headers = {'server': 'angel'};
|
final Map<String, String> _headers = {'server': 'angel'};
|
||||||
|
@ -313,12 +313,9 @@ abstract class ResponseContext<RawResponse> implements StreamSink<List<int>>, St
|
||||||
|
|
||||||
/// Releases critical resources from the [correspondingRequest].
|
/// Releases critical resources from the [correspondingRequest].
|
||||||
void releaseCorrespondingRequest() {
|
void releaseCorrespondingRequest() {
|
||||||
if (correspondingRequest?.injections?.containsKey(Stopwatch) == true) {
|
if (!correspondingRequest.app.isProduction &&
|
||||||
(correspondingRequest.injections[Stopwatch] as Stopwatch).stop();
|
correspondingRequest.app.logger != null) {
|
||||||
}
|
correspondingRequest.container.make<Stopwatch>().stop();
|
||||||
|
|
||||||
if (correspondingRequest?.injections?.containsKey(PoolResource) == true) {
|
|
||||||
(correspondingRequest.injections[PoolResource] as PoolResource).release();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -34,7 +34,8 @@ class Angel extends Routable {
|
||||||
(String view, [Map data]) => 'No view engine has been configured yet.';
|
(String view, [Map data]) => 'No view engine has been configured yet.';
|
||||||
|
|
||||||
final List<Angel> _children = [];
|
final List<Angel> _children = [];
|
||||||
final Map<String, Tuple3<List, Map, ParseResult<Map<String, dynamic>>>>
|
final Map<String,
|
||||||
|
Tuple3<List, Map<String, dynamic>, ParseResult<Map<String, dynamic>>>>
|
||||||
handlerCache = new HashMap();
|
handlerCache = new HashMap();
|
||||||
|
|
||||||
Router _flattened;
|
Router _flattened;
|
||||||
|
|
|
@ -8,7 +8,6 @@ import 'package:merge_map/merge_map.dart';
|
||||||
import '../util.dart';
|
import '../util.dart';
|
||||||
import 'hooked_service.dart' show HookedService;
|
import 'hooked_service.dart' show HookedService;
|
||||||
import 'metadata.dart';
|
import 'metadata.dart';
|
||||||
import 'request_context.dart';
|
|
||||||
import 'response_context.dart';
|
import 'response_context.dart';
|
||||||
import 'routable.dart';
|
import 'routable.dart';
|
||||||
import 'server.dart';
|
import 'server.dart';
|
||||||
|
|
|
@ -8,14 +8,16 @@ import 'dart:io'
|
||||||
HttpServer,
|
HttpServer,
|
||||||
Platform,
|
Platform,
|
||||||
SecurityContext;
|
SecurityContext;
|
||||||
|
|
||||||
import 'package:angel_http_exception/angel_http_exception.dart';
|
import 'package:angel_http_exception/angel_http_exception.dart';
|
||||||
import 'package:angel_route/angel_route.dart';
|
import 'package:angel_route/angel_route.dart';
|
||||||
import 'package:combinator/combinator.dart';
|
import 'package:combinator/combinator.dart';
|
||||||
import 'package:stack_trace/stack_trace.dart';
|
import 'package:stack_trace/stack_trace.dart';
|
||||||
import 'package:tuple/tuple.dart';
|
import 'package:tuple/tuple.dart';
|
||||||
|
|
||||||
|
import '../core/core.dart';
|
||||||
import 'http_request_context.dart';
|
import 'http_request_context.dart';
|
||||||
import 'http_response_context.dart';
|
import 'http_response_context.dart';
|
||||||
import '../core/core.dart';
|
|
||||||
|
|
||||||
final RegExp _straySlashes = new RegExp(r'(^/+)|(/+$)');
|
final RegExp _straySlashes = new RegExp(r'(^/+)|(/+$)');
|
||||||
|
|
||||||
|
@ -60,7 +62,7 @@ class AngelHttp {
|
||||||
/// the server.
|
/// the server.
|
||||||
factory AngelHttp.secure(
|
factory AngelHttp.secure(
|
||||||
Angel app, String certificateChainPath, String serverKeyPath,
|
Angel app, String certificateChainPath, String serverKeyPath,
|
||||||
{bool debug: false, String password, bool useZone: true}) {
|
{ String password, bool useZone: true}) {
|
||||||
var certificateChain =
|
var certificateChain =
|
||||||
Platform.script.resolve(certificateChainPath).toFilePath();
|
Platform.script.resolve(certificateChainPath).toFilePath();
|
||||||
var serverKey = Platform.script.resolve(serverKeyPath).toFilePath();
|
var serverKey = Platform.script.resolve(serverKeyPath).toFilePath();
|
||||||
|
@ -107,14 +109,16 @@ class AngelHttp {
|
||||||
var path = req.path;
|
var path = req.path;
|
||||||
if (path == '/') path = '';
|
if (path == '/') path = '';
|
||||||
|
|
||||||
Tuple3<List, Map, ParseResult<Map<String, dynamic>>> resolveTuple() {
|
Tuple3<List, Map<String, dynamic>, ParseResult<Map<String, dynamic>>>
|
||||||
|
resolveTuple() {
|
||||||
Router r = app.optimizedRouter;
|
Router r = app.optimizedRouter;
|
||||||
var resolved =
|
var resolved =
|
||||||
r.resolveAbsolute(path, method: req.method, strip: false);
|
r.resolveAbsolute(path, method: req.method, strip: false);
|
||||||
|
|
||||||
return new Tuple3(
|
return new Tuple3(
|
||||||
new MiddlewarePipeline(resolved).handlers,
|
new MiddlewarePipeline(resolved).handlers,
|
||||||
resolved.fold<Map>({}, (out, r) => out..addAll(r.allParams)),
|
resolved.fold<Map<String, dynamic>>(
|
||||||
|
<String, dynamic>{}, (out, r) => out..addAll(r.allParams)),
|
||||||
resolved.isEmpty ? null : resolved.first.parseResult,
|
resolved.isEmpty ? null : resolved.first.parseResult,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -125,10 +129,15 @@ class AngelHttp {
|
||||||
: resolveTuple();
|
: resolveTuple();
|
||||||
|
|
||||||
req.params.addAll(tuple.item2);
|
req.params.addAll(tuple.item2);
|
||||||
req.inject(ParseResult, tuple.item3);
|
|
||||||
|
|
||||||
if (!app.isProduction && app.logger != null)
|
req.container.registerSingleton<ParseResult<Map<String, dynamic>>>(
|
||||||
req.inject(Stopwatch, new Stopwatch()..start());
|
tuple.item3);
|
||||||
|
req.container.registerSingleton<ParseResult>(tuple.item3);
|
||||||
|
|
||||||
|
if (!app.isProduction && app.logger != null) {
|
||||||
|
req.container
|
||||||
|
.registerSingleton<Stopwatch>(new Stopwatch()..start());
|
||||||
|
}
|
||||||
|
|
||||||
var pipeline = tuple.item1;
|
var pipeline = tuple.item1;
|
||||||
|
|
||||||
|
@ -215,8 +224,9 @@ class AngelHttp {
|
||||||
);
|
);
|
||||||
|
|
||||||
var zone = Zone.current.fork(specification: zoneSpec);
|
var zone = Zone.current.fork(specification: zoneSpec);
|
||||||
req.inject(Zone, zone);
|
req.container.registerSingleton<Zone>(zone);
|
||||||
req.inject(ZoneSpecification, zoneSpec);
|
req.container.registerSingleton<ZoneSpecification>(zoneSpec);
|
||||||
|
|
||||||
return zone.run(handle).whenComplete(() {
|
return zone.run(handle).whenComplete(() {
|
||||||
res.dispose();
|
res.dispose();
|
||||||
});
|
});
|
||||||
|
@ -321,7 +331,7 @@ class AngelHttp {
|
||||||
|
|
||||||
return request.response.close().then((_) {
|
return request.response.close().then((_) {
|
||||||
if (!app.isProduction && app.logger != null) {
|
if (!app.isProduction && app.logger != null) {
|
||||||
var sw = req.grab<Stopwatch>(Stopwatch);
|
var sw = req.container.make<Stopwatch>();
|
||||||
|
|
||||||
if (sw.isRunning) {
|
if (sw.isRunning) {
|
||||||
sw?.stop();
|
sw?.stop();
|
||||||
|
|
|
@ -15,8 +15,6 @@ class Controller {
|
||||||
/// The [Angel] application powering this controller.
|
/// The [Angel] application powering this controller.
|
||||||
Angel get app => _app;
|
Angel get app => _app;
|
||||||
|
|
||||||
final bool debug;
|
|
||||||
|
|
||||||
/// If `true` (default), this class will inject itself as a singleton into the [app]'s container when bootstrapped.
|
/// If `true` (default), this class will inject itself as a singleton into the [app]'s container when bootstrapped.
|
||||||
final bool injectSingleton;
|
final bool injectSingleton;
|
||||||
|
|
||||||
|
@ -26,7 +24,7 @@ class Controller {
|
||||||
/// A mapping of route paths to routes, produced from the [Expose] annotations on this class.
|
/// A mapping of route paths to routes, produced from the [Expose] annotations on this class.
|
||||||
Map<String, Route> routeMappings = {};
|
Map<String, Route> routeMappings = {};
|
||||||
|
|
||||||
Controller({this.debug: false, this.injectSingleton: true});
|
Controller({this.injectSingleton: true});
|
||||||
|
|
||||||
@mustCallSuper
|
@mustCallSuper
|
||||||
Future configureServer(Angel app) {
|
Future configureServer(Angel app) {
|
||||||
|
|
|
@ -12,7 +12,7 @@ main() {
|
||||||
|
|
||||||
setUp(() {
|
setUp(() {
|
||||||
app = new Angel(reflector: MirrorsReflector())
|
app = new Angel(reflector: MirrorsReflector())
|
||||||
..inject('global', 305); // Pitbull!
|
..configuration['global'] = 305; // Pitbull!
|
||||||
http = new AngelHttp(app);
|
http = new AngelHttp(app);
|
||||||
|
|
||||||
app.get('/string/:string', ioc((String string) => string));
|
app.get('/string/:string', ioc((String string) => string));
|
||||||
|
|
|
@ -89,7 +89,8 @@ main() {
|
||||||
});
|
});
|
||||||
|
|
||||||
test('global injection added to injection map', () async {
|
test('global injection added to injection map', () async {
|
||||||
var app = new Angel(reflector: MirrorsReflector())..inject('a', 'b');
|
var app = new Angel(reflector: MirrorsReflector())
|
||||||
|
..configuration['a'] = 'b';
|
||||||
var http = new AngelHttp(app);
|
var http = new AngelHttp(app);
|
||||||
app.get('/', ioc((String a) => a));
|
app.get('/', ioc((String a) => a));
|
||||||
var rq = new MockHttpRequest('GET', Uri.parse('/'))..close();
|
var rq = new MockHttpRequest('GET', Uri.parse('/'))..close();
|
||||||
|
|
Loading…
Reference in a new issue