Initial implementation of Dart Tooling Daemon server

This will contain the code for running the DTD server.

When this is fully funcitonal VSCode will be responsible for running the, and letting our other tools(i.e. DevTools) know which address it is running at.

Change-Id: Ia4cc9553f000a5e765f604541f4846b107d4d00c
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/338501
Commit-Queue: Dan Chevalier <danchevalier@google.com>
Reviewed-by: Devon Carew <devoncarew@google.com>
Reviewed-by: Ben Konyi <bkonyi@google.com>
diff --git a/pkg/dtd_impl/.gitignore b/pkg/dtd_impl/.gitignore
new file mode 100644
index 0000000..36d54f2
--- /dev/null
+++ b/pkg/dtd_impl/.gitignore
@@ -0,0 +1,4 @@
+# https://dart.dev/guides/libraries/private-files
+# Created by `dart pub`
+.dart_tool/
+bin/dtd_server
\ No newline at end of file
diff --git a/pkg/dtd_impl/CHANGELOG.md b/pkg/dtd_impl/CHANGELOG.md
new file mode 100644
index 0000000..b78d64c
--- /dev/null
+++ b/pkg/dtd_impl/CHANGELOG.md
@@ -0,0 +1,3 @@
+## 0.0.1
+
+- Initial version.
diff --git a/pkg/dtd_impl/LICENSE b/pkg/dtd_impl/LICENSE
new file mode 100644
index 0000000..4fd5739
--- /dev/null
+++ b/pkg/dtd_impl/LICENSE
@@ -0,0 +1,27 @@
+Copyright 2023, the Dart project authors.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are
+met:
+
+    * Redistributions of source code must retain the above copyright
+      notice, this list of conditions and the following disclaimer.
+    * Redistributions in binary form must reproduce the above
+      copyright notice, this list of conditions and the following
+      disclaimer in the documentation and/or other materials provided
+      with the distribution.
+    * Neither the name of Google LLC nor the names of its
+      contributors may be used to endorse or promote products derived
+      from this software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
diff --git a/pkg/dtd_impl/README.md b/pkg/dtd_impl/README.md
new file mode 100644
index 0000000..7120583
--- /dev/null
+++ b/pkg/dtd_impl/README.md
@@ -0,0 +1,19 @@
+The server implementation for the Dart Tooling Daemon. This is meant to be run
+by our internal tooling and facilitates communication between our internal
+tools.
+
+# Running the sample
+
+## Running with the Dart SDK
+
+You can run the example with the [Dart SDK](https://dart.dev/get-dart)
+like this:
+
+```
+$ dart run bin/dtd_server.dart
+The Dart Tooling Daemon is listening on 0.0.0.0:8080
+```
+
+## Compiling the binary
+
+`dart compile exe bin/dtd_server.dart -o bin/dtd_server`
diff --git a/pkg/dtd_impl/analysis_options.yaml b/pkg/dtd_impl/analysis_options.yaml
new file mode 100644
index 0000000..d63aed7
--- /dev/null
+++ b/pkg/dtd_impl/analysis_options.yaml
@@ -0,0 +1,6 @@
+include: package:lints/recommended.yaml
+
+linter:
+  rules:
+    - avoid_void_async
+    - unawaited_futures
diff --git a/pkg/dtd_impl/bin/dtd_server.dart b/pkg/dtd_impl/bin/dtd_server.dart
new file mode 100644
index 0000000..ea89a5c
--- /dev/null
+++ b/pkg/dtd_impl/bin/dtd_server.dart
@@ -0,0 +1,39 @@
+// Copyright (c) 2023, the Dart project authors.  Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+
+import 'dart:io';
+
+import 'package:shelf/shelf.dart';
+import 'package:shelf/shelf_io.dart';
+import 'package:shelf_router/shelf_router.dart';
+
+// Configure routes.
+final _router = Router()
+  ..get('/', _rootHandler)
+  ..get('/echo/<message>', _echoHandler);
+
+Response _rootHandler(Request req) {
+  return Response.ok('Hello, World!\n');
+}
+
+Response _echoHandler(Request request) {
+  final message = request.params['message'];
+  return Response.ok('$message\n');
+}
+
+void main(List<String> args) async {
+  // Use any available host or container IP (usually `0.0.0.0`).
+  final ip = InternetAddress.anyIPv4;
+
+  // Configure a pipeline that logs requests.
+  final handler =
+      Pipeline().addMiddleware(logRequests()).addHandler(_router.call);
+
+  // For running in containers, we respect the PORT environment variable.
+  final port = int.parse(Platform.environment['PORT'] ?? '8080');
+  final server = await serve(handler, ip, port);
+  print(
+    'The Dart Tooling Daemon is listening on ${server.address.host}:${server.port}',
+  );
+}
diff --git a/pkg/dtd_impl/pubspec.yaml b/pkg/dtd_impl/pubspec.yaml
new file mode 100644
index 0000000..8e3f3ab
--- /dev/null
+++ b/pkg/dtd_impl/pubspec.yaml
@@ -0,0 +1,20 @@
+name: dtd_impl
+description: A server app using the shelf package and Docker.
+repository: https://github.com/dart-lang/sdk/tree/main/pkg/dtd_impl
+publish_to: none
+
+environment:
+  sdk: ^3.3.0-152.0.dev
+
+# Use 'any' constraints here; we get our versions from the DEPS file.
+dependencies:
+  shelf: any
+  shelf_router: any
+# We use 'any' version constraints here as we get our package versions from
+# the dart-lang/sdk repo's DEPS file. Note that this is a special case; the
+# best practice for packages is to specify their compatible version ranges.
+# See also https://dart.dev/tools/pub/dependencies.
+# dev_dependencies:
+#   http: any
+#   lints: any
+#   test: any
diff --git a/pkg/dtd_impl/test/server_test.dart b/pkg/dtd_impl/test/server_test.dart
new file mode 100644
index 0000000..90cb4fb
--- /dev/null
+++ b/pkg/dtd_impl/test/server_test.dart
@@ -0,0 +1,44 @@
+// Copyright (c) 2023, the Dart project authors.  Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+
+// TODO(danchevalier) make these tests relevant.
+// import 'dart:io';
+
+// import 'package:http/http.dart';
+// import 'package:test/test.dart';
+
+void main() {
+  // final port = '8080';
+  // final host = 'http://0.0.0.0:$port';
+  // late Process p;
+
+  // setUp(() async {
+  //   p = await Process.start(
+  //     'dart',
+  //     ['run', 'bin/dtd_server.dart'],
+  //     environment: {'PORT': port},
+  //   );
+  //   // Wait for server to start and print to stdout.
+  //   await p.stdout.first;
+  // });
+
+  // tearDown(() => p.kill());
+
+  // test('Root', () async {
+  //   final response = await get(Uri.parse('$host/'));
+  //   expect(response.statusCode, 200);
+  //   expect(response.body, 'Hello, World!\n');
+  // });
+
+  // test('Echo', () async {
+  //   final response = await get(Uri.parse('$host/echo/hello'));
+  //   expect(response.statusCode, 200);
+  //   expect(response.body, 'hello\n');
+  // });
+
+  // test('404', () async {
+  //   final response = await get(Uri.parse('$host/foobar'));
+  //   expect(response.statusCode, 404);
+  // });
+}