From abdf346963e01a4292247b2fdc58cff7ee621645 Mon Sep 17 00:00:00 2001 From: Parker Lougheed Date: Sat, 12 Aug 2023 16:07:18 -0500 Subject: [PATCH 1/2] Update Dart sample to Dart 3 --- .../serving/helloworld-dart/.gitignore | 5 +- .../serving/helloworld-dart/README.md | 6 +- .../serving/helloworld-dart/index.md | 69 ++++--------------- .../serving/helloworld-dart/pubspec.yaml | 12 ++-- 4 files changed, 24 insertions(+), 68 deletions(-) diff --git a/code-samples/community/serving/helloworld-dart/.gitignore b/code-samples/community/serving/helloworld-dart/.gitignore index 9af85e5b15b..514935e4230 100644 --- a/code-samples/community/serving/helloworld-dart/.gitignore +++ b/code-samples/community/serving/helloworld-dart/.gitignore @@ -1,4 +1,3 @@ -# See https://www.dartlang.org/guides/libraries/private-files -.dart_tool -.packages +# https://dart.dev/guides/libraries/private-files +.dart_tool/ pubspec.lock diff --git a/code-samples/community/serving/helloworld-dart/README.md b/code-samples/community/serving/helloworld-dart/README.md index 57c8bb619a2..57283243332 100644 --- a/code-samples/community/serving/helloworld-dart/README.md +++ b/code-samples/community/serving/helloworld-dart/README.md @@ -8,11 +8,11 @@ that you can use for testing. It reads in the env variable `TARGET` and prints ## Prerequisites - A Kubernetes cluster with Knative installed and DNS configured. Follow the - [Knative installation instructions](https://knative.dev/docs/install/) if you need to create - one. + [Knative installation instructions](https://knative.dev/docs/install/) if + you need to create one. - [Docker](https://www.docker.com) installed and running on your local machine, and a Docker Hub account configured (we'll use it for a container registry). -- [dart-sdk](https://www.dart.dev/tools/sdk#install) installed and +- [dart-sdk](https://dart.dev/get-dart) installed and configured if you want to run the program locally. ## Recreating the sample code diff --git a/code-samples/community/serving/helloworld-dart/index.md b/code-samples/community/serving/helloworld-dart/index.md index 18744a3d4de..0701156dfdf 100644 --- a/code-samples/community/serving/helloworld-dart/index.md +++ b/code-samples/community/serving/helloworld-dart/index.md @@ -8,76 +8,33 @@ that you can use for testing. It reads in the env variable `TARGET` and prints ## Prerequisites - A Kubernetes cluster with Knative installed and DNS configured. Follow the - [Knative installation instructions](https://knative.dev/docs/install/) if you need to create - one. + [Knative installation instructions](https://knative.dev/docs/install/) if + you need to create one. - [Docker](https://www.docker.com) installed and running on your local machine, and a Docker Hub account configured (we'll use it for a container registry). -- [dart-sdk](https://www.dart.dev/tools/sdk#install) installed and +- [dart-sdk](https://dart.dev/get-dart) installed and configured if you want to run the program locally. ## Recreating the sample code -While you can clone all of the code from this directory, it is useful to know -how to build a hello world Dart application step-by-step. This application can -be created using the following instructions. +While you can clone all of the code from this directory, we recommend you create +your hello world Dart application by using the `dart` developer tool. This takes +just a few steps: -1. Create a new directory and write `pubspec.yaml` as follows: +1. Create a new Dart app using the `server_shelf` template: - ```yaml - name: hello_world_dart - publish_to: none # let's not accidentally publish this to pub.dartlang.org - - environment: - sdk: ">=2.12.0 <3.0.0" - - dependencies: - shelf: ^1.0.0 + ```shell + > dart create -t server-shelf helloworld-dart ``` -2. If you want to run locally, install dependencies. If you only want to run in +1. If you want to run locally, install dependencies. If you only want to run in Docker or Knative, you can skip this step. - ```bash - > pub get - ``` - -3. Create a new file `bin/server.dart` and write the following code: - - ```dart - import 'dart:io'; - - import 'package:shelf/shelf.dart'; - import 'package:shelf/shelf_io.dart'; - - Future main() async { - // Find port to listen on from environment variable. - final port = int.parse(Platform.environment['PORT'] ?? '8080'); - - // Read $TARGET from environment variable. - final target = Platform.environment['TARGET'] ?? 'World'; - - Response handler(Request request) => Response.ok('Hello $target'); - - // Serve handler on given port. - final server = await serve( - const Pipeline().addMiddleware(logRequests()).addHandler(handler), - InternetAddress.anyIPv4, - port, - ); - print('Serving at http://${server.address.host}:${server.port}'); - } - ``` - -4. Create a new file named `Dockerfile`, this file defines instructions for - dockerizing your applications, for dart apps this can be done as follows: - - ```Dockerfile - # Use Google's official Dart image. - # https://hub.docker.com/r/google/dart-runtime/ - FROM google/dart-runtime + ```shell + > dart pub get ``` -5. Create a new file, `service.yaml` and copy the following service definition +1. Create a new file, `service.yaml` and copy the following service definition into the file. Make sure to replace `{username}` with your Docker Hub username. diff --git a/code-samples/community/serving/helloworld-dart/pubspec.yaml b/code-samples/community/serving/helloworld-dart/pubspec.yaml index fd2703e13d1..d4b3242706b 100644 --- a/code-samples/community/serving/helloworld-dart/pubspec.yaml +++ b/code-samples/community/serving/helloworld-dart/pubspec.yaml @@ -1,15 +1,15 @@ name: hello_world_dart -publish_to: none # let's not accidentally publish this to pub.dartlang.org +publish_to: none # Avoid accidentally publishing this to pub.dev environment: - sdk: ">=2.17.0 <3.0.0" + sdk: ^3.0.0 dependencies: - args: ^2.0.0 - shelf: ^1.1.0 - shelf_router: ^1.0.0 + args: ^2.3.0 + shelf: ^1.4.0 + shelf_router: ^1.1.0 dev_dependencies: http: ^0.13.0 lints: ^2.0.0 - test: ^1.15.0 + test: ^1.21.0 From 131fa0723956df3b966f5b372b3c39713d3ec401 Mon Sep 17 00:00:00 2001 From: Parker Lougheed Date: Sat, 12 Aug 2023 16:12:06 -0500 Subject: [PATCH 2/2] Update Dart SDK mention --- code-samples/community/serving/helloworld-dart/README.md | 2 +- code-samples/community/serving/helloworld-dart/index.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/code-samples/community/serving/helloworld-dart/README.md b/code-samples/community/serving/helloworld-dart/README.md index 57283243332..3f50d54f53f 100644 --- a/code-samples/community/serving/helloworld-dart/README.md +++ b/code-samples/community/serving/helloworld-dart/README.md @@ -12,7 +12,7 @@ that you can use for testing. It reads in the env variable `TARGET` and prints you need to create one. - [Docker](https://www.docker.com) installed and running on your local machine, and a Docker Hub account configured (we'll use it for a container registry). -- [dart-sdk](https://dart.dev/get-dart) installed and +- The [Dart SDK](https://dart.dev/get-dart) installed and configured if you want to run the program locally. ## Recreating the sample code diff --git a/code-samples/community/serving/helloworld-dart/index.md b/code-samples/community/serving/helloworld-dart/index.md index 0701156dfdf..2031d223a85 100644 --- a/code-samples/community/serving/helloworld-dart/index.md +++ b/code-samples/community/serving/helloworld-dart/index.md @@ -12,7 +12,7 @@ that you can use for testing. It reads in the env variable `TARGET` and prints you need to create one. - [Docker](https://www.docker.com) installed and running on your local machine, and a Docker Hub account configured (we'll use it for a container registry). -- [dart-sdk](https://dart.dev/get-dart) installed and +- The [Dart SDK](https://dart.dev/get-dart) installed and configured if you want to run the program locally. ## Recreating the sample code