|
| 1 | +# Copyright 2018 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 | +# [START app] |
| 16 | +import base64 |
| 17 | +import json |
| 18 | +import logging |
| 19 | +import os |
| 20 | + |
| 21 | +from flask import current_app, Flask, render_template, request |
| 22 | +from googleapiclient.discovery import build |
| 23 | + |
| 24 | + |
| 25 | +app = Flask(__name__) |
| 26 | + |
| 27 | +# Configure the following environment variables via app.yaml |
| 28 | +# This is used in the push request handler to veirfy that the request came from |
| 29 | +# pubsub and originated from a trusted source. |
| 30 | +app.config['PUBSUB_VERIFICATION_TOKEN'] = \ |
| 31 | + os.environ['PUBSUB_VERIFICATION_TOKEN'] |
| 32 | +app.config['PUBSUB_TOPIC'] = os.environ['PUBSUB_TOPIC'] |
| 33 | +app.config['GCLOUD_PROJECT'] = os.environ['GOOGLE_CLOUD_PROJECT'] |
| 34 | + |
| 35 | + |
| 36 | +# Global list to storage messages received by this instance. |
| 37 | +MESSAGES = [] |
| 38 | + |
| 39 | + |
| 40 | +# [START index] |
| 41 | +@app.route('/', methods=['GET', 'POST']) |
| 42 | +def index(): |
| 43 | + if request.method == 'GET': |
| 44 | + return render_template('index.html', messages=MESSAGES) |
| 45 | + |
| 46 | + data = request.form.get('payload', 'Example payload').encode('utf-8') |
| 47 | + |
| 48 | + service = build('pubsub', 'v1') |
| 49 | + topic_path = 'projects/{project_id}/topics/{topic}'.format( |
| 50 | + project_id=app.config['GCLOUD_PROJECT'], |
| 51 | + topic=app.config['PUBSUB_TOPIC'] |
| 52 | + ) |
| 53 | + service.projects().topics().publish( |
| 54 | + topic=topic_path, body={ |
| 55 | + "messages": [{ |
| 56 | + "data": base64.b64encode(data) |
| 57 | + }] |
| 58 | + }).execute() |
| 59 | + |
| 60 | + return 'OK', 200 |
| 61 | +# [END index] |
| 62 | + |
| 63 | + |
| 64 | +# [START push] |
| 65 | +@app.route('/_ah/push-handlers/receive_messages', methods=['POST']) |
| 66 | +def receive_messages_handler(): |
| 67 | + if (request.args.get('token', '') != |
| 68 | + current_app.config['PUBSUB_VERIFICATION_TOKEN']): |
| 69 | + return 'Invalid request', 400 |
| 70 | + |
| 71 | + envelope = json.loads(request.data.decode('utf-8')) |
| 72 | + payload = base64.b64decode(envelope['message']['data']) |
| 73 | + |
| 74 | + MESSAGES.append(payload) |
| 75 | + |
| 76 | + # Returning any 2xx status indicates successful receipt of the message. |
| 77 | + return 'OK', 200 |
| 78 | +# [END push] |
| 79 | + |
| 80 | + |
| 81 | +@app.errorhandler(500) |
| 82 | +def server_error(e): |
| 83 | + logging.exception('An error occurred during a request.') |
| 84 | + return """ |
| 85 | + An internal error occurred: <pre>{}</pre> |
| 86 | + See logs for full stacktrace. |
| 87 | + """.format(e), 500 |
| 88 | + |
| 89 | + |
| 90 | +if __name__ == '__main__': |
| 91 | + # This is used when running locally. Gunicorn is used to run the |
| 92 | + # application on Google App Engine. See entrypoint in app.yaml. |
| 93 | + app.run(host='127.0.0.1', port=8080, debug=True) |
| 94 | +# [END app] |
0 commit comments