platform/packages/body_parser/example/main.dart

42 lines
1.1 KiB
Dart
Raw Normal View History

2017-10-12 02:32:42 +00:00
import 'dart:async';
import 'dart:convert';
import 'dart:io';
import 'dart:isolate';
import 'package:body_parser/body_parser.dart';
main() async {
2018-08-11 02:08:44 +00:00
var address = '127.0.0.1';
2017-10-12 02:32:42 +00:00
var port = 3000;
var futures = <Future>[];
for (int i = 1; i < Platform.numberOfProcessors; i++) {
2018-08-11 02:08:44 +00:00
futures.add(Isolate.spawn(start, [address, port, i]));
2017-10-12 02:32:42 +00:00
}
Future.wait(futures).then((_) {
print('All instances started.');
print(
'Test with "wrk -t12 -c400 -d30s -s ./example/post.lua http://localhost:3000" or similar');
2018-08-11 02:08:44 +00:00
start([address, port, 0]);
2017-10-12 02:32:42 +00:00
});
}
void start(List args) {
2018-08-11 02:08:44 +00:00
var address = new InternetAddress(args[0] as String);
2017-10-12 02:32:42 +00:00
int port = args[1], id = args[2];
HttpServer.bind(address, port, shared: true).then((server) {
server.listen((request) async {
2018-08-11 02:08:44 +00:00
// ignore: deprecated_member_use
2017-10-12 02:32:42 +00:00
var body = await parseBody(request);
request.response
2018-08-11 02:08:44 +00:00
..headers.contentType = new ContentType('application', 'json')
..write(json.encode(body.body))
2017-10-12 02:32:42 +00:00
..close();
});
print(
'Server #$id listening at http://${server.address.address}:${server.port}');
});
}