|
| 1 | +import 'dart:convert'; |
| 2 | +import 'dart:io'; |
| 3 | + |
| 4 | +/// The fixed TCP port used by the server. |
| 5 | +/// Defined here for visibility and ease of configuration. |
| 6 | +const _defaultPort = 8080; |
| 7 | + |
| 8 | +/// A reusable instance of the UTF-8 JSON encoder to efficiently |
| 9 | +/// transform Dart objects into byte arrays for HTTP responses. |
| 10 | +final _jsonEncoder = JsonUtf8Encoder(); |
| 11 | + |
| 12 | +void main(List<String> args) async { |
| 13 | + /// Binds the [HttpServer] on `0.0.0.0:8080`. |
| 14 | + final server = await HttpServer.bind( |
| 15 | + InternetAddress.anyIPv4, |
| 16 | + _defaultPort, |
| 17 | + ); |
| 18 | + |
| 19 | + server |
| 20 | + ..defaultResponseHeaders.clear() |
| 21 | + /// Sets [HttpServer]'s [serverHeader]. |
| 22 | + ..serverHeader = 'dart_jit' |
| 23 | + /// Handles [HttpRequest]'s from [HttpServer]. |
| 24 | + ..listen(_handleRequest); |
| 25 | +} |
| 26 | + |
| 27 | +/// Dispatches requests to specific test handlers. Wrapped in a try-catch |
| 28 | +/// to ensure stable execution and guaranteed response delivery. |
| 29 | +void _handleRequest(HttpRequest request) { |
| 30 | + try { |
| 31 | + switch (request.uri.path) { |
| 32 | + case '/json': |
| 33 | + _jsonTest(request); |
| 34 | + break; |
| 35 | + case '/plaintext': |
| 36 | + _plaintextTest(request); |
| 37 | + break; |
| 38 | + default: |
| 39 | + _sendResponse(request, HttpStatus.notFound); |
| 40 | + } |
| 41 | + } catch (e) { |
| 42 | + _sendResponse(request, HttpStatus.internalServerError); |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +/// Completes the given [request] by writing the [bytes] with the given |
| 47 | +/// [statusCode] and [type]. |
| 48 | +void _sendResponse( |
| 49 | + HttpRequest request, |
| 50 | + int statusCode, { |
| 51 | + ContentType? type, |
| 52 | + List<int> bytes = const [], |
| 53 | +}) => request.response |
| 54 | + ..statusCode = statusCode |
| 55 | + ..headers.contentType = type |
| 56 | + ..headers.date = DateTime.now() |
| 57 | + ..contentLength = bytes.length |
| 58 | + ..add(bytes) |
| 59 | + ..close(); |
| 60 | + |
| 61 | +/// Completes the given [request] by writing the [response] as JSON. |
| 62 | +void _sendJson(HttpRequest request, Object response) => _sendResponse( |
| 63 | + request, |
| 64 | + HttpStatus.ok, |
| 65 | + type: ContentType.json, |
| 66 | + bytes: _jsonEncoder.convert(response), |
| 67 | +); |
| 68 | + |
| 69 | +/// Completes the given [request] by writing the [response] as plain text. |
| 70 | +void _sendText(HttpRequest request, String response) => _sendResponse( |
| 71 | + request, |
| 72 | + HttpStatus.ok, |
| 73 | + type: ContentType.text, |
| 74 | + bytes: utf8.encode(response), |
| 75 | +); |
| 76 | + |
| 77 | +/// Responds with the JSON test to the [request]. |
| 78 | +void _jsonTest(HttpRequest request) => _sendJson( |
| 79 | + request, |
| 80 | + const {'message': 'Hello, World!'}, |
| 81 | +); |
| 82 | + |
| 83 | +/// Responds with the plaintext test to the [request]. |
| 84 | +void _plaintextTest(HttpRequest request) => _sendText( |
| 85 | + request, |
| 86 | + 'Hello, World!', |
| 87 | +); |
0 commit comments