Skip to content

Commit 6dc822e

Browse files
jonasfjknative-prow-robot
authored andcommitted
Added a serving sample for dart (#452)
1 parent c9430ee commit 6dc822e

File tree

6 files changed

+295
-0
lines changed

6 files changed

+295
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
FROM google/dart-runtime
Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
# Hello World - Dart sample
2+
3+
A simple web app written in the [Dart](www.dartlang.org) programming language
4+
that you can use for testing. It reads in the env variable `TARGET` and prints
5+
`"Hello $TARGET"`. If `TARGET` is not specified, it will use `"World"` as `TARGET`.
6+
7+
## Prerequisites
8+
9+
* A Kubernetes cluster with Knative installed. Follow the
10+
[installation instructions](https://github.com/knative/docs/blob/master/install/README.md) if you need
11+
to create one.
12+
* [Docker](https://www.docker.com) installed and running on your local machine,
13+
and a Docker Hub account configured (we'll use it for a container registry).
14+
* [dart-sdk](https://www.dartlang.org/tools/sdk#install) installed and configured.
15+
16+
## Recreating the sample code
17+
18+
While you can clone all of the code from this directory, it is useful to know how
19+
to build a hello world Dart application step-by-step. This application can be
20+
created using the following instructions.
21+
22+
1. Create a new directory and write `pubspec.yaml` as follows:
23+
24+
```yaml
25+
name: hello_world_dart
26+
private: True # let's not accidentally publish this to pub.dartlang.org
27+
description: >-
28+
Hello world server example in dart.
29+
dependencies:
30+
shelf: ^0.7.3
31+
environment:
32+
sdk: '>=2.0.0 <3.0.0'
33+
```
34+
35+
2. Install dependencies
36+
37+
```shell
38+
pub get
39+
```
40+
41+
3. Create a new file `bin/main.dart` and write the following code:
42+
43+
```dart
44+
import 'dart:io';
45+
46+
import 'package:shelf/shelf.dart';
47+
import 'package:shelf/shelf_io.dart';
48+
49+
void main() {
50+
// Find port to listen on from environment variable.
51+
var port = int.tryParse(Platform.environment['PORT']);
52+
53+
// Read $TARGET from environment variable.
54+
var target = Platform.environment['TARGET'] ?? 'World';
55+
56+
// Create handler.
57+
var handler = Pipeline().addMiddleware(logRequests()).addHandler((request) {
58+
return Response.ok('Hello $target');
59+
});
60+
61+
// Serve handler on given port.
62+
serve(handler, InternetAddress.anyIPv4, port).then((server) {
63+
print('Serving at http://${server.address.host}:${server.port}');
64+
});
65+
}
66+
```
67+
68+
4. Create a new file named `Dockerfile`, this file defines instructions for
69+
dockerizing your applications, for dart apps this can be done as follows:
70+
71+
```Dockerfile
72+
FROM google/dart-runtime
73+
```
74+
75+
5. Create a new file, `service.yaml` and copy the following service definition
76+
into the file. Make sure to replace `{username}` with your Docker Hub username.
77+
78+
```yaml
79+
apiVersion: serving.knative.dev/v1alpha1
80+
kind: Service
81+
metadata:
82+
name: helloworld-dart
83+
namespace: default
84+
spec:
85+
runLatest:
86+
configuration:
87+
revisionTemplate:
88+
spec:
89+
container:
90+
image: docker.io/{username}/helloworld-dart
91+
env:
92+
- name: TARGET
93+
value: "Dart Sample v1"
94+
```
95+
96+
## Building and deploying the sample
97+
98+
Once you have recreated the sample code files (or used the files in the sample
99+
folder) you're ready to build and deploy the sample app.
100+
101+
1. Use Docker to build the sample code into a container. To build and push with
102+
Docker Hub, run these commands replacing `{username}` with your
103+
Docker Hub username:
104+
105+
```shell
106+
# Build the container on your local machine
107+
docker build -t {username}/helloworld-dart .
108+
109+
# Push the container to docker registry
110+
docker push {username}/helloworld-dart
111+
```
112+
113+
1. After the build has completed and the container is pushed to docker hub, you
114+
can deploy the app into your cluster. Ensure that the container image value
115+
in `service.yaml` matches the container you built in
116+
the previous step. Apply the configuration using `kubectl`:
117+
118+
```shell
119+
kubectl apply --filename service.yaml
120+
```
121+
122+
1. Now that your service is created, Knative will perform the following steps:
123+
* Create a new immutable revision for this version of the app.
124+
* Network programming to create a route, ingress, service, and load balance for your app.
125+
* Automatically scale your pods up and down (including to zero active pods).
126+
127+
1. To find the IP address for your service, use
128+
`kubectl get svc knative-ingressgateway -n istio-system` to get the ingress IP for your
129+
cluster. If your cluster is new, it may take sometime for the service to get asssigned
130+
an external IP address.
131+
132+
```shell
133+
kubectl get svc knative-ingressgateway --namespace istio-system
134+
135+
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
136+
knative-ingressgateway LoadBalancer 10.23.247.74 35.203.155.229 80:32380/TCP,443:32390/TCP,32400:32400/TCP 2d
137+
138+
```
139+
140+
1. To find the URL for your service, use
141+
```
142+
kubectl get services.serving.knative.dev helloworld-dart --output=custom-columns=NAME:.metadata.name,DOMAIN:.status.domain
143+
NAME DOMAIN
144+
helloworld-dart helloworld-dart.default.example.com
145+
```
146+
147+
1. Now you can make a request to your app to see the result. Replace
148+
`{IP_ADDRESS}` with the address you see returned in the previous step.
149+
150+
```shell
151+
curl -H "Host: helloworld-dart.default.example.com" http://{IP_ADDRESS}
152+
Hello Dart Sample v1
153+
```
154+
155+
## Removing the sample app deployment
156+
157+
To remove the sample app from your cluster, delete the service record:
158+
159+
```shell
160+
kubectl delete --filename service.yaml
161+
```
162+
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import 'dart:io';
2+
3+
import 'package:shelf/shelf.dart';
4+
import 'package:shelf/shelf_io.dart';
5+
6+
void main() {
7+
// Find port to listen on from environment variable.
8+
var port = int.tryParse(Platform.environment['PORT']);
9+
10+
// Read $TARGET from environment variable.
11+
var target = Platform.environment['TARGET'] ?? 'World';
12+
13+
// Create handler.
14+
var handler = Pipeline().addMiddleware(logRequests()).addHandler((request) {
15+
return Response.ok('Hello $target');
16+
});
17+
18+
// Serve handler on given port.
19+
serve(handler, InternetAddress.anyIPv4, port).then((server) {
20+
print('Serving at http://${server.address.host}:${server.port}');
21+
});
22+
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
# Generated by pub
2+
# See https://www.dartlang.org/tools/pub/glossary#lockfile
3+
packages:
4+
async:
5+
dependency: transitive
6+
description:
7+
name: async
8+
url: "https://pub.dartlang.org"
9+
source: hosted
10+
version: "2.0.8"
11+
charcode:
12+
dependency: transitive
13+
description:
14+
name: charcode
15+
url: "https://pub.dartlang.org"
16+
source: hosted
17+
version: "1.1.2"
18+
collection:
19+
dependency: transitive
20+
description:
21+
name: collection
22+
url: "https://pub.dartlang.org"
23+
source: hosted
24+
version: "1.14.11"
25+
http_parser:
26+
dependency: transitive
27+
description:
28+
name: http_parser
29+
url: "https://pub.dartlang.org"
30+
source: hosted
31+
version: "3.1.3"
32+
meta:
33+
dependency: transitive
34+
description:
35+
name: meta
36+
url: "https://pub.dartlang.org"
37+
source: hosted
38+
version: "1.1.6"
39+
path:
40+
dependency: transitive
41+
description:
42+
name: path
43+
url: "https://pub.dartlang.org"
44+
source: hosted
45+
version: "1.6.2"
46+
shelf:
47+
dependency: "direct main"
48+
description:
49+
name: shelf
50+
url: "https://pub.dartlang.org"
51+
source: hosted
52+
version: "0.7.3+3"
53+
source_span:
54+
dependency: transitive
55+
description:
56+
name: source_span
57+
url: "https://pub.dartlang.org"
58+
source: hosted
59+
version: "1.4.1"
60+
stack_trace:
61+
dependency: transitive
62+
description:
63+
name: stack_trace
64+
url: "https://pub.dartlang.org"
65+
source: hosted
66+
version: "1.9.3"
67+
stream_channel:
68+
dependency: transitive
69+
description:
70+
name: stream_channel
71+
url: "https://pub.dartlang.org"
72+
source: hosted
73+
version: "1.6.8"
74+
string_scanner:
75+
dependency: transitive
76+
description:
77+
name: string_scanner
78+
url: "https://pub.dartlang.org"
79+
source: hosted
80+
version: "1.0.4"
81+
typed_data:
82+
dependency: transitive
83+
description:
84+
name: typed_data
85+
url: "https://pub.dartlang.org"
86+
source: hosted
87+
version: "1.1.6"
88+
sdks:
89+
dart: ">=2.0.0 <3.0.0"
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
name: hello_world_dart
2+
private: True # let's not accidentally publish this to pub.dartlang.org
3+
dependencies:
4+
shelf: ^0.7.3
5+
environment:
6+
sdk: '>=2.0.0 <3.0.0'
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
apiVersion: serving.knative.dev/v1alpha1
2+
kind: Service
3+
metadata:
4+
name: helloworld-dart
5+
namespace: default
6+
spec:
7+
runLatest:
8+
configuration:
9+
revisionTemplate:
10+
spec:
11+
container:
12+
image: docker.io/{username}/helloworld-dart
13+
env:
14+
- name: TARGET
15+
value: "Dart Sample v1"

0 commit comments

Comments
 (0)