@@ -2,7 +2,6 @@ part of monadart;
2
2
3
3
/// A set of values to configure an instance of a web server application.
4
4
class ApplicationInstanceConfiguration {
5
-
6
5
/// The address to listen for HTTP requests on.
7
6
///
8
7
/// By default, this address will default to 'any' address. If [isIpv6Only] is true,
@@ -37,7 +36,6 @@ class ApplicationInstanceConfiguration {
37
36
/// that are attached to this application.
38
37
Map <dynamic , dynamic > pipelineOptions;
39
38
40
-
41
39
/// Whether or not the server configuration defined by this instance can be shared across isolates.
42
40
///
43
41
/// Defaults to false. When false, only one isolate may listen for requests on the [address] and [port]
@@ -51,13 +49,13 @@ class ApplicationInstanceConfiguration {
51
49
/// A copy constructor
52
50
ApplicationInstanceConfiguration .fromConfiguration (
53
51
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,
60
- this .pipelineOptions = config.pipelineOptions;
52
+ : this .address = config.address,
53
+ this .port = config.port,
54
+ this .isIpv6Only = config.isIpv6Only,
55
+ this .isUsingClientCertificate = config.isUsingClientCertificate,
56
+ this .serverCertificateName = config.serverCertificateName,
57
+ this .shared = config.shared,
58
+ this .pipelineOptions = config.pipelineOptions;
61
59
}
62
60
63
61
/// A abstract class that concrete subclasses will implement to provide request handling behavior.
@@ -66,34 +64,31 @@ class ApplicationInstanceConfiguration {
66
64
/// responds to requests is defined by its [ApplicationPipeline] . Instances of this class must implement the
67
65
/// [handleRequest] method from [RequestHandler] - this is the entry point of all requests into this pipeline.
68
66
abstract class ApplicationPipeline implements RequestHandler {
69
-
70
67
/// Passed in options for this pipeline from its owning [Application] .
71
68
///
72
69
/// These values give an opportunity for a pipeline to have a customization point within attachTo., like running
73
70
/// the owning [Application] in 'Development' or 'Production' mode. This property will always be set prior to invoking attachTo, but may be null
74
71
/// if the user did not set any configuration values.
75
72
Map <String , dynamic > options;
76
73
77
- void onPipelineOpen ();
74
+ void pipelineWillOpen ();
75
+ void pipelineDidOpen ();
78
76
}
79
77
80
-
81
-
82
78
/// A container for web server applications.
83
79
///
84
80
/// Applications are responsible for managing starting and stopping of HTTP server instances across multiple isolates.
85
81
/// Behavior specific to an application is implemented by setting the [Application] 's [configuration] and providing
86
82
/// a [pipelineType] as a [ApplicationPipeline] subclass.
87
83
class Application {
88
-
89
84
/// A list of items identifying the Isolates running a HTTP(s) listener and response handlers.
90
85
List <_ServerRecord > servers = [];
91
86
92
87
/// The configuration for the HTTP(s) server this application is running.
93
88
///
94
89
/// This must be configured prior to [start] ing the [Application] .
95
90
ApplicationInstanceConfiguration configuration =
96
- new ApplicationInstanceConfiguration ();
91
+ new ApplicationInstanceConfiguration ();
97
92
98
93
/// The type of [ApplicationPipeline] that configures how requests are handled.
99
94
///
@@ -118,7 +113,7 @@ class Application {
118
113
119
114
for (int i = 0 ; i < numberOfInstances; i++ ) {
120
115
var config =
121
- new ApplicationInstanceConfiguration .fromConfiguration (configuration);
116
+ new ApplicationInstanceConfiguration .fromConfiguration (configuration);
122
117
123
118
var serverRecord = await _spawn (config, i + 1 );
124
119
servers.add (serverRecord);
@@ -129,17 +124,18 @@ class Application {
129
124
});
130
125
}
131
126
132
- Future <_ServerRecord > _spawn (ApplicationInstanceConfiguration config, int identifier) async {
127
+ Future <_ServerRecord > _spawn (
128
+ ApplicationInstanceConfiguration config, int identifier) async {
133
129
var receivePort = new ReceivePort ();
134
130
135
131
var pipelineTypeMirror = reflectType (pipelineType);
136
132
var pipelineLibraryURI = (pipelineTypeMirror.owner as LibraryMirror ).uri;
137
133
var pipelineTypeName = MirrorSystem .getName (pipelineTypeMirror.simpleName);
138
134
139
- var initialMessage = new _InitialServerMessage (
140
- pipelineTypeName, pipelineLibraryURI, config, identifier, receivePort.sendPort);
135
+ var initialMessage = new _InitialServerMessage (pipelineTypeName,
136
+ pipelineLibraryURI, config, identifier, receivePort.sendPort);
141
137
var isolate =
142
- await Isolate .spawn (_Server .entry, initialMessage, paused: true );
138
+ await Isolate .spawn (_Server .entry, initialMessage, paused: true );
143
139
isolate.addErrorListener (receivePort.sendPort);
144
140
145
141
return new _ServerRecord (isolate, receivePort, identifier);
@@ -153,52 +149,56 @@ class _Server {
153
149
ApplicationPipeline pipeline;
154
150
int identifier;
155
151
156
- _Server (this .pipeline, this .configuration, this .identifier, this .parentMessagePort);
152
+ _Server (this .pipeline, this .configuration, this .identifier,
153
+ this .parentMessagePort);
157
154
158
155
Future start () async {
156
+ pipeline.options = configuration.pipelineOptions;
157
+ pipeline.pipelineWillOpen ();
158
+
159
159
var onBind = (s) {
160
160
server = s;
161
161
162
162
server.serverHeader = "monadart/${this .identifier }" ;
163
163
164
- pipeline.options = configuration.pipelineOptions;
165
- server .map ((httpReq) => new ResourceRequest (httpReq))
166
- .listen (pipeline.handleRequest);
164
+ server
165
+ .map ((httpReq) => new ResourceRequest (httpReq))
166
+ .listen (pipeline.handleRequest);
167
167
168
- pipeline.onPipelineOpen ();
168
+ pipeline.pipelineDidOpen ();
169
169
};
170
170
171
171
if (configuration.serverCertificateName != null ) {
172
172
HttpServer
173
- .bindSecure (configuration.address, configuration.port,
174
- certificateName: configuration.serverCertificateName,
175
- v6Only: configuration.isIpv6Only,
176
- shared: configuration.shared)
177
- .then (onBind);
173
+ .bindSecure (configuration.address, configuration.port,
174
+ certificateName: configuration.serverCertificateName,
175
+ v6Only: configuration.isIpv6Only,
176
+ shared: configuration.shared)
177
+ .then (onBind);
178
178
} else if (configuration.isUsingClientCertificate) {
179
179
HttpServer
180
- .bindSecure (configuration.address, configuration.port,
181
- requestClientCertificate: true ,
182
- v6Only: configuration.isIpv6Only,
183
- shared: configuration.shared)
184
- .then (onBind);
180
+ .bindSecure (configuration.address, configuration.port,
181
+ requestClientCertificate: true ,
182
+ v6Only: configuration.isIpv6Only,
183
+ shared: configuration.shared)
184
+ .then (onBind);
185
185
} else {
186
186
HttpServer
187
- .bind (configuration.address, configuration.port,
188
- v6Only: configuration.isIpv6Only, shared: configuration.shared)
189
- .then (onBind);
187
+ .bind (configuration.address, configuration.port,
188
+ v6Only: configuration.isIpv6Only, shared: configuration.shared)
189
+ .then (onBind);
190
190
}
191
191
}
192
192
193
193
static void entry (_InitialServerMessage params) {
194
194
var pipelineSourceLibraryMirror =
195
- currentMirrorSystem ().libraries[params.pipelineLibraryURI];
195
+ currentMirrorSystem ().libraries[params.pipelineLibraryURI];
196
196
var pipelineTypeMirror = pipelineSourceLibraryMirror.declarations[
197
- new Symbol (params.pipelineTypeName)] as ClassMirror ;
197
+ new Symbol (params.pipelineTypeName)] as ClassMirror ;
198
198
199
199
var app = pipelineTypeMirror.newInstance (new Symbol ("" ), []).reflectee;
200
- var server =
201
- new _Server ( app, params.configuration, params.identifier, params.parentMessagePort);
200
+ var server = new _Server (
201
+ app, params.configuration, params.identifier, params.parentMessagePort);
202
202
203
203
server.start ();
204
204
}
@@ -224,5 +224,5 @@ class _InitialServerMessage {
224
224
int identifier;
225
225
226
226
_InitialServerMessage (this .pipelineTypeName, this .pipelineLibraryURI,
227
- this .configuration, this .identifier, this .parentMessagePort);
228
- }
227
+ this .configuration, this .identifier, this .parentMessagePort);
228
+ }
0 commit comments