Skip to content

Commit 6b3ae16

Browse files
authored
Updates for CloudEvents (#17)
* Move HTTP example * Add example of CloudEvent function * Error if data is empty * Add a dummy endpoint for GET healthchecks on / * Update changelog
1 parent 2a30643 commit 6b3ae16

File tree

11 files changed

+66
-1
lines changed

11 files changed

+66
-1
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1111
### Changed
1212
- Make `--debug` a flag instead of a boolean option
1313

14+
### Fixed
15+
- Better support for CloudEvent functions and error handling
16+
1417
## [1.0.1] - 2020-01-30
1518
### Added
1619
- Add Cloud Run Button ([#1])

examples/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
# Python Functions Frameworks Examples
22

3-
* [`cloud_run`](./cloud_run/) - Deploying a function to [Cloud Run](http://cloud.google.com/run) with the Functions Framework
3+
* [`cloud_run_http`](./cloud_run_http/) - Deploying an HTTP function to [Cloud Run](http://cloud.google.com/run) with the Functions Framework
4+
* [`cloud_run_event`](./cloud_run_event/) - Deploying a CloudEvent function to [Cloud Run](http://cloud.google.com/run) with the Functions Framework

examples/cloud_run_event/Dockerfile

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Use the official Python image.
2+
# https://hub.docker.com/_/python
3+
FROM python:3.7-slim
4+
5+
# Copy local code to the container image.
6+
ENV APP_HOME /app
7+
WORKDIR $APP_HOME
8+
COPY . .
9+
10+
# Install production dependencies.
11+
RUN pip install gunicorn functions-framework
12+
RUN pip install -r requirements.txt
13+
14+
# Run the web service on container startup. Here we use the gunicorn
15+
# webserver, with one worker process and 8 threads.
16+
# For environments with multiple CPU cores, increase the number of workers
17+
# to be equal to the cores available.
18+
CMD exec gunicorn --bind :$PORT --workers 1 --threads 8 -e FUNCTION_TARGET=hello -e FUNCTION_SIGNATURE_TYPE=event functions_framework:app

examples/cloud_run_event/main.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Copyright 2020 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+
16+
def hello(data, context):
17+
pass
File renamed without changes.
File renamed without changes.
File renamed without changes.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Optionally include additional dependencies here

src/functions_framework/__init__.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@ def view_func(path):
9393
else:
9494
# This is a regular CloudEvent
9595
event_data = request.get_json()
96+
if not event_data:
97+
flask.abort(400)
9698
event_object = _Event(**event_data)
9799
data = event_object.data
98100
context = Context(**event_object.context)
@@ -184,6 +186,9 @@ def create_app(target=None, source=None, signature_type=None):
184186
werkzeug.routing.Rule("/<path:path>", endpoint="run", methods=["POST"])
185187
)
186188
app.view_functions["run"] = _event_view_func_wrapper(function, flask.request)
189+
# Add a dummy endpoint for GET /
190+
app.url_map.add(werkzeug.routing.Rule("/", endpoint="get", methods=["GET"]))
191+
app.view_functions["get"] = lambda: ""
187192
else:
188193
raise FunctionsFrameworkException(
189194
"Invalid signature type: {signature_type}".format(

0 commit comments

Comments
 (0)