Skip to content
This repository was archived by the owner on Mar 18, 2021. It is now read-only.

Commit c9205d4

Browse files
Made ApplicationPipeline a request handler, removed its initialHandler function as handleRequest is better anyhow
1 parent f888716 commit c9205d4

6 files changed

+59
-57
lines changed

lib/application.dart

Lines changed: 31 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -51,33 +51,27 @@ class ApplicationInstanceConfiguration {
5151
/// A copy constructor
5252
ApplicationInstanceConfiguration.fromConfiguration(
5353
ApplicationInstanceConfiguration config)
54-
: this.address = config.address,
55-
this.port = config.port,
56-
this.isIpv6Only = config.isIpv6Only,
57-
this.isUsingClientCertificate = config.isUsingClientCertificate,
58-
this.serverCertificateName = config.serverCertificateName,
59-
this.shared = config.shared;
54+
: this.address = config.address,
55+
this.port = config.port,
56+
this.isIpv6Only = config.isIpv6Only,
57+
this.isUsingClientCertificate = config.isUsingClientCertificate,
58+
this.serverCertificateName = config.serverCertificateName,
59+
this.shared = config.shared;
6060
}
6161

6262
/// A abstract class that concrete subclasses will implement to provide request handling behavior.
6363
///
6464
/// [Application]s set up HTTP(s) listeners, but do not do anything with them. The behavior of how an application
65-
/// responds to requests is defined by its [ApplicationPipeline].
66-
abstract class ApplicationPipeline {
65+
/// responds to requests is defined by its [ApplicationPipeline]. Instances of this class must implement the
66+
/// [handleRequest] method from [RequestHandler] - this is the entry point of all requests into this pipeline.
67+
abstract class ApplicationPipeline implements RequestHandler {
6768

6869
/// Passed in options for this pipeline from its owning [Application].
6970
///
7071
/// These values give an opportunity for a pipeline to have a customization point within attachTo., like running
7172
/// the owning [Application] in 'Development' or 'Production' mode. This property will always be set prior to invoking attachTo, but may be null
7273
/// if the user did not set any configuration values.
7374
Map<String, dynamic> options;
74-
75-
/// Allows an [ApplicationPipeline] to handle HTTP(s) requests from its [Application].
76-
///
77-
/// Implementors of [ApplicationPipeline] must override this method to respond to an [Application]'s requests.
78-
/// The return value is the first [RequestHandler] that will be the initial [ResourceRequest] handler.
79-
/// The [initialHandler] will likely have handlers attached to it.
80-
RequestHandler initialHandler();
8175
}
8276

8377
/// A container for web server applications.
@@ -94,7 +88,7 @@ class Application {
9488
///
9589
/// This must be configured prior to [start]ing the [Application].
9690
ApplicationInstanceConfiguration configuration =
97-
new ApplicationInstanceConfiguration();
91+
new ApplicationInstanceConfiguration();
9892

9993
/// The type of [ApplicationPipeline] that configures how requests are handled.
10094
///
@@ -119,7 +113,7 @@ class Application {
119113

120114
for (int i = 0; i < numberOfInstances; i++) {
121115
var config =
122-
new ApplicationInstanceConfiguration.fromConfiguration(configuration);
116+
new ApplicationInstanceConfiguration.fromConfiguration(configuration);
123117

124118
var serverRecord = await _spawn(config, i + 1);
125119
servers.add(serverRecord);
@@ -140,7 +134,7 @@ class Application {
140134
var initialMessage = new _InitialServerMessage(
141135
pipelineTypeName, pipelineLibraryURI, config, identifier, receivePort.sendPort);
142136
var isolate =
143-
await Isolate.spawn(_Server.entry, initialMessage, paused: true);
137+
await Isolate.spawn(_Server.entry, initialMessage, paused: true);
144138
isolate.addErrorListener(receivePort.sendPort);
145139

146140
return new _ServerRecord(isolate, receivePort, identifier);
@@ -153,7 +147,6 @@ class _Server {
153147
HttpServer server;
154148
ApplicationPipeline pipeline;
155149
int identifier;
156-
RequestHandler initialHandler;
157150

158151
_Server(this.pipeline, this.configuration, this.identifier, this.parentMessagePort);
159152

@@ -164,42 +157,41 @@ class _Server {
164157
server.serverHeader = "monadart/${this.identifier}";
165158

166159
pipeline.options = configuration.pipelineOptions;
167-
initialHandler = pipeline.initialHandler();
168160
server.map((httpReq) => new ResourceRequest(httpReq))
169-
.listen(initialHandler.handleRequest);
161+
.listen(pipeline.handleRequest);
170162
};
171163

172164
if (configuration.serverCertificateName != null) {
173165
HttpServer
174-
.bindSecure(configuration.address, configuration.port,
175-
certificateName: configuration.serverCertificateName,
176-
v6Only: configuration.isIpv6Only,
177-
shared: configuration.shared)
178-
.then(onBind);
166+
.bindSecure(configuration.address, configuration.port,
167+
certificateName: configuration.serverCertificateName,
168+
v6Only: configuration.isIpv6Only,
169+
shared: configuration.shared)
170+
.then(onBind);
179171
} else if (configuration.isUsingClientCertificate) {
180172
HttpServer
181-
.bindSecure(configuration.address, configuration.port,
182-
requestClientCertificate: true,
183-
v6Only: configuration.isIpv6Only,
184-
shared: configuration.shared)
185-
.then(onBind);
173+
.bindSecure(configuration.address, configuration.port,
174+
requestClientCertificate: true,
175+
v6Only: configuration.isIpv6Only,
176+
shared: configuration.shared)
177+
.then(onBind);
186178
} else {
187179
HttpServer
188-
.bind(configuration.address, configuration.port,
189-
v6Only: configuration.isIpv6Only, shared: configuration.shared)
190-
.then(onBind);
180+
.bind(configuration.address, configuration.port,
181+
v6Only: configuration.isIpv6Only, shared: configuration.shared)
182+
.then(onBind);
191183
}
192184
}
193185

194186
static void entry(_InitialServerMessage params) {
195187
var pipelineSourceLibraryMirror =
196-
currentMirrorSystem().libraries[params.pipelineLibraryURI];
188+
currentMirrorSystem().libraries[params.pipelineLibraryURI];
197189
var pipelineTypeMirror = pipelineSourceLibraryMirror.declarations[
198-
new Symbol(params.pipelineTypeName)] as ClassMirror;
190+
new Symbol(params.pipelineTypeName)] as ClassMirror;
199191

200192
var app = pipelineTypeMirror.newInstance(new Symbol(""), []).reflectee;
201193
var server =
202-
new _Server(app, params.configuration, params.identifier, params.parentMessagePort);
194+
new _Server(app, params.configuration, params.identifier, params.parentMessagePort);
203195

204196
server.start();
205197
}
@@ -225,5 +217,5 @@ class _InitialServerMessage {
225217
int identifier;
226218

227219
_InitialServerMessage(this.pipelineTypeName, this.pipelineLibraryURI,
228-
this.configuration, this.identifier, this.parentMessagePort);
229-
}
220+
this.configuration, this.identifier, this.parentMessagePort);
221+
}

lib/http_controller.dart

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class HttpMethod {
4141

4242
HttpMethod._fromMethod(HttpMethod m, List<String> parameters)
4343
: this.method = m.method,
44-
this._parameters = parameters;
44+
this._parameters = parameters;
4545

4646
/// Returns whether or not this [HttpMethod] matches a [ResourceRequest].
4747
bool matchesRequest(ResourceRequest req) {
@@ -82,9 +82,11 @@ abstract class HttpController implements RequestHandler {
8282
/// but once execution enters a handler method (one decorated with [HttpMethod]), this exception handler
8383
/// is in place.
8484
Function get exceptionHandler => _exceptionHandler;
85+
8586
void set exceptionHandler(Response handler(ResourceRequest resourceRequest, dynamic exceptionOrError, StackTrace stacktrace)) {
8687
_exceptionHandler = handler;
8788
}
89+
8890
Function _exceptionHandler = _defaultExceptionHandler;
8991

9092
/// The request being processed by this [HttpController].
@@ -177,29 +179,29 @@ abstract class HttpController implements RequestHandler {
177179

178180
dynamic _convertParameterWithMirror(String parameterValue, ParameterMirror parameterMirror) {
179181
var typeMirror = parameterMirror.type;
180-
if(typeMirror.isSubtypeOf(reflectType(String))) {
182+
if (typeMirror.isSubtypeOf(reflectType(String))) {
181183
return parameterValue;
182184
}
183185

184-
if(typeMirror is ClassMirror) {
186+
if (typeMirror is ClassMirror) {
185187
var cm = (typeMirror as ClassMirror);
186188
var parseDecl = cm.declarations[new Symbol("parse")];
187-
if(parseDecl != null) {
189+
if (parseDecl != null) {
188190
try {
189191
var reflValue = cm.invoke(parseDecl.simpleName, [parameterValue]);
190192
return reflValue.reflectee;
191193
} catch (e) {
192194
throw new _InternalControllerException("Invalid value for parameter type",
193-
HttpStatus.BAD_REQUEST,
194-
responseMessage: "URI parameter is wrong type");
195+
HttpStatus.BAD_REQUEST,
196+
responseMessage: "URI parameter is wrong type");
195197
}
196198
}
197199
}
198200

199201
// If we get here, then it wasn't a string and couldn't be parsed, and we should throw?
200202
throw new _InternalControllerException("Invalid path parameter type, types must be String or implement parse",
201-
HttpStatus.INTERNAL_SERVER_ERROR,
202-
responseMessage: "URI parameter is wrong type");
203+
HttpStatus.INTERNAL_SERVER_ERROR,
204+
responseMessage: "URI parameter is wrong type");
203205
return null;
204206
}
205207

@@ -218,18 +220,18 @@ abstract class HttpController implements RequestHandler {
218220

219221
Map<Symbol, dynamic> _queryParametersForRequest(ResourceRequest req, Symbol handlerMethodSymbol) {
220222
var queryParams = req.request.uri.queryParameters;
221-
if(queryParams.length == 0) {
223+
if (queryParams.length == 0) {
222224
return null;
223225
}
224226

225227
var optionalParams = (reflect(this).type.declarations[handlerMethodSymbol] as MethodMirror)
226-
.parameters.where((methodParameter) => methodParameter.isOptional).toList();
228+
.parameters.where((methodParameter) => methodParameter.isOptional).toList();
227229

228230
var retMap = {};
229231
queryParams.forEach((k, v) {
230232
var keySymbol = new Symbol(k);
231233
var matchingParameter = optionalParams.firstWhere((p) => p.simpleName == keySymbol, orElse: () => null);
232-
if(matchingParameter != null) {
234+
if (matchingParameter != null) {
233235
retMap[keySymbol] = _convertParameterWithMirror(v, matchingParameter);
234236
}
235237
});

test/application_tests.dart

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,17 @@ main() {
3030
}
3131

3232
class TPipeline extends ApplicationPipeline {
33+
Router router;
3334

34-
RequestHandler initialHandler() {
35-
var router = new Router();
35+
TPipeline() {
36+
router = new Router();
3637

3738
router.addRouteHandler("/t", new RequestHandlerGenerator<TController>());
3839
router.addRouteHandler("/r", new RequestHandlerGenerator<RController>());
40+
}
3941

40-
return router;
42+
void handleRequest(ResourceRequest req) {
43+
router.handleRequest(req);
4144
}
4245
}
4346

test/controller_tests.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,7 @@ class IntController extends HttpController {
231231
Future<Response> getOne(int id) async {
232232
return new Response.ok("${id * 2}");
233233
}
234+
234235
@httpGet
235236
Future<Response> getAll({int opt: null}) async {
236237
return new Response.ok("${opt}");
@@ -242,6 +243,7 @@ class DateTimeController extends HttpController {
242243
Future<Response> getOne(DateTime time) async {
243244
return new Response.ok("${time.add(new Duration(seconds: 5))}");
244245
}
246+
245247
@httpGet
246248
Future<Response> getAll({DateTime opt: null}) async {
247249
return new Response.ok("${opt}");

test/isolate_application_tests.dart

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,19 @@ main() {
5252

5353
});
5454
}
55+
5556
class TPipeline extends ApplicationPipeline {
57+
Router router;
5658

57-
RequestHandler initialHandler() {
58-
var router = new Router();
59+
TPipeline() {
60+
router = new Router();
5961

6062
router.addRouteHandler("/t", new RequestHandlerGenerator<TController>());
6163
router.addRouteHandler("/r", new RequestHandlerGenerator<RController>());
64+
}
6265

63-
return router;
66+
void handleRequest(ResourceRequest req) {
67+
router.handleRequest(req);
6468
}
6569
}
6670

test/pattern_tests.dart

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
@TestOn("vm")
2-
32
import "package:test/test.dart";
43
import "dart:core";
54
import '../lib/monadart.dart';

0 commit comments

Comments
 (0)