|
| 1 | +# Python 3 Google Cloud Pub/Sub sample for Google App Engine Standard Environment |
| 2 | + |
| 3 | +[![Open in Cloud Shell][shell_img]][shell_link] |
| 4 | + |
| 5 | +[shell_img]: http://gstatic.com/cloudssh/images/open-btn.png |
| 6 | +[shell_link]: https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/GoogleCloudPlatform/python-docs-samples&page=editor&open_in_editor=appengine/standard/pubsub/README.md |
| 7 | + |
| 8 | +This demonstrates how to send and receive messages using [Google Cloud Pub/Sub](https://cloud.google.com/pubsub) on [Google App Engine Standard Environment](https://cloud.google.com/appengine/docs/standard/). |
| 9 | + |
| 10 | +## Setup |
| 11 | + |
| 12 | +Before you can run or deploy the sample, you will need to do the following: |
| 13 | + |
| 14 | +1. Enable the Cloud Pub/Sub API in the [Google Developers Console](https://console.developers.google.com/project/_/apiui/apiview/pubsub/overview). |
| 15 | + |
| 16 | +2. Create a topic and subscription. The push auth service account must have Service Account Token Creator Role assigned, which can be done in the Cloud Console [IAM & admin](https://console.cloud.google.com/iam-admin/iam) UI. `--push-auth-token-audience` is optional. If set, remember to modify the audience field check in `main.py` (line 88). |
| 17 | + |
| 18 | + $ gcloud pubsub topics create [your-topic-name] |
| 19 | + $ gcloud beta pubsub subscriptions create [your-subscription-name] \ |
| 20 | + --topic=[your-topic-name] \ |
| 21 | + --push-endpoint=\ |
| 22 | + https://[your-app-id].appspot.com/_ah/push-handlers/receive_messages/token=[your-token] \ |
| 23 | + --ack-deadline=30 \ |
| 24 | + --push-auth-service-account=[your-service-account-email] \ |
| 25 | + --push-auth-token-audience=example.com |
| 26 | + |
| 27 | +3. Update the environment variables in ``app.yaml``. |
| 28 | + |
| 29 | +## Running locally |
| 30 | + |
| 31 | +When running locally, you can use the [Google Cloud SDK](https://cloud.google.com/sdk) to provide authentication to use Google Cloud APIs: |
| 32 | + |
| 33 | + $ gcloud init |
| 34 | + |
| 35 | +Install dependencies, preferably with a virtualenv: |
| 36 | + |
| 37 | + $ virtualenv env |
| 38 | + $ source env/bin/activate |
| 39 | + $ pip install -r requirements.txt |
| 40 | + |
| 41 | +Then set environment variables before starting your application: |
| 42 | + |
| 43 | + $ export GOOGLE_CLOUD_PROJECT=[your-project-name] |
| 44 | + $ export PUBSUB_VERIFICATION_TOKEN=[your-verification-token] |
| 45 | + $ export PUBSUB_TOPIC=[your-topic] |
| 46 | + $ python main.py |
| 47 | + |
| 48 | +### Simulating push notifications |
| 49 | + |
| 50 | +The application can send messages locally, but it is not able to receive push messages locally. You can, however, simulate a push message by making an HTTP request to the local push notification endpoint. There is an included ``sample_message.json``. You can use |
| 51 | +``curl`` or [httpie](https://github.com/jkbrzt/httpie) to POST this: |
| 52 | + |
| 53 | + $ curl -i --data @sample_message.json "localhost:8080/_ah/push-handlers/receive_messages?token=[your-token]" |
| 54 | + |
| 55 | +Or |
| 56 | + |
| 57 | + $ http POST ":8080/_ah/push-handlers/receive_messages?token=[your-token]" < sample_message.json |
| 58 | + |
| 59 | +Response: |
| 60 | + |
| 61 | + HTTP/1.0 400 BAD REQUEST |
| 62 | + Content-Type: text/html; charset=utf-8 |
| 63 | + Content-Length: 58 |
| 64 | + Server: Werkzeug/0.15.2 Python/3.7.3 |
| 65 | + Date: Sat, 06 Apr 2019 04:56:12 GMT |
| 66 | + |
| 67 | + Invalid token: 'NoneType' object has no attribute 'split' |
| 68 | + |
| 69 | +The simulated push request fails because it does not have a Cloud Pub/Sub-generated JWT in the "Authorization" header. |
| 70 | + |
| 71 | +## Running on App Engine |
| 72 | + |
| 73 | +Note: Not all the files in the current directory are needed to run your code on App Engine. Specifically, `main_test.py` and the `data` directory, which contains a mocked private key file and a mocked public certs file, are for testing purposes only. They SHOULD NOT be included in when deploying your app. When your app is up and running, Cloud Pub/Sub creates tokens using a private key, then the Google Auth Python library takes care of verifying and decoding the token using Google's public certs, to confirm that the push requests indeed come from Cloud Pub/Sub. |
| 74 | + |
| 75 | +In the current directory, deploy using `gcloud`: |
| 76 | + |
| 77 | + $ gcloud app deploy app.yaml |
| 78 | + |
| 79 | +You can now access the application at `https://[your-app-id].appspot.com`. You can use the form to submit messages, but it's non-deterministic which instance of your application will receive the notification. You can send multiple messages and refresh the page to see the received message. |
0 commit comments