@@ -2,24 +2,22 @@ import 'dart:convert';
22import 'dart:io' ;
33import 'dart:isolate' ;
44
5- final _encoder = JsonUtf8Encoder ();
6-
7- void main (List <String > _) async {
8- /// Create an [Isolate] containinig an [HttpServer]
5+ void main (List <String > args) async {
6+ /// Create an [Isolate] containing an [HttpServer]
97 /// for each processor after the first
108 for (var i = 1 ; i < Platform .numberOfProcessors; i++ ) {
11- await Isolate .spawn (_startServer, _ );
9+ await Isolate .spawn (_startServer, args );
1210 }
1311
1412 /// Create a [HttpServer] for the first processor
15- await _startServer (_ );
13+ await _startServer (args );
1614}
1715
1816/// Creates and setup a [HttpServer]
1917Future <void > _startServer (List <String > _) async {
2018 /// Binds the [HttpServer] on `0.0.0.0:8080` .
2119 final server = await HttpServer .bind (
22- InternetAddress ( '0.0.0.0' , type : InternetAddressType . IPv4 ) ,
20+ InternetAddress .anyIPv4 ,
2321 8080 ,
2422 shared: true ,
2523 );
@@ -51,39 +49,35 @@ void _sendResponse(
5149 int statusCode, {
5250 ContentType ? type,
5351 List <int > bytes = const [],
54- }) =>
55- request.response
56- ..statusCode = statusCode
57- ..headers.contentType = type
58- ..headers.date = DateTime .now ()
59- ..contentLength = bytes.length
60- ..add (bytes)
61- ..close ();
52+ }) => request.response
53+ ..statusCode = statusCode
54+ ..headers.contentType = type
55+ ..headers.date = DateTime .now ()
56+ ..contentLength = bytes.length
57+ ..add (bytes)
58+ ..close ();
6259
6360/// Completes the given [request] by writing the [response] as JSON.
6461void _sendJson (HttpRequest request, Object response) => _sendResponse (
65- request,
66- HttpStatus .ok,
67- type: ContentType .json,
68- bytes: _encoder .convert (response),
69- );
62+ request,
63+ HttpStatus .ok,
64+ type: ContentType .json,
65+ bytes: _jsonEncoder .convert (response),
66+ );
7067
7168/// Completes the given [request] by writing the [response] as plain text.
7269void _sendText (HttpRequest request, String response) => _sendResponse (
73- request,
74- HttpStatus .ok,
75- type: ContentType .text,
76- bytes: utf8.encode (response),
77- );
70+ request,
71+ HttpStatus .ok,
72+ type: ContentType .text,
73+ bytes: utf8.encode (response),
74+ );
7875
7976/// Responds with the JSON test to the [request] .
80- void _jsonTest (HttpRequest request) => _sendJson (
81- request,
82- const {'message' : 'Hello, World!' },
83- );
77+ void _jsonTest (HttpRequest request) =>
78+ _sendJson (request, const {'message' : 'Hello, World!' });
8479
8580/// Responds with the plaintext test to the [request] .
86- void _plaintextTest (HttpRequest request) => _sendText (
87- request,
88- 'Hello, World!' ,
89- );
81+ void _plaintextTest (HttpRequest request) => _sendText (request, 'Hello, World!' );
82+
83+ final _jsonEncoder = JsonUtf8Encoder ();
0 commit comments