2016-09-15 19:53:01 +00:00
|
|
|
library angel_framework.http.server;
|
|
|
|
|
|
|
|
import 'dart:async';
|
2017-11-28 18:14:50 +00:00
|
|
|
import 'dart:collection' show HashMap;
|
2017-08-15 23:01:16 +00:00
|
|
|
import 'dart:convert';
|
2016-09-15 19:53:01 +00:00
|
|
|
import 'dart:io';
|
2018-08-19 15:33:25 +00:00
|
|
|
import 'package:angel_container/angel_container.dart';
|
|
|
|
import 'package:angel_container/mirrors.dart';
|
2017-09-22 04:48:22 +00:00
|
|
|
import 'package:angel_http_exception/angel_http_exception.dart';
|
2017-11-28 18:14:50 +00:00
|
|
|
import 'package:angel_route/angel_route.dart';
|
|
|
|
import 'package:combinator/combinator.dart';
|
2017-09-22 04:48:22 +00:00
|
|
|
import 'package:logging/logging.dart';
|
2017-08-28 15:29:27 +00:00
|
|
|
import 'package:tuple/tuple.dart';
|
2018-02-07 05:44:21 +00:00
|
|
|
import '../http/http.dart';
|
2016-09-15 19:53:01 +00:00
|
|
|
import 'angel_base.dart';
|
|
|
|
import 'request_context.dart';
|
|
|
|
import 'response_context.dart';
|
|
|
|
import 'routable.dart';
|
|
|
|
import 'service.dart';
|
2016-02-28 13:11:17 +00:00
|
|
|
|
2016-10-22 20:41:36 +00:00
|
|
|
final RegExp _straySlashes = new RegExp(r'(^/+)|(/+$)');
|
|
|
|
|
2016-04-18 03:27:23 +00:00
|
|
|
/// A function that binds an [Angel] server to an Internet address and port.
|
2018-02-07 04:17:40 +00:00
|
|
|
///
|
|
|
|
/// Prefer the Future<HttpServer> Function(dynamic, int) syntax.
|
|
|
|
@deprecated
|
2017-10-04 14:09:12 +00:00
|
|
|
typedef Future<HttpServer> ServerGenerator(address, int port);
|
2016-02-28 13:11:17 +00:00
|
|
|
|
2017-02-25 00:16:31 +00:00
|
|
|
/// A function that configures an [Angel] server in some way.
|
2018-06-08 07:06:26 +00:00
|
|
|
typedef FutureOr AngelConfigurer(Angel app);
|
2017-02-25 00:16:31 +00:00
|
|
|
|
2016-02-28 13:11:17 +00:00
|
|
|
/// A powerful real-time/REST/MVC server class.
|
2016-09-15 19:53:01 +00:00
|
|
|
class Angel extends AngelBase {
|
2016-12-21 18:18:26 +00:00
|
|
|
final List<Angel> _children = [];
|
2018-06-23 03:29:38 +00:00
|
|
|
final Map<String, Tuple3<List, Map, ParseResult<Map<String, dynamic>>>>
|
2017-11-28 18:14:50 +00:00
|
|
|
handlerCache = new HashMap();
|
2017-08-15 23:01:16 +00:00
|
|
|
|
2017-04-01 01:00:24 +00:00
|
|
|
Router _flattened;
|
2018-02-07 03:46:52 +00:00
|
|
|
AngelHttp _http;
|
2017-11-28 18:14:50 +00:00
|
|
|
bool _isProduction;
|
2016-12-21 18:18:26 +00:00
|
|
|
Angel _parent;
|
2016-09-17 16:12:25 +00:00
|
|
|
|
2017-08-15 23:01:16 +00:00
|
|
|
/// A global Map of converters that can transform responses bodies.
|
|
|
|
final Map<String, Converter<List<int>, List<int>>> encoders = {};
|
|
|
|
|
2017-09-22 04:48:22 +00:00
|
|
|
final Map _injections = {};
|
|
|
|
|
2018-02-07 04:13:26 +00:00
|
|
|
@deprecated
|
2017-09-22 04:48:22 +00:00
|
|
|
Future<ZoneSpecification> Function(
|
|
|
|
HttpRequest request, RequestContext req, ResponseContext res)
|
|
|
|
createZoneForRequest;
|
2017-08-15 23:01:16 +00:00
|
|
|
|
2016-12-31 01:46:41 +00:00
|
|
|
final Map<dynamic, InjectionRequest> _preContained = {};
|
2018-02-07 03:48:26 +00:00
|
|
|
|
|
|
|
/// A middleware to inject a serialize on every request.
|
2018-02-07 03:54:24 +00:00
|
|
|
String Function(dynamic) serializer;
|
2016-12-31 01:46:41 +00:00
|
|
|
|
2017-06-06 12:42:33 +00:00
|
|
|
/// A [Map] of dependency data obtained via reflection.
|
|
|
|
///
|
|
|
|
/// You may modify this [Map] yourself if you intend to avoid reflection entirely.
|
|
|
|
Map<dynamic, InjectionRequest> get preContained => _preContained;
|
|
|
|
|
2018-02-07 03:51:52 +00:00
|
|
|
/// Returns the [flatten]ed version of this router in production.
|
|
|
|
Router get optimizedRouter => _flattened ?? this;
|
|
|
|
|
2017-03-02 22:06:02 +00:00
|
|
|
/// Determines whether to allow HTTP request method overrides.
|
|
|
|
bool allowMethodOverrides = true;
|
|
|
|
|
2016-12-21 18:18:26 +00:00
|
|
|
/// All child application mounted on this instance.
|
|
|
|
List<Angel> get children => new List<Angel>.unmodifiable(_children);
|
|
|
|
|
2017-10-04 14:09:12 +00:00
|
|
|
final Map<Pattern, Controller> _controllers = {};
|
|
|
|
|
|
|
|
/// A set of [Controller] objects that have been loaded into the application.
|
|
|
|
Map<Pattern, Controller> get controllers => _controllers;
|
|
|
|
|
2016-12-21 18:18:26 +00:00
|
|
|
/// Indicates whether the application is running in a production environment.
|
|
|
|
///
|
|
|
|
/// The criteria for this is the `ANGEL_ENV` environment variable being set to
|
|
|
|
/// `'production'`.
|
2017-04-01 01:00:24 +00:00
|
|
|
///
|
|
|
|
/// This value is memoized the first time you call it, so do not change environment
|
|
|
|
/// configuration at runtime!
|
|
|
|
bool get isProduction {
|
2017-11-28 18:14:50 +00:00
|
|
|
return _isProduction ??=
|
|
|
|
(Platform.environment['ANGEL_ENV'] == 'production');
|
2017-04-01 01:00:24 +00:00
|
|
|
}
|
2016-12-21 18:18:26 +00:00
|
|
|
|
2018-02-07 04:17:40 +00:00
|
|
|
/// Use the serving methods in [AngelHttp] instead.
|
|
|
|
@deprecated
|
2018-02-07 04:34:08 +00:00
|
|
|
ServerGenerator get serverGenerator =>
|
|
|
|
(_http ??= new AngelHttp(this)).serverGenerator;
|
2017-03-28 23:29:22 +00:00
|
|
|
|
2016-12-21 18:18:26 +00:00
|
|
|
/// Returns the parent instance of this application, if any.
|
|
|
|
Angel get parent => _parent;
|
|
|
|
|
2017-09-22 04:48:22 +00:00
|
|
|
/// Outputs diagnostics and debug messages.
|
|
|
|
Logger logger;
|
|
|
|
|
2017-02-01 21:43:18 +00:00
|
|
|
/// Plug-ins to be called right before server startup.
|
|
|
|
///
|
|
|
|
/// If the server is never started, they will never be called.
|
2017-09-22 04:48:22 +00:00
|
|
|
final List<AngelConfigurer> startupHooks = [];
|
2017-02-01 21:43:18 +00:00
|
|
|
|
2017-09-22 04:48:22 +00:00
|
|
|
/// Plug-ins to be called right before server shutdown.
|
2017-04-04 08:35:36 +00:00
|
|
|
///
|
|
|
|
/// If the server is never [close]d, they will never be called.
|
2017-09-22 04:48:22 +00:00
|
|
|
final List<AngelConfigurer> shutdownHooks = [];
|
2017-04-04 08:35:36 +00:00
|
|
|
|
2016-12-19 01:38:23 +00:00
|
|
|
/// Always run before responses are sent.
|
|
|
|
///
|
2017-09-22 14:03:23 +00:00
|
|
|
/// These will only not run if a response's `willCloseItself` is set to `true`.
|
2016-12-19 01:38:23 +00:00
|
|
|
final List<RequestHandler> responseFinalizers = [];
|
|
|
|
|
2018-02-07 03:46:52 +00:00
|
|
|
/// All global dependencies injected into the application.
|
|
|
|
Map get injections => _injections;
|
|
|
|
|
2017-10-04 14:09:12 +00:00
|
|
|
/// Use [configuration] instead.
|
|
|
|
@deprecated
|
|
|
|
Map get properties {
|
|
|
|
try {
|
|
|
|
throw new Error();
|
|
|
|
} catch (e, st) {
|
|
|
|
logger?.warning(
|
|
|
|
'`properties` is deprecated, and should not be used.',
|
|
|
|
new UnsupportedError('`properties` is deprecated.'),
|
|
|
|
st,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return configuration;
|
|
|
|
}
|
|
|
|
|
2017-03-04 21:12:39 +00:00
|
|
|
/// The handler currently configured to run on [AngelHttpException]s.
|
2017-09-22 04:48:22 +00:00
|
|
|
Function(AngelHttpException e, RequestContext req, ResponseContext res)
|
2017-10-04 14:09:12 +00:00
|
|
|
errorHandler =
|
|
|
|
(AngelHttpException e, RequestContext req, ResponseContext res) {
|
2017-12-06 14:46:35 +00:00
|
|
|
if (!req.accepts('text/html', strict: true) &&
|
2017-10-04 14:09:12 +00:00
|
|
|
(req.accepts('application/json') ||
|
|
|
|
req.accepts('application/javascript'))) {
|
|
|
|
res.json(e.toJson());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-02-07 04:11:52 +00:00
|
|
|
res.headers['content-type'] = 'text/html';
|
2016-12-19 01:38:23 +00:00
|
|
|
res.statusCode = e.statusCode;
|
2016-07-03 22:23:55 +00:00
|
|
|
res.write("<!DOCTYPE html><html><head><title>${e.message}</title>");
|
Angel.secure, fallback routes, 404, app.addRoute, app.all, services are a go (just missing params, i.e. $sort?), now have service.app, app.before, app.after, angel.configure now uses futures, errors are implemented
2016-04-29 00:01:58 +00:00
|
|
|
res.write("</head><body><h1>${e.message}</h1><ul>");
|
2016-12-31 01:46:41 +00:00
|
|
|
|
Angel.secure, fallback routes, 404, app.addRoute, app.all, services are a go (just missing params, i.e. $sort?), now have service.app, app.before, app.after, angel.configure now uses futures, errors are implemented
2016-04-29 00:01:58 +00:00
|
|
|
for (String error in e.errors) {
|
|
|
|
res.write("<li>$error</li>");
|
|
|
|
}
|
2016-12-31 01:46:41 +00:00
|
|
|
|
Angel.secure, fallback routes, 404, app.addRoute, app.all, services are a go (just missing params, i.e. $sort?), now have service.app, app.before, app.after, angel.configure now uses futures, errors are implemented
2016-04-29 00:01:58 +00:00
|
|
|
res.write("</ul></body></html>");
|
|
|
|
res.end();
|
|
|
|
};
|
|
|
|
|
2018-02-07 04:10:49 +00:00
|
|
|
/// Use the serving methods in [AngelHttp] instead.
|
|
|
|
@deprecated
|
2018-02-07 04:34:08 +00:00
|
|
|
HttpServer get httpServer => _http?.httpServer;
|
2016-04-18 03:27:23 +00:00
|
|
|
|
2018-02-07 04:10:49 +00:00
|
|
|
/// Use the serving methods in [AngelHttp] instead.
|
|
|
|
@deprecated
|
|
|
|
Future<HttpServer> startServer([address, int port]) {
|
|
|
|
_http ??= new AngelHttp(this);
|
|
|
|
return _http.startServer(address, port);
|
2016-07-04 18:06:31 +00:00
|
|
|
}
|
|
|
|
|
2017-04-01 01:00:24 +00:00
|
|
|
@override
|
2017-08-03 16:40:21 +00:00
|
|
|
Route addRoute(String method, Pattern path, Object handler,
|
2017-04-01 01:00:24 +00:00
|
|
|
{List middleware: const []}) {
|
|
|
|
if (_flattened != null) {
|
2017-09-22 14:53:49 +00:00
|
|
|
logger?.warning(
|
2017-04-02 19:14:10 +00:00
|
|
|
'WARNING: You added a route ($method $path) to the router, after it had been optimized.');
|
2017-09-22 14:53:49 +00:00
|
|
|
logger?.warning(
|
|
|
|
'This route will be ignored, and no requests will ever reach it.');
|
2017-04-01 01:00:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return super.addRoute(method, path, handler, middleware: middleware ?? []);
|
|
|
|
}
|
|
|
|
|
2017-04-02 19:14:10 +00:00
|
|
|
@override
|
|
|
|
mount(Pattern path, Router router, {bool hooked: true, String namespace}) {
|
|
|
|
if (_flattened != null) {
|
2017-09-22 14:53:49 +00:00
|
|
|
logger?.warning(
|
2017-04-02 19:14:10 +00:00
|
|
|
'WARNING: You added mounted a child router ($path) on the router, after it had been optimized.');
|
2017-09-22 14:53:49 +00:00
|
|
|
logger?.warning(
|
|
|
|
'This route will be ignored, and no requests will ever reach it.');
|
2017-04-02 19:14:10 +00:00
|
|
|
}
|
2018-06-23 03:59:41 +00:00
|
|
|
return super.mount(path.toString(), router,
|
|
|
|
hooked: hooked != false, namespace: namespace);
|
2017-04-02 19:14:10 +00:00
|
|
|
}
|
|
|
|
|
2016-09-17 16:12:25 +00:00
|
|
|
/// Loads some base dependencies into the service container.
|
|
|
|
void bootstrapContainer() {
|
2017-01-20 22:11:20 +00:00
|
|
|
if (runtimeType != Angel) container.singleton(this, as: Angel);
|
2016-09-17 16:12:25 +00:00
|
|
|
container.singleton(this, as: AngelBase);
|
2016-11-23 19:50:17 +00:00
|
|
|
container.singleton(this, as: Routable);
|
|
|
|
container.singleton(this, as: Router);
|
2016-09-17 16:12:25 +00:00
|
|
|
container.singleton(this);
|
|
|
|
}
|
|
|
|
|
2017-04-04 08:35:36 +00:00
|
|
|
/// Shuts down the server, and closes any open [StreamController]s.
|
2017-10-28 08:50:16 +00:00
|
|
|
///
|
|
|
|
/// The server will be **COMPLETE DEFUNCT** after this operation!
|
2018-06-08 07:06:26 +00:00
|
|
|
Future close() {
|
|
|
|
Future.forEach(services.values, (Service service) {
|
|
|
|
service.close();
|
2017-04-04 08:35:36 +00:00
|
|
|
});
|
|
|
|
|
2018-06-08 07:06:26 +00:00
|
|
|
super.close();
|
2017-10-28 08:50:16 +00:00
|
|
|
_preContained.clear();
|
2017-11-18 17:42:31 +00:00
|
|
|
handlerCache.clear();
|
2017-10-28 08:50:16 +00:00
|
|
|
_injections.clear();
|
|
|
|
encoders.clear();
|
2018-08-19 15:49:33 +00:00
|
|
|
//_serializer = json.encode;
|
2017-10-28 08:50:16 +00:00
|
|
|
_children.clear();
|
|
|
|
_parent = null;
|
|
|
|
logger = null;
|
|
|
|
startupHooks.clear();
|
|
|
|
shutdownHooks.clear();
|
|
|
|
responseFinalizers.clear();
|
|
|
|
_flattened = null;
|
2018-06-08 07:06:26 +00:00
|
|
|
_http?.close();
|
|
|
|
return new Future.value(_http?.httpServer);
|
2017-04-04 08:35:36 +00:00
|
|
|
}
|
|
|
|
|
2017-04-01 01:00:24 +00:00
|
|
|
@override
|
2017-08-28 15:29:27 +00:00
|
|
|
void dumpTree(
|
|
|
|
{callback(String tree),
|
|
|
|
String header: 'Dumping route tree:',
|
|
|
|
String tab: ' ',
|
|
|
|
bool showMatchers: false}) {
|
2017-04-01 01:00:24 +00:00
|
|
|
if (isProduction) {
|
2017-11-28 18:14:50 +00:00
|
|
|
_flattened ??= flatten(this);
|
2017-04-01 01:00:24 +00:00
|
|
|
|
|
|
|
_flattened.dumpTree(
|
|
|
|
callback: callback,
|
|
|
|
header: header?.isNotEmpty == true
|
|
|
|
? header
|
|
|
|
: (isProduction
|
2017-08-28 15:29:27 +00:00
|
|
|
? 'Dumping flattened route tree:'
|
|
|
|
: 'Dumping route tree:'),
|
2017-11-28 18:14:50 +00:00
|
|
|
tab: tab ?? ' ');
|
2017-04-01 01:00:24 +00:00
|
|
|
} else {
|
|
|
|
super.dumpTree(
|
|
|
|
callback: callback,
|
|
|
|
header: header?.isNotEmpty == true
|
|
|
|
? header
|
|
|
|
: (isProduction
|
2017-08-28 15:29:27 +00:00
|
|
|
? 'Dumping flattened route tree:'
|
|
|
|
: 'Dumping route tree:'),
|
2017-11-28 18:14:50 +00:00
|
|
|
tab: tab ?? ' ');
|
2017-04-01 01:00:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-06 00:52:16 +00:00
|
|
|
/// Shortcut for adding a middleware to inject a key/value pair on every request.
|
|
|
|
void inject(key, value) {
|
2017-09-22 04:48:22 +00:00
|
|
|
_injections[key] = value;
|
2017-03-06 00:52:16 +00:00
|
|
|
}
|
|
|
|
|
2017-08-15 23:01:16 +00:00
|
|
|
/// Shortcuts for adding converters to transform the response buffer/stream of any request.
|
|
|
|
void injectEncoders(Map<String, Converter<List<int>, List<int>>> encoders) {
|
|
|
|
this.encoders.addAll(encoders);
|
|
|
|
}
|
|
|
|
|
2018-02-07 03:48:26 +00:00
|
|
|
/// Prefer directly setting [serializer].
|
|
|
|
@deprecated
|
2018-02-07 03:54:24 +00:00
|
|
|
void injectSerializer(String serializer(x)) {
|
2018-02-07 03:48:26 +00:00
|
|
|
this.serializer = serializer;
|
2017-03-06 00:52:16 +00:00
|
|
|
}
|
|
|
|
|
2018-06-08 07:06:26 +00:00
|
|
|
Future getHandlerResult(handler, RequestContext req, ResponseContext res) {
|
2016-04-21 20:37:02 +00:00
|
|
|
if (handler is RequestHandler) {
|
2018-06-08 07:06:26 +00:00
|
|
|
var result = handler(req, res);
|
|
|
|
return getHandlerResult(result, req, res);
|
2016-09-17 16:12:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (handler is Future) {
|
2018-06-08 07:06:26 +00:00
|
|
|
return handler.then((result) => getHandlerResult(result, req, res));
|
2016-09-17 16:12:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (handler is Function) {
|
2018-06-08 07:06:26 +00:00
|
|
|
var result = runContained(handler, req, res);
|
|
|
|
return getHandlerResult(result, req, res);
|
2017-06-30 13:49:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (handler is Stream) {
|
2018-06-08 07:06:26 +00:00
|
|
|
return getHandlerResult(handler.toList(), req, res);
|
2016-09-17 16:12:25 +00:00
|
|
|
}
|
|
|
|
|
2017-06-06 12:42:33 +00:00
|
|
|
var middleware = (req.app ?? this).findMiddleware(handler);
|
|
|
|
if (middleware != null) {
|
2018-06-08 07:06:26 +00:00
|
|
|
return getHandlerResult(middleware, req, res);
|
2016-04-18 03:27:23 +00:00
|
|
|
}
|
2016-09-17 16:12:25 +00:00
|
|
|
|
2018-06-08 07:06:26 +00:00
|
|
|
return new Future.value(handler);
|
2017-04-15 17:42:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Runs some [handler]. Returns `true` if request execution should continue.
|
2017-08-28 15:29:27 +00:00
|
|
|
Future<bool> executeHandler(
|
2018-06-08 07:06:26 +00:00
|
|
|
handler, RequestContext req, ResponseContext res) {
|
|
|
|
return getHandlerResult(handler, req, res).then((result) {
|
|
|
|
if (result == null)
|
|
|
|
return false;
|
|
|
|
else if (result is bool) {
|
|
|
|
return result;
|
|
|
|
} else if (result != null) {
|
2018-06-10 23:17:11 +00:00
|
|
|
return res.serialize(result,
|
2018-06-08 07:06:26 +00:00
|
|
|
contentType: res.headers['content-type'] ?? 'application/json');
|
|
|
|
} else
|
|
|
|
return res.isOpen;
|
|
|
|
});
|
2016-04-18 03:27:23 +00:00
|
|
|
}
|
|
|
|
|
2018-02-07 03:46:52 +00:00
|
|
|
/// Use the serving methods in [AngelHttp] instead.
|
|
|
|
@deprecated
|
2017-01-20 22:40:48 +00:00
|
|
|
Future<RequestContext> createRequestContext(HttpRequest request) {
|
2018-02-07 04:34:08 +00:00
|
|
|
_http ??= new AngelHttp(this);
|
2018-02-07 03:46:52 +00:00
|
|
|
return _http.createRequestContext(request);
|
2017-01-20 22:40:48 +00:00
|
|
|
}
|
|
|
|
|
2018-02-07 03:46:52 +00:00
|
|
|
/// Use the serving methods in [AngelHttp] instead.
|
|
|
|
@deprecated
|
2017-08-15 23:01:16 +00:00
|
|
|
Future<ResponseContext> createResponseContext(HttpResponse response,
|
2018-02-07 04:34:08 +00:00
|
|
|
[RequestContext correspondingRequest]) {
|
|
|
|
_http ??= new AngelHttp(this);
|
|
|
|
|
|
|
|
return _http.createResponseContext(response, correspondingRequest);
|
|
|
|
}
|
2017-01-20 22:40:48 +00:00
|
|
|
|
2017-06-06 12:42:33 +00:00
|
|
|
/// Attempts to find a middleware by the given name within this application.
|
|
|
|
findMiddleware(key) {
|
|
|
|
if (requestMiddleware.containsKey(key)) return requestMiddleware[key];
|
|
|
|
return parent != null ? parent.findMiddleware(key) : null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Attempts to find a property by the given name within this application.
|
|
|
|
findProperty(key) {
|
2017-09-24 19:43:14 +00:00
|
|
|
if (configuration.containsKey(key)) return configuration[key];
|
2017-06-06 12:42:33 +00:00
|
|
|
return parent != null ? parent.findProperty(key) : null;
|
|
|
|
}
|
|
|
|
|
2018-02-07 03:46:52 +00:00
|
|
|
/// Use the serving methods in [AngelHttp] instead.
|
|
|
|
@deprecated
|
2017-09-22 04:48:22 +00:00
|
|
|
Future handleAngelHttpException(AngelHttpException e, StackTrace st,
|
2017-06-30 13:49:45 +00:00
|
|
|
RequestContext req, ResponseContext res, HttpRequest request,
|
2018-02-07 03:46:52 +00:00
|
|
|
{bool ignoreFinalizers: false}) {
|
2018-02-07 04:34:08 +00:00
|
|
|
_http ??= new AngelHttp(this);
|
2018-02-07 03:46:52 +00:00
|
|
|
return _http.handleAngelHttpException(e, st, req, res, request,
|
2017-06-30 13:49:45 +00:00
|
|
|
ignoreFinalizers: ignoreFinalizers == true);
|
2017-06-19 01:53:51 +00:00
|
|
|
}
|
|
|
|
|
2018-02-07 03:46:52 +00:00
|
|
|
/// Use the serving methods in [AngelHttp] instead.
|
|
|
|
@deprecated
|
|
|
|
Future handleRequest(HttpRequest request) {
|
2018-02-07 04:34:08 +00:00
|
|
|
_http ??= new AngelHttp(this);
|
2018-02-07 03:46:52 +00:00
|
|
|
return _http.handleRequest(request);
|
2018-02-07 03:32:31 +00:00
|
|
|
}
|
|
|
|
|
2017-04-01 01:00:24 +00:00
|
|
|
/// Runs several optimizations, *if* [isProduction] is `true`.
|
|
|
|
///
|
|
|
|
/// * Preprocesses all dependency injection, and eliminates the burden of reflecting handlers
|
2016-12-31 02:00:52 +00:00
|
|
|
/// at run-time.
|
2017-04-01 01:00:24 +00:00
|
|
|
/// * [flatten]s the route tree into a linear one.
|
2017-06-06 12:42:33 +00:00
|
|
|
///
|
|
|
|
/// You may [force] the optimization to run, if you are not running in production.
|
|
|
|
void optimizeForProduction({bool force: false}) {
|
|
|
|
if (isProduction == true || force == true) {
|
2017-06-30 13:49:45 +00:00
|
|
|
_isProduction = true;
|
2017-04-01 01:00:24 +00:00
|
|
|
_add(v) {
|
|
|
|
if (v is Function && !_preContained.containsKey(v)) {
|
|
|
|
_preContained[v] = preInject(v);
|
|
|
|
}
|
2016-12-31 02:00:52 +00:00
|
|
|
}
|
|
|
|
|
2017-04-01 01:00:24 +00:00
|
|
|
void _walk(Router router) {
|
|
|
|
router.requestMiddleware.forEach((k, v) => _add(v));
|
|
|
|
router.middleware.forEach(_add);
|
2017-06-06 12:42:33 +00:00
|
|
|
router.routes.forEach((r) {
|
|
|
|
r.handlers.forEach(_add);
|
|
|
|
if (r is SymlinkRoute) _walk(r.router);
|
|
|
|
});
|
2017-01-15 19:52:14 +00:00
|
|
|
}
|
|
|
|
|
2017-11-28 18:14:50 +00:00
|
|
|
_flattened ??= flatten(this);
|
2017-04-02 19:14:10 +00:00
|
|
|
|
|
|
|
_walk(_flattened);
|
2017-08-03 16:40:21 +00:00
|
|
|
|
2017-09-22 14:53:49 +00:00
|
|
|
logger?.config('Angel is running in production mode.');
|
2016-12-31 02:00:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-31 01:46:41 +00:00
|
|
|
/// Run a function after injecting from service container.
|
|
|
|
/// If this function has been reflected before, then
|
|
|
|
/// the execution will be faster, as the injection requirements were stored beforehand.
|
2017-08-28 15:29:27 +00:00
|
|
|
Future runContained(
|
|
|
|
Function handler, RequestContext req, ResponseContext res) {
|
2018-06-08 07:06:26 +00:00
|
|
|
return new Future.sync(() {
|
|
|
|
if (_preContained.containsKey(handler)) {
|
|
|
|
return handleContained(handler, _preContained[handler])(req, res);
|
|
|
|
}
|
2016-12-31 01:46:41 +00:00
|
|
|
|
2018-06-08 07:06:26 +00:00
|
|
|
return runReflected(handler, req, res);
|
|
|
|
});
|
2016-12-31 01:46:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Runs with DI, and *always* reflects. Prefer [runContained].
|
2017-08-28 15:29:27 +00:00
|
|
|
Future runReflected(
|
2018-06-08 07:06:26 +00:00
|
|
|
Function handler, RequestContext req, ResponseContext res) {
|
2017-01-12 01:52:06 +00:00
|
|
|
var h =
|
2017-08-28 15:29:27 +00:00
|
|
|
handleContained(handler, _preContained[handler] = preInject(handler));
|
2018-06-08 07:06:26 +00:00
|
|
|
return new Future.sync(() => h(req, res));
|
|
|
|
// return closureMirror.apply(args).reflectee;
|
2016-09-17 16:12:25 +00:00
|
|
|
}
|
|
|
|
|
2018-02-07 03:46:52 +00:00
|
|
|
/// Use the serving methods in [AngelHttp] instead.
|
|
|
|
@deprecated
|
2017-08-28 15:29:27 +00:00
|
|
|
Future sendResponse(
|
|
|
|
HttpRequest request, RequestContext req, ResponseContext res,
|
2017-08-03 16:40:21 +00:00
|
|
|
{bool ignoreFinalizers: false}) {
|
2018-02-07 04:34:08 +00:00
|
|
|
_http ??= new AngelHttp(this);
|
2018-02-07 03:46:52 +00:00
|
|
|
return _http.sendResponse(request, req, res);
|
2017-11-28 18:14:50 +00:00
|
|
|
}
|
|
|
|
|
2018-02-07 03:46:52 +00:00
|
|
|
/// Use the serving methods in [AngelHttp] instead.
|
|
|
|
@deprecated
|
2017-11-28 18:14:50 +00:00
|
|
|
void throttle(int maxConcurrentRequests, {Duration timeout}) {
|
2018-02-07 03:46:52 +00:00
|
|
|
_http?.throttle(maxConcurrentRequests, timeout: timeout);
|
2017-01-20 22:40:48 +00:00
|
|
|
}
|
|
|
|
|
2016-04-18 03:27:23 +00:00
|
|
|
/// Applies an [AngelConfigurer] to this instance.
|
2018-06-08 07:06:26 +00:00
|
|
|
Future configure(AngelConfigurer configurer) {
|
|
|
|
return new Future.sync(() => configurer(this));
|
2016-02-28 13:11:17 +00:00
|
|
|
}
|
|
|
|
|
2017-09-22 04:48:22 +00:00
|
|
|
/// Mounts the child on this router. If [routable] is `null`,
|
|
|
|
/// then this method will add a handler as a global middleware instead.
|
2016-12-21 18:18:26 +00:00
|
|
|
///
|
|
|
|
/// 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.
|
2016-12-31 01:46:41 +00:00
|
|
|
///
|
2016-12-21 18:18:26 +00:00
|
|
|
/// NOTE: The above will not be properly copied if [path] is
|
|
|
|
/// a [RegExp].
|
Angel.secure, fallback routes, 404, app.addRoute, app.all, services are a go (just missing params, i.e. $sort?), now have service.app, app.before, app.after, angel.configure now uses futures, errors are implemented
2016-04-29 00:01:58 +00:00
|
|
|
@override
|
2018-06-08 07:06:26 +00:00
|
|
|
use(path, [Router routable, String namespace = null]) {
|
2017-09-22 04:48:22 +00:00
|
|
|
if (routable == null) return all('*', path);
|
|
|
|
|
2017-01-20 22:40:48 +00:00
|
|
|
var head = path.toString().replaceAll(_straySlashes, '');
|
2016-12-21 18:18:26 +00:00
|
|
|
|
2016-11-28 00:49:27 +00:00
|
|
|
if (routable is Angel) {
|
2016-12-21 18:18:26 +00:00
|
|
|
_children.add(routable.._parent = this);
|
2017-01-15 19:52:14 +00:00
|
|
|
_preContained.addAll(routable._preContained);
|
2016-12-21 18:18:26 +00:00
|
|
|
|
|
|
|
if (routable.responseFinalizers.isNotEmpty) {
|
2018-06-08 07:06:26 +00:00
|
|
|
responseFinalizers.add((req, res) {
|
2016-12-21 18:18:26 +00:00
|
|
|
if (req.path.replaceAll(_straySlashes, '').startsWith(head)) {
|
|
|
|
for (var finalizer in routable.responseFinalizers)
|
2018-06-08 07:06:26 +00:00
|
|
|
finalizer(req, res);
|
2016-12-21 18:18:26 +00:00
|
|
|
}
|
|
|
|
|
2018-06-08 07:06:26 +00:00
|
|
|
return new Future.value(true);
|
2016-12-21 18:18:26 +00:00
|
|
|
});
|
|
|
|
}
|
2016-11-28 00:49:27 +00:00
|
|
|
|
2017-10-04 14:09:12 +00:00
|
|
|
routable._controllers.forEach((k, v) {
|
2017-01-20 22:40:48 +00:00
|
|
|
var tail = k.toString().replaceAll(_straySlashes, '');
|
2017-10-04 14:09:12 +00:00
|
|
|
_controllers['$head/$tail'.replaceAll(_straySlashes, '')] = v;
|
2016-11-28 00:49:27 +00:00
|
|
|
});
|
2016-12-21 18:18:26 +00:00
|
|
|
|
|
|
|
routable.services.forEach((k, v) {
|
2017-01-20 22:40:48 +00:00
|
|
|
var tail = k.toString().replaceAll(_straySlashes, '');
|
2016-12-21 18:18:26 +00:00
|
|
|
services['$head/$tail'.replaceAll(_straySlashes, '')] = v;
|
|
|
|
});
|
2016-11-28 00:49:27 +00:00
|
|
|
}
|
|
|
|
|
Angel.secure, fallback routes, 404, app.addRoute, app.all, services are a go (just missing params, i.e. $sort?), now have service.app, app.before, app.after, angel.configure now uses futures, errors are implemented
2016-04-29 00:01:58 +00:00
|
|
|
if (routable is Service) {
|
|
|
|
routable.app = this;
|
|
|
|
}
|
2016-10-22 20:41:36 +00:00
|
|
|
|
2017-09-22 04:48:22 +00:00
|
|
|
return super.use(path, routable, namespace);
|
Angel.secure, fallback routes, 404, app.addRoute, app.all, services are a go (just missing params, i.e. $sort?), now have service.app, app.before, app.after, angel.configure now uses futures, errors are implemented
2016-04-29 00:01:58 +00:00
|
|
|
}
|
2016-04-22 01:42:39 +00:00
|
|
|
|
2017-01-20 22:11:20 +00:00
|
|
|
/// Default constructor. ;)
|
2018-08-19 15:33:25 +00:00
|
|
|
Angel(Reflector reflector) : super(reflector) {
|
2016-09-17 16:12:25 +00:00
|
|
|
bootstrapContainer();
|
2018-02-07 04:13:26 +00:00
|
|
|
// ignore: deprecated_member_use
|
2017-11-28 18:14:50 +00:00
|
|
|
createZoneForRequest = defaultZoneCreator;
|
|
|
|
}
|
2017-09-22 04:48:22 +00:00
|
|
|
|
2018-02-07 04:13:26 +00:00
|
|
|
@deprecated
|
2018-06-08 07:06:26 +00:00
|
|
|
Future<ZoneSpecification> defaultZoneCreator(request, req, res) {
|
|
|
|
return new Future.value(
|
|
|
|
new ZoneSpecification(
|
|
|
|
print: (Zone self, ZoneDelegate parent, Zone zone, String line) {
|
|
|
|
if (logger != null) {
|
|
|
|
logger.info(line);
|
|
|
|
} else {
|
|
|
|
return parent.print(zone, line);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
),
|
2017-11-28 18:14:50 +00:00
|
|
|
);
|
2016-09-17 16:12:25 +00:00
|
|
|
}
|
2016-02-28 13:11:17 +00:00
|
|
|
|
2018-02-07 04:10:49 +00:00
|
|
|
/// Use the serving methods in [AngelHttp] instead.
|
|
|
|
@deprecated
|
2018-02-07 04:34:08 +00:00
|
|
|
factory Angel.custom(
|
|
|
|
Future<HttpServer> Function(dynamic, int) serverGenerator) {
|
2018-08-19 15:33:25 +00:00
|
|
|
var app = new Angel(MirrorsReflector());
|
2018-02-07 04:10:49 +00:00
|
|
|
return app.._http = new AngelHttp.custom(app, serverGenerator);
|
2017-09-22 04:48:22 +00:00
|
|
|
}
|
2017-01-20 22:11:20 +00:00
|
|
|
|
2018-02-07 04:10:49 +00:00
|
|
|
/// Use the serving methods in [AngelHttp] instead.
|
|
|
|
@deprecated
|
2017-09-22 04:48:22 +00:00
|
|
|
factory Angel.fromSecurityContext(SecurityContext context) {
|
2018-08-19 15:33:25 +00:00
|
|
|
var app = new Angel(MirrorsReflector());
|
2017-02-26 21:31:09 +00:00
|
|
|
|
2018-06-08 07:06:26 +00:00
|
|
|
app._http = new AngelHttp.custom(app, (address, int port) {
|
|
|
|
return HttpServer.bindSecure(address, port, context);
|
2018-02-07 04:10:49 +00:00
|
|
|
});
|
2017-02-26 21:31:09 +00:00
|
|
|
|
|
|
|
return app;
|
|
|
|
}
|
|
|
|
|
2018-02-07 04:10:49 +00:00
|
|
|
/// Use the serving methods in [AngelHttp] instead.
|
|
|
|
@deprecated
|
2016-11-23 19:50:17 +00:00
|
|
|
factory Angel.secure(String certificateChainPath, String serverKeyPath,
|
2018-02-07 04:10:49 +00:00
|
|
|
{String password}) {
|
2018-08-19 15:33:25 +00:00
|
|
|
var app = new Angel(MirrorsReflector());
|
2018-02-07 04:10:49 +00:00
|
|
|
return app
|
|
|
|
.._http = new AngelHttp.secure(app, certificateChainPath, serverKeyPath,
|
|
|
|
password: password);
|
Angel.secure, fallback routes, 404, app.addRoute, app.all, services are a go (just missing params, i.e. $sort?), now have service.app, app.before, app.after, angel.configure now uses futures, errors are implemented
2016-04-29 00:01:58 +00:00
|
|
|
}
|
2017-11-28 18:14:50 +00:00
|
|
|
}
|