platform/performance/hello/main.dart

49 lines
1.1 KiB
Dart
Raw Normal View History

/// A basic server that prints "Hello, world!"
library performance.hello;
2017-11-18 17:42:31 +00:00
import 'dart:async';
import 'dart:io';
import 'dart:isolate';
import 'package:angel_framework/angel_framework.dart';
2017-11-28 18:14:50 +00:00
main() async {
var isolates = <Isolate>[];
2017-11-18 17:42:31 +00:00
for (int i = 0; i < Platform.numberOfProcessors; i++) {
2017-11-28 18:14:50 +00:00
isolates.add(await Isolate.spawn(start, i + 1));
2017-10-28 08:50:16 +00:00
}
2017-11-28 18:14:50 +00:00
await Future.wait(isolates.map((i) {
var rcv = new ReceivePort();
i.addOnExitListener(rcv.sendPort);
return rcv.first;
}));
//start(0);
}
void start(int id) {
2018-02-07 05:36:24 +00:00
var app = new Angel()..lazyParseBodies = true;
var http = new AngelHttp.custom(app, startShared);
2017-11-28 18:14:50 +00:00
app.get('/', (req, ResponseContext res) {
res.willCloseItself = true;
res.io
..write('Hello, world!')
..close();
return false;
});
2018-02-07 05:36:24 +00:00
2017-09-22 14:53:49 +00:00
var oldHandler = app.errorHandler;
app.errorHandler = (e, req, res) {
print('Oops: ${e.error ?? e}');
print(e.stackTrace);
return oldHandler(e, req, res);
};
2018-02-07 05:36:24 +00:00
http.startServer(InternetAddress.LOOPBACK_IP_V4, 3000).then((server) {
print(
'Instance #$id listening at http://${server.address.address}:${server.port}');
});
}