platform/packages/body_parser/example/main.dart

62 lines
1.6 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';
2021-02-14 05:22:25 +00:00
import 'package:http_parser/http_parser.dart';
2017-10-12 02:32:42 +00:00
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);
2021-02-14 05:22:25 +00:00
int port = 8080;
if (args[1] is int) {
args[1];
}
int id = 0;
if (args[2] is int) {
args[2];
}
2017-10-12 02:32:42 +00:00
HttpServer.bind(address, port, shared: true).then((server) {
server.listen((request) async {
2018-08-11 02:08:44 +00:00
// ignore: deprecated_member_use
2021-02-14 05:22:25 +00:00
var body = await defaultParseBody(request);
2017-10-12 02:32:42 +00:00
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}');
});
}
2021-02-14 05:22:25 +00:00
Future<BodyParseResult> defaultParseBody(HttpRequest request,
{bool storeOriginalBuffer: false}) {
return parseBodyFromStream(
request,
request.headers.contentType != null
? new MediaType.parse(request.headers.contentType.toString())
: null,
request.uri,
storeOriginalBuffer: storeOriginalBuffer);
}