2016-09-15 19:53:01 +00:00
|
|
|
library angel_framework.http.server;
|
|
|
|
|
|
|
|
import 'dart:async';
|
|
|
|
import 'dart:io';
|
|
|
|
import 'dart:math' show Random;
|
2016-09-17 16:12:25 +00:00
|
|
|
import 'dart:mirrors';
|
2016-09-15 19:53:01 +00:00
|
|
|
import 'package:json_god/json_god.dart' as god;
|
|
|
|
import 'angel_base.dart';
|
|
|
|
import 'angel_http_exception.dart';
|
|
|
|
import 'controller.dart';
|
|
|
|
import 'request_context.dart';
|
|
|
|
import 'response_context.dart';
|
|
|
|
import 'routable.dart';
|
|
|
|
import 'route.dart';
|
|
|
|
import 'service.dart';
|
2016-09-17 16:12:25 +00:00
|
|
|
export 'package:container/container.dart';
|
2016-02-28 13:11:17 +00:00
|
|
|
|
2016-04-18 03:27:23 +00:00
|
|
|
/// A function that binds an [Angel] server to an Internet address and port.
|
2016-02-28 13:11:17 +00:00
|
|
|
typedef Future<HttpServer> ServerGenerator(InternetAddress address, int port);
|
|
|
|
|
2016-05-02 22:28:14 +00:00
|
|
|
/// Handles an [AngelHttpException].
|
2016-09-17 16:12:25 +00:00
|
|
|
typedef Future AngelErrorHandler(
|
|
|
|
AngelHttpException err, RequestContext req, ResponseContext res);
|
2016-05-02 22:28:14 +00:00
|
|
|
|
2016-09-15 19:53:01 +00:00
|
|
|
/// A function that configures an [AngelBase] server in some way.
|
|
|
|
typedef Future AngelConfigurer(AngelBase app);
|
2016-04-18 03:27:23 +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-09-17 16:12:25 +00:00
|
|
|
var _afterProcessed = new StreamController<HttpRequest>.broadcast();
|
|
|
|
var _beforeProcessed = new StreamController<HttpRequest>.broadcast();
|
2016-09-19 06:52:21 +00:00
|
|
|
var _fatalErrorStream = new StreamController<Map>.broadcast();
|
2016-07-05 22:11:54 +00:00
|
|
|
var _onController = new StreamController<Controller>.broadcast();
|
2016-09-17 16:12:25 +00:00
|
|
|
ServerGenerator _serverGenerator =
|
|
|
|
(address, port) async => await HttpServer.bind(address, port);
|
|
|
|
|
|
|
|
/// Fired after a request is processed. Always runs.
|
|
|
|
Stream<HttpRequest> get afterProcessed => _afterProcessed.stream;
|
2016-07-04 18:06:31 +00:00
|
|
|
|
2016-07-05 22:11:54 +00:00
|
|
|
/// Fired before a request is processed. Always runs.
|
2016-07-04 18:06:31 +00:00
|
|
|
Stream<HttpRequest> get beforeProcessed => _beforeProcessed.stream;
|
2016-07-05 22:11:54 +00:00
|
|
|
|
2016-09-19 06:52:21 +00:00
|
|
|
/// Fired on fatal errors.
|
|
|
|
Stream<Map> get fatalErrorStream => _fatalErrorStream.stream;
|
|
|
|
|
2016-07-05 22:11:54 +00:00
|
|
|
/// Fired whenever a controller is added to this instance.
|
|
|
|
///
|
|
|
|
/// **NOTE**: This is a broadcast stream.
|
|
|
|
Stream<Controller> get onController => _onController.stream;
|
|
|
|
|
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
|
|
|
/// Default error handler, show HTML error page
|
2016-09-17 16:12:25 +00:00
|
|
|
AngelErrorHandler _errorHandler =
|
|
|
|
(AngelHttpException e, req, ResponseContext res) {
|
2016-05-01 02:03:31 +00:00
|
|
|
res.header(HttpHeaders.CONTENT_TYPE, ContentType.HTML.toString());
|
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.status(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>");
|
|
|
|
for (String error in e.errors) {
|
|
|
|
res.write("<li>$error</li>");
|
|
|
|
}
|
|
|
|
res.write("</ul></body></html>");
|
|
|
|
res.end();
|
|
|
|
};
|
|
|
|
|
2016-09-21 05:10:21 +00:00
|
|
|
/// The handler currently configured to run on [AngelHttpException]s.
|
|
|
|
AngelErrorHandler get errorHandler => _errorHandler;
|
|
|
|
|
2016-05-02 22:28:14 +00:00
|
|
|
/// [RequestMiddleware] to be run before all requests.
|
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
|
|
|
List before = [];
|
|
|
|
|
2016-05-02 22:28:14 +00:00
|
|
|
/// [RequestMiddleware] to be run after all requests.
|
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
|
|
|
List after = [];
|
2016-04-18 03:27:23 +00:00
|
|
|
|
2016-06-21 22:56:04 +00:00
|
|
|
/// The native HttpServer running this instancce.
|
2016-04-18 03:27:23 +00:00
|
|
|
HttpServer httpServer;
|
|
|
|
|
2016-06-21 22:56:04 +00:00
|
|
|
/// Starts the server.
|
|
|
|
///
|
|
|
|
/// Returns false on failure; otherwise, returns the HttpServer.
|
2016-09-17 16:12:25 +00:00
|
|
|
Future<HttpServer> startServer([InternetAddress address, int port]) async {
|
2016-09-18 02:59:06 +00:00
|
|
|
var server = await _serverGenerator(
|
|
|
|
address ?? InternetAddress.LOOPBACK_IP_V4, port ?? 0);
|
2016-04-18 03:27:23 +00:00
|
|
|
this.httpServer = server;
|
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-07-04 18:06:31 +00:00
|
|
|
server.listen(handleRequest);
|
|
|
|
|
|
|
|
return server;
|
|
|
|
}
|
|
|
|
|
2016-09-17 16:12:25 +00:00
|
|
|
/// Loads some base dependencies into the service container.
|
|
|
|
void bootstrapContainer() {
|
|
|
|
container.singleton(this, as: AngelBase);
|
|
|
|
container.singleton(this);
|
|
|
|
|
2016-09-18 02:59:06 +00:00
|
|
|
if (runtimeType != Angel) container.singleton(this, as: Angel);
|
2016-09-17 16:12:25 +00:00
|
|
|
}
|
|
|
|
|
2016-07-04 18:06:31 +00:00
|
|
|
Future handleRequest(HttpRequest request) async {
|
|
|
|
_beforeProcessed.add(request);
|
2016-09-17 16:12:25 +00:00
|
|
|
|
|
|
|
String requestedUrl = request.uri
|
|
|
|
.toString()
|
|
|
|
.replaceAll("?" + request.uri.query, "")
|
|
|
|
.replaceAll(new RegExp(r'\/+$'), '');
|
|
|
|
|
|
|
|
if (requestedUrl.isEmpty) requestedUrl = '/';
|
|
|
|
|
2016-07-04 18:06:31 +00:00
|
|
|
RequestContext req = await RequestContext.from(request, {}, this, null);
|
|
|
|
ResponseContext res = await ResponseContext.from(request.response, this);
|
|
|
|
|
|
|
|
bool canContinue = true;
|
|
|
|
|
2016-09-17 16:12:25 +00:00
|
|
|
executeHandler(handler, req) async {
|
2016-07-04 18:06:31 +00:00
|
|
|
if (canContinue) {
|
2016-09-17 16:12:25 +00:00
|
|
|
try {
|
|
|
|
canContinue = await _applyHandler(handler, req, res);
|
|
|
|
} catch (e, stackTrace) {
|
2016-07-04 18:06:31 +00:00
|
|
|
if (e is AngelHttpException) {
|
|
|
|
// Special handling for AngelHttpExceptions :)
|
|
|
|
try {
|
|
|
|
res.status(e.statusCode);
|
|
|
|
String accept = request.headers.value(HttpHeaders.ACCEPT);
|
|
|
|
if (accept == "*/*" ||
|
2016-09-17 16:12:25 +00:00
|
|
|
accept.contains(ContentType.JSON.mimeType) ||
|
2016-07-04 18:06:31 +00:00
|
|
|
accept.contains("application/javascript")) {
|
|
|
|
res.json(e.toMap());
|
|
|
|
} else {
|
|
|
|
await _errorHandler(e, req, res);
|
|
|
|
}
|
|
|
|
_finalizeResponse(request, res);
|
|
|
|
} catch (_) {}
|
|
|
|
}
|
|
|
|
_onError(e, stackTrace);
|
|
|
|
canContinue = false;
|
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
|
|
|
return false;
|
2016-09-17 16:12:25 +00:00
|
|
|
}
|
2016-07-04 18:06:31 +00:00
|
|
|
} else
|
|
|
|
return false;
|
2016-09-17 16:12:25 +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
|
|
|
|
2016-07-04 18:06:31 +00:00
|
|
|
for (var handler in before) {
|
2016-09-17 16:12:25 +00:00
|
|
|
await executeHandler(handler, req);
|
2016-07-04 18:06:31 +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
|
|
|
|
2016-07-04 18:06:31 +00:00
|
|
|
for (Route route in routes) {
|
|
|
|
if (!canContinue) break;
|
2016-09-17 16:12:25 +00:00
|
|
|
|
|
|
|
if (route.matcher.hasMatch(requestedUrl) &&
|
2016-07-04 18:06:31 +00:00
|
|
|
(request.method == route.method || route.method == '*')) {
|
2016-09-17 16:12:25 +00:00
|
|
|
req.params = route.parseParameters(requestedUrl);
|
2016-07-04 18:06:31 +00:00
|
|
|
req.route = route;
|
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-07-04 18:06:31 +00:00
|
|
|
for (var handler in route.handlers) {
|
2016-09-17 16:12:25 +00:00
|
|
|
await executeHandler(handler, req);
|
2016-04-18 03:27:23 +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
|
|
|
}
|
2016-07-04 18:06:31 +00:00
|
|
|
}
|
2016-04-18 03:27:23 +00:00
|
|
|
|
2016-07-04 18:06:31 +00:00
|
|
|
for (var handler in after) {
|
2016-09-17 16:12:25 +00:00
|
|
|
await executeHandler(handler, req);
|
2016-07-04 18:06:31 +00:00
|
|
|
}
|
2016-09-17 16:12:25 +00:00
|
|
|
|
2016-07-04 18:06:31 +00:00
|
|
|
_finalizeResponse(request, res);
|
2016-04-18 03:27:23 +00:00
|
|
|
}
|
|
|
|
|
2016-09-17 16:12:25 +00:00
|
|
|
Future<bool> _applyHandler(
|
|
|
|
handler, RequestContext req, ResponseContext res) async {
|
2016-05-02 22:28:14 +00:00
|
|
|
if (handler is RequestMiddleware) {
|
2016-04-21 20:37:02 +00:00
|
|
|
var result = await handler(req, res);
|
2016-09-17 16:12:25 +00:00
|
|
|
|
2016-04-21 20:37:02 +00:00
|
|
|
if (result is bool)
|
|
|
|
return result == true;
|
|
|
|
else if (result != null) {
|
|
|
|
res.json(result);
|
|
|
|
return false;
|
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
|
|
|
} else
|
|
|
|
return res.isOpen;
|
2016-04-18 03:27:23 +00:00
|
|
|
}
|
|
|
|
|
2016-04-21 20:37:02 +00:00
|
|
|
if (handler is RequestHandler) {
|
2016-04-18 03:27:23 +00:00
|
|
|
await handler(req, res);
|
|
|
|
return res.isOpen;
|
2016-09-17 16:12:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (handler is RawRequestHandler) {
|
2016-04-18 03:27:23 +00:00
|
|
|
var result = await handler(req.underlyingRequest);
|
2016-04-21 20:37:02 +00:00
|
|
|
if (result is bool)
|
|
|
|
return result == true;
|
|
|
|
else if (result != null) {
|
|
|
|
res.json(result);
|
|
|
|
return false;
|
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
|
|
|
} else
|
|
|
|
return true;
|
2016-09-17 16:12:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (handler is Future) {
|
|
|
|
var result = await handler;
|
|
|
|
if (result is bool)
|
|
|
|
return result == true;
|
|
|
|
else if (result != null) {
|
|
|
|
res.json(result);
|
|
|
|
return false;
|
|
|
|
} else
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (handler is Function) {
|
|
|
|
var result = await runContained(handler, req, res);
|
2016-04-21 20:37:02 +00:00
|
|
|
if (result is bool)
|
|
|
|
return result == true;
|
|
|
|
else if (result != null) {
|
|
|
|
res.json(result);
|
|
|
|
return false;
|
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
|
|
|
} else
|
|
|
|
return true;
|
2016-09-17 16:12:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (requestMiddleware.containsKey(handler)) {
|
2016-05-02 22:28:14 +00:00
|
|
|
return await _applyHandler(requestMiddleware[handler], req, res);
|
2016-04-18 03:27:23 +00:00
|
|
|
}
|
2016-09-17 16:12:25 +00:00
|
|
|
|
|
|
|
res.willCloseItself = true;
|
|
|
|
res.underlyingResponse.write(god.serialize(handler));
|
|
|
|
await res.underlyingResponse.close();
|
|
|
|
return false;
|
2016-04-18 03:27:23 +00:00
|
|
|
}
|
|
|
|
|
2016-04-22 01:42:39 +00:00
|
|
|
_finalizeResponse(HttpRequest request, ResponseContext res) async {
|
2016-05-01 01:42:52 +00:00
|
|
|
try {
|
|
|
|
if (!res.willCloseItself) {
|
|
|
|
res.responseData.forEach((blob) => request.response.add(blob));
|
|
|
|
await request.response.close();
|
2016-07-04 18:06:31 +00:00
|
|
|
_afterProcessed.add(request);
|
2016-05-01 01:42:52 +00:00
|
|
|
}
|
|
|
|
} catch (e) {
|
2016-09-17 16:12:25 +00:00
|
|
|
failSilently(request, res);
|
2016-04-22 01:42:39 +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
|
|
|
String _randomString(int length) {
|
|
|
|
var rand = new Random();
|
|
|
|
var codeUnits = new List.generate(length, (index) {
|
|
|
|
return rand.nextInt(33) + 89;
|
|
|
|
});
|
|
|
|
|
|
|
|
return new String.fromCharCodes(codeUnits);
|
|
|
|
}
|
|
|
|
|
2016-09-17 16:12:25 +00:00
|
|
|
// Run a function after injecting from service container
|
2016-09-18 02:59:06 +00:00
|
|
|
Future runContained(Function handler, RequestContext req, ResponseContext res,
|
|
|
|
{Map<String, dynamic> namedParameters,
|
|
|
|
Map<Type, dynamic> injecting}) async {
|
2016-09-17 16:12:25 +00:00
|
|
|
ClosureMirror closureMirror = reflect(handler);
|
|
|
|
List args = [];
|
|
|
|
|
|
|
|
for (ParameterMirror parameter in closureMirror.function.parameters) {
|
|
|
|
if (parameter.type.reflectedType == RequestContext)
|
|
|
|
args.add(req);
|
|
|
|
else if (parameter.type.reflectedType == ResponseContext)
|
|
|
|
args.add(res);
|
|
|
|
else {
|
|
|
|
// First, search to see if we can map this to a type
|
|
|
|
if (parameter.type.reflectedType != dynamic) {
|
2016-09-18 02:59:06 +00:00
|
|
|
args.add(container.make(parameter.type.reflectedType,
|
|
|
|
namedParameters: namedParameters, injecting: injecting));
|
2016-09-17 16:12:25 +00:00
|
|
|
} else {
|
|
|
|
String name = MirrorSystem.getName(parameter.simpleName);
|
|
|
|
|
2016-09-18 02:59:06 +00:00
|
|
|
if (req.params.containsKey(name))
|
|
|
|
args.add(req.params[name]);
|
|
|
|
else if (name == "req")
|
2016-09-17 16:12:25 +00:00
|
|
|
args.add(req);
|
|
|
|
else if (name == "res")
|
|
|
|
args.add(res);
|
|
|
|
else {
|
2016-09-18 02:59:06 +00:00
|
|
|
throw new Exception(
|
|
|
|
"Cannot resolve parameter '$name' within handler.");
|
2016-09-17 16:12:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return await closureMirror.apply(args).reflectee;
|
|
|
|
}
|
|
|
|
|
2016-04-18 03:27:23 +00:00
|
|
|
/// Applies an [AngelConfigurer] to this instance.
|
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
|
|
|
Future configure(AngelConfigurer configurer) async {
|
|
|
|
await configurer(this);
|
2016-07-05 22:11:54 +00:00
|
|
|
|
2016-09-17 16:12:25 +00:00
|
|
|
if (configurer is Controller) _onController.add(configurer);
|
2016-02-28 13:11:17 +00:00
|
|
|
}
|
|
|
|
|
2016-09-17 16:12:25 +00:00
|
|
|
/// Fallback when an error is thrown while handling a request.
|
|
|
|
void failSilently(HttpRequest request, ResponseContext res) {}
|
|
|
|
|
2016-02-28 13:11:17 +00:00
|
|
|
/// Starts the server.
|
|
|
|
void listen({InternetAddress address, int port: 3000}) {
|
|
|
|
runZoned(() async {
|
2016-04-18 03:27:23 +00:00
|
|
|
await startServer(address, port);
|
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
|
|
|
}, onError: _onError);
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
2016-05-02 22:28:14 +00:00
|
|
|
use(Pattern path, Routable routable,
|
2016-06-21 04:19:43 +00:00
|
|
|
{bool hooked: true, String middlewareNamespace: null}) {
|
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-06-24 19:19:02 +00:00
|
|
|
return super.use(path, routable,
|
2016-06-21 20:34:31 +00:00
|
|
|
hooked: hooked, middlewareNamespace: middlewareNamespace);
|
2016-02-28 13:11:17 +00:00
|
|
|
}
|
|
|
|
|
2016-06-21 22:56:04 +00:00
|
|
|
/// Registers a callback to run upon errors.
|
|
|
|
onError(AngelErrorHandler handler) {
|
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
|
|
|
_errorHandler = handler;
|
|
|
|
}
|
2016-04-22 01:42:39 +00:00
|
|
|
|
2016-02-28 13:11:17 +00:00
|
|
|
/// Handles a server error.
|
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
|
|
|
_onError(e, [StackTrace stackTrace]) {
|
2016-09-19 06:52:21 +00:00
|
|
|
_fatalErrorStream.add({
|
|
|
|
"error": e,
|
|
|
|
"stack": stackTrace
|
|
|
|
});
|
2016-04-22 01:42:39 +00:00
|
|
|
}
|
2016-04-18 03:27:23 +00:00
|
|
|
|
2016-09-17 16:12:25 +00:00
|
|
|
Angel() : super() {
|
|
|
|
bootstrapContainer();
|
|
|
|
}
|
2016-02-28 13:11:17 +00:00
|
|
|
|
2016-04-18 03:27:23 +00:00
|
|
|
/// Creates an HTTPS server.
|
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
|
|
|
/// Provide paths to a certificate chain and server key (both .pem).
|
|
|
|
/// If no password is provided, a random one will be generated upon running
|
|
|
|
/// the server.
|
2016-06-21 20:34:31 +00:00
|
|
|
Angel.secure(String certificateChainPath, String serverKeyPath,
|
|
|
|
{String password})
|
|
|
|
: super() {
|
2016-09-17 16:12:25 +00:00
|
|
|
bootstrapContainer();
|
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
|
|
|
_serverGenerator = (InternetAddress address, int port) async {
|
|
|
|
var certificateChain =
|
2016-09-17 16:12:25 +00:00
|
|
|
Platform.script.resolve('server_chain.pem').toFilePath();
|
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
|
|
|
var serverKey = Platform.script.resolve('server_key.pem').toFilePath();
|
|
|
|
var serverContext = new SecurityContext();
|
|
|
|
serverContext.useCertificateChain(certificateChain);
|
|
|
|
serverContext.usePrivateKey(serverKey,
|
|
|
|
password: password ?? _randomString(8));
|
|
|
|
|
|
|
|
return await HttpServer.bindSecure(address, port, serverContext);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|