|
| 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/ |
0 commit comments