@@ -134,14 +134,14 @@ response instead.
134
134
1 . Create a ` main.py ` file with the following contents:
135
135
136
136
``` python
137
- def hello (request ):
138
- return " Hello world! "
137
+ def hello (event , context ):
138
+ print ( " Received " , context.event_id)
139
139
```
140
140
141
141
1 . Start the Functions Framework on port 8080:
142
142
143
143
``` sh
144
- functions-framework --target=hello --debug --port=8080
144
+ functions-framework --target=hello --signature-type=event -- debug --port=8080
145
145
```
146
146
147
147
1 . In a second terminal, start the Pub/Sub emulator on port 8085.
@@ -159,7 +159,7 @@ response instead.
159
159
[pubsub] INFO: Server started, listening on 8085
160
160
```
161
161
162
- 1 . In a third terminal, create a Pub/Sub topic and attach a push subscription to the topic, using ` http://localhost:8085 ` as its push endpoint. [ Publish] ( https://cloud.google.com/pubsub/docs/quickstart-client-libraries#publish_messages ) some messages to the topic. Observe your function getting triggered by the Pub/Sub messages.
162
+ 1 . In a third terminal, create a Pub/Sub topic and attach a push subscription to the topic, using ` http://localhost:8080 ` as its push endpoint. [ Publish] ( https://cloud.google.com/pubsub/docs/quickstart-client-libraries#publish_messages ) some messages to the topic. Observe your function getting triggered by the Pub/Sub messages.
163
163
164
164
``` sh
165
165
export PUBSUB_PROJECT_ID=my-project
@@ -172,7 +172,7 @@ response instead.
172
172
pip install -r requirements.txt
173
173
174
174
python publisher.py $PUBSUB_PROJECT_ID create $TOPIC_ID
175
- python subscriber.py $PUBSUB_PROJECT_ID create-push $TOPIC_ID $PUSH_SUBSCRIPTION_ID http://localhost:8085
175
+ python subscriber.py $PUBSUB_PROJECT_ID create-push $TOPIC_ID $PUSH_SUBSCRIPTION_ID http://localhost:8080
176
176
python publisher.py $PUBSUB_PROJECT_ID publish $TOPIC_ID
177
177
```
178
178
@@ -183,14 +183,14 @@ response instead.
183
183
184
184
topic: "projects/my-project/topics/my-topic"
185
185
push_config {
186
- push_endpoint: "http://localhost:8085 "
186
+ push_endpoint: "http://localhost:8080 "
187
187
}
188
188
ack_deadline_seconds: 10
189
189
message_retention_duration {
190
190
seconds: 604800
191
191
}
192
192
.
193
- Endpoint for subscription is: http://localhost:8085
193
+ Endpoint for subscription is: http://localhost:8080
194
194
195
195
1
196
196
2
@@ -204,6 +204,41 @@ response instead.
204
204
Published messages to projects/my-project/topics/my-topic.
205
205
```
206
206
207
+ And in the terminal where the Functions Framework is running:
208
+
209
+ ``` none
210
+ * Serving Flask app "hello" (lazy loading)
211
+ * Environment: production
212
+ WARNING: This is a development server. Do not use it in a production deployment.
213
+ Use a production WSGI server instead.
214
+ * Debug mode: on
215
+ * Running on http://0.0.0.0:8080/ (Press CTRL+C to quit)
216
+ * Restarting with fsevents reloader
217
+ * Debugger is active!
218
+ * Debugger PIN: 911-794-046
219
+ Received 1
220
+ 127.0.0.1 - - [11/Aug/2021 14:42:22] "POST / HTTP/1.1" 200 -
221
+ Received 2
222
+ 127.0.0.1 - - [11/Aug/2021 14:42:22] "POST / HTTP/1.1" 200 -
223
+ Received 5
224
+ 127.0.0.1 - - [11/Aug/2021 14:42:22] "POST / HTTP/1.1" 200 -
225
+ Received 6
226
+ 127.0.0.1 - - [11/Aug/2021 14:42:22] "POST / HTTP/1.1" 200 -
227
+ Received 7
228
+ 127.0.0.1 - - [11/Aug/2021 14:42:22] "POST / HTTP/1.1" 200 -
229
+ Received 8
230
+ 127.0.0.1 - - [11/Aug/2021 14:42:22] "POST / HTTP/1.1" 200 -
231
+ Received 9
232
+ 127.0.0.1 - - [11/Aug/2021 14:42:39] "POST / HTTP/1.1" 200 -
233
+ Received 3
234
+ 127.0.0.1 - - [11/Aug/2021 14:42:39] "POST / HTTP/1.1" 200 -
235
+ Received 4
236
+ 127.0.0.1 - - [11/Aug/2021 14:42:39] "POST / HTTP/1.1" 200 -
237
+ ```
238
+
239
+ For more details on extracting data from a Pub/Sub event, see
240
+ https://cloud.google.com/functions/docs/tutorials/pubsub#functions_helloworld_pubsub_tutorial-python
241
+
207
242
### Quickstart: Build a Deployable Container
208
243
209
244
1 . Install [ Docker] ( https://store.docker.com/search?type=edition&offering=community ) and the [ ` pack ` tool] ( https://buildpacks.io/docs/install-pack/ ) .
@@ -263,13 +298,13 @@ You can configure the Functions Framework using command-line flags or environmen
263
298
## Enable Google Cloud Functions Events
264
299
265
300
The Functions Framework can unmarshall incoming
266
- Google Cloud Functions [ event] ( https://cloud.google.com/functions/docs/concepts/events-triggers#events ) payloads to ` data ` and ` context ` objects.
301
+ Google Cloud Functions [ event] ( https://cloud.google.com/functions/docs/concepts/events-triggers#events ) payloads to ` event ` and ` context ` objects.
267
302
These will be passed as arguments to your function when it receives a request.
268
303
Note that your function must use the ` event ` -style function signature:
269
304
270
305
``` python
271
- def hello (data , context ):
272
- print (data )
306
+ def hello (event , context ):
307
+ print (event )
273
308
print (context)
274
309
```
275
310
0 commit comments