Skip to content

Commit 2cc8791

Browse files
authored
chore: add firebase protobuf example (#417)
1 parent 6a954ab commit 2cc8791

38 files changed

+2607
-4
lines changed

.github/workflows/verify_examples.yml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,14 @@ jobs:
1818
strategy:
1919
matrix:
2020
example_directory:
21-
- examples/hello
22-
- examples/json
23-
- examples/raw_cloudevent
24-
- examples/fullstack/backend
2521
- dartfn/templates/cloudevent
2622
- dartfn/templates/helloworld
2723
- dartfn/templates/json
24+
- examples/fullstack/backend
25+
- examples/hello
26+
- examples/json
27+
- examples/protobuf_firestore
28+
- examples/raw_cloudevent
2829
defaults:
2930
run:
3031
working-directory: ${{ matrix.example_directory }}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
.dart_tool/
2+
.dockerignore
3+
.git/
4+
.github/
5+
.gitignore
6+
.idea/
7+
Dockerfile
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# See https://dart.dev/guides/libraries/private-files
2+
3+
# Files and directories created by pub
4+
.dart_tool/
5+
pubspec.lock
6+
7+
# Standard files for Cloud Functions conformance tests
8+
# https://github.com/GoogleCloudPlatform/functions-framework-conformance
9+
function_output.json
10+
serverlog_stderr.txt
11+
serverlog_stdout.txt
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
FROM dart:stable AS build
2+
3+
# Resolve app dependencies.
4+
WORKDIR /app
5+
COPY pubspec.* ./
6+
RUN dart pub get
7+
8+
# Copy app source code and AOT compile it.
9+
COPY . .
10+
# Ensure packages are still up-to-date if anything has changed
11+
RUN dart pub get --offline
12+
RUN dart pub run build_runner build --delete-conflicting-outputs
13+
RUN dart compile exe bin/server.dart -o bin/server
14+
15+
# Build minimal serving image from AOT-compiled `/server` and required system
16+
# libraries and configuration files stored in `/runtime/` from the build stage.
17+
FROM scratch
18+
COPY --from=build /runtime/ /
19+
COPY --from=build /app/bin/server /app/bin/
20+
21+
# Start server.
22+
EXPOSE 8080
23+
ENTRYPOINT ["/app/bin/server", "--signature-type=cloudevent"]
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
# Basic CloudEvent example
2+
3+
This example demonstrates writing a function to handle a CloudEvent.
4+
5+
CloudEvent function handlers don't return a response to send to the event
6+
producer. They generally perform some work and print output for logging.
7+
8+
The basic shape of the function handler looks like this:
9+
10+
```dart
11+
@CloudFunction()
12+
void function(CloudEvent event, RequestContext context) {
13+
}
14+
```
15+
16+
Or like this if it needs to perform work that will complete sometime in the
17+
future:
18+
19+
```dart
20+
@CloudFunction()
21+
FutureOr<void> function(CloudEvent event, RequestContext context) async {
22+
}
23+
```
24+
25+
The full code of the function for this example is shown below:
26+
27+
lib/functions.dart
28+
29+
```dart
30+
import 'dart:convert';
31+
import 'dart:io';
32+
33+
import 'package:functions_framework/functions_framework.dart';
34+
35+
const _encoder = JsonEncoder.withIndent(' ');
36+
37+
@CloudFunction()
38+
void function(CloudEvent event, RequestContext context) {
39+
context.logger.info('event subject: ${event.subject}');
40+
stderr.writeln(_encoder.convert(event));
41+
}
42+
```
43+
44+
All the function does is log the source and subject of the CloudEvent that
45+
triggered it, and then prints out the entire event JSON object for informational
46+
purposes.
47+
48+
## Generate project files
49+
50+
The Dart `build_runner` tool generates `bin/server.dart`, the main entry point
51+
for the function server app, which invokes the function in `lib/functions.dart`.
52+
53+
Run the `build_runner` tool, as shown here:
54+
55+
```shell
56+
$ dart run build_runner build
57+
[INFO] Generating build script completed, took 337ms
58+
[INFO] Reading cached asset graph completed, took 48ms
59+
[INFO] Checking for updates since last build completed, took 426ms
60+
[INFO] Running build completed, took 13ms
61+
[INFO] Caching finalized dependency graph completed, took 29ms
62+
[INFO] Succeeded after 51ms with 0 outputs (0 actions)
63+
```
64+
65+
## Test the function
66+
67+
```shell
68+
$ dart test
69+
00:02 +1: All tests passed!
70+
```
71+
72+
## Run the function
73+
74+
The default signature type for a function is for handling normal HTTP requests.
75+
When running a function for handling a cloudevent, you must either set
76+
the `FUNCTION_SIGNATURE_TYPE` environment variable or the
77+
`--signature-type` command line option to `cloudevent`, as shown below:
78+
79+
```shell
80+
$ dart run bin/server.dart --signature-type cloudevent
81+
Listening on :8080
82+
```
83+
84+
From another terminal, trigger the CloudEvent by posting event data:
85+
86+
```shell
87+
$ curl --data-binary @sample/data.json -H 'content-type: application/json' -w '%{http_code}\n' localhost:8080
88+
200
89+
```
90+
91+
Tools like [curl] (and [postman]) are good for sending HTTP requests. The
92+
options used in this example are:
93+
94+
- `--data-binary @sample/data.json` - set the request body to a JSON document
95+
read from the file `sample/data.json`
96+
- `-H "content-type: application/json"` - set an HTTP header to indicate that
97+
the body is a JSON document
98+
- `-w '%{http_code}\n'` - print the HTTP status code (expect 200 for success)
99+
100+
Alternatively, instead of running `curl`, you can run either of the following
101+
Dart scripts examples under the `examples/raw_cloudevent/tool` directory:
102+
103+
- `dart run tool/binary_mode_request.dart`
104+
- `dart run tool/structured_mode_request.dart`
105+
106+
For more details on getting started or to see how to run the function locally on
107+
Docker or deploy to Cloud Run, see these quick start guides:
108+
109+
- [Quickstart: Dart]
110+
- [Quickstart: Docker]
111+
- [Quickstart: Cloud Run]
112+
113+
<!-- reference links -->
114+
[curl]: https://curl.se/docs/manual.html
115+
[Quickstart: Dart]: https://github.com/GoogleCloudPlatform/functions-framework-dart/blob/main/docs/quickstarts/01-quickstart-dart.md
116+
[Quickstart: Docker]: https://github.com/GoogleCloudPlatform/functions-framework-dart/blob/main/docs/quickstarts/02-quickstart-docker.md
117+
[Quickstart: Cloud Run]: https://github.com/GoogleCloudPlatform/functions-framework-dart/blob/main/docs/quickstarts/03-quickstart-cloudrun.md
118+
[postman]: https://www.postman.com/product/api-client/

examples/protobuf_firestore/bin/server.dart

Lines changed: 28 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright 2021 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
import 'dart:convert';
16+
import 'dart:io';
17+
18+
import 'package:functions_framework/functions_framework.dart';
19+
20+
import 'src/function_types.dart';
21+
22+
@CloudFunction()
23+
void function(CloudEvent event, RequestContext context) {
24+
context.logger.info('event subject: ${event.subject}');
25+
context.logger.debug(jsonEncode(context.request.headers));
26+
context.responseHeaders['x-data-runtime-types'] =
27+
event.data.runtimeType.toString();
28+
29+
final instance = DocumentEventData.fromBuffer(event.data as List<int>);
30+
stderr.writeln(jsonEncode(instance.toProto3Json()));
31+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export 'google/events/cloud/firestore/v1/data.pb.dart';
2+
export 'google/protobuf/struct.pbenum.dart' show NullValue;
3+
export 'google/protobuf/timestamp.pb.dart';
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# https://dart.dev/guides/language/analysis-options
2+
analyzer:
3+
errors:
4+
inference_failure_on_collection_literal: ignore
5+
require_trailing_commas: ignore

0 commit comments

Comments
 (0)