platform/packages/framework/example/json.dart

54 lines
1.2 KiB
Dart
Raw Normal View History

2017-03-28 23:29:22 +00:00
import 'dart:async';
2018-08-21 01:27:39 +00:00
import 'dart:io';
2017-03-28 23:29:22 +00:00
import 'dart:isolate';
2021-05-14 10:34:09 +00:00
import 'package:angel3_framework/angel3_framework.dart';
import 'package:angel3_framework/http.dart';
2017-03-28 23:29:22 +00:00
2021-05-14 10:34:09 +00:00
void main() async {
2021-07-08 02:42:40 +00:00
var x = 0;
2019-05-02 22:48:31 +00:00
var c = Completer();
var exit = ReceivePort();
2021-07-08 02:42:40 +00:00
var isolates = <Isolate>[];
2017-03-28 23:29:22 +00:00
exit.listen((_) {
if (++x >= 50) {
c.complete();
}
});
2021-07-08 02:42:40 +00:00
for (var i = 1; i < Platform.numberOfProcessors; i++) {
2017-03-28 23:29:22 +00:00
var isolate = await Isolate.spawn(serverMain, null);
isolates.add(isolate);
print('Spawned isolate #${i + 1}...');
isolate.addOnExitListener(exit.sendPort);
}
2018-08-21 01:27:39 +00:00
serverMain(null);
2017-03-28 23:29:22 +00:00
print('Angel listening at http://localhost:3000');
await c.future;
}
2021-07-08 02:42:40 +00:00
void serverMain(_) async {
2019-05-02 22:48:31 +00:00
var app = Angel();
2018-12-09 15:49:59 +00:00
var http =
2019-05-02 22:48:31 +00:00
AngelHttp.custom(app, startShared, useZone: false); // Run a cluster
2017-03-28 23:29:22 +00:00
app.get('/', (req, res) {
2018-11-13 21:17:31 +00:00
return res.serialize({
2021-07-08 02:42:40 +00:00
'foo': 'bar',
'one': [2, 'three'],
'bar': {'baz': 'quux'}
});
2017-03-28 23:29:22 +00:00
});
2017-10-04 14:09:12 +00:00
app.errorHandler = (e, req, res) {
2021-04-10 11:23:57 +00:00
print(e.message);
2017-10-04 14:09:12 +00:00
print(e.stackTrace);
};
2017-03-28 23:29:22 +00:00
2021-07-08 02:42:40 +00:00
var server = await http.startServer('127.0.0.1', 3000);
2018-02-07 04:38:26 +00:00
print('Listening at http://${server.address.address}:${server.port}');
2017-03-28 23:29:22 +00:00
}