platform/example/json.dart
2018-08-20 16:21:06 -04:00

53 lines
1.2 KiB
Dart

import 'dart:async';
import 'dart:convert';
import 'dart:isolate';
import 'package:angel_container/mirrors.dart';
import 'package:angel_framework/angel_framework.dart';
main() async {
int x = 0;
var c = new Completer();
var exit = new ReceivePort();
List<Isolate> isolates = [];
exit.listen((_) {
if (++x >= 50) {
c.complete();
}
});
for (int i = 0; i < 50; i++) {
var isolate = await Isolate.spawn(serverMain, null);
isolates.add(isolate);
print('Spawned isolate #${i + 1}...');
isolate.addOnExitListener(exit.sendPort);
}
print('Angel listening at http://localhost:3000');
await c.future;
}
serverMain(_) async {
var app = new Angel(reflector: MirrorsReflector());
var http = new AngelHttp.custom(app, startShared); // Run a cluster
app.get('/', (req, res) {
res.serialize({
"foo": "bar",
"one": [2, "three"],
"bar": {"baz": "quux"}
});
});
// Performance tuning
app..serializer = json.encode;
app.errorHandler = (e, req, res) {
print(e.message ?? e.error ?? e);
print(e.stackTrace);
};
var server = await http.startServer('127.0.0.1', 3000);
print('Listening at http://${server.address.address}:${server.port}');
}