Skip to content

Commit b774b2c

Browse files
authored
feat: init eventarc generic sample (GoogleCloudPlatform#1206)
1 parent aeefbfd commit b774b2c

File tree

7 files changed

+453
-0
lines changed

7 files changed

+453
-0
lines changed

eventarc/generic/.dockerignore

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# The .dockerignore file excludes files from the container build process.
2+
#
3+
# https://docs.docker.com/engine/reference/builder/#dockerignore-file
4+
5+
# Exclude locally vendored dependencies.
6+
vendor/
7+
8+
# Exclude "build-time" ignore files.
9+
.dockerignore
10+
.gcloudignore
11+
12+
# Exclude git history and configuration.
13+
.gitignore
14+
.git

eventarc/generic/.gcloudignore

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# The .gcloudignore file excludes file from upload to Cloud Build.
2+
# If this file is deleted, gcloud will default to .gitignore.
3+
#
4+
# https://cloud.google.com/cloud-build/docs/speeding-up-builds#gcloudignore
5+
# https://cloud.google.com/sdk/gcloud/reference/topic/gcloudignore
6+
7+
# Exclude locally vendored dependencies.
8+
vendor/
9+
10+
# Exclude git history and configuration.
11+
.git/
12+
.gitignore

eventarc/generic/Dockerfile

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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+
# https://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 eventarc_generic_dockerfile]
16+
17+
# Use the official PHP image.
18+
# https://hub.docker.com/_/php
19+
FROM php:7.4-apache
20+
21+
# Configure PHP for Cloud Run.
22+
# Precompile PHP code with opcache.
23+
RUN docker-php-ext-install -j "$(nproc)" opcache
24+
RUN set -ex; \
25+
{ \
26+
echo "; Cloud Run enforces memory & timeouts"; \
27+
echo "memory_limit = -1"; \
28+
echo "max_execution_time = 0"; \
29+
echo "; File upload at Cloud Run network limit"; \
30+
echo "upload_max_filesize = 32M"; \
31+
echo "post_max_size = 32M"; \
32+
echo "; Configure Opcache for Containers"; \
33+
echo "opcache.enable = On"; \
34+
echo "opcache.validate_timestamps = Off"; \
35+
echo "; Configure Opcache Memory (Application-specific)"; \
36+
echo "opcache.memory_consumption = 32"; \
37+
} > "$PHP_INI_DIR/conf.d/cloud-run.ini"
38+
39+
# Copy in custom code from the host machine.
40+
WORKDIR /var/www/html
41+
COPY . ./
42+
43+
# Use the PORT environment variable in Apache configuration files.
44+
# https://cloud.google.com/run/docs/reference/container-contract#port
45+
RUN sed -i 's/80/${PORT}/g' /etc/apache2/sites-available/000-default.conf /etc/apache2/ports.conf
46+
47+
# Configure PHP for development.
48+
# Switch to the production php.ini for production operations.
49+
# RUN mv "$PHP_INI_DIR/php.ini-production" "$PHP_INI_DIR/php.ini"
50+
# https://github.com/docker-library/docs/blob/master/php/README.md#configuration
51+
RUN mv "$PHP_INI_DIR/php.ini-development" "$PHP_INI_DIR/php.ini"
52+
53+
# [END eventarc_generic_dockerfile]

eventarc/generic/README.md

Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
<img src="https://avatars2.githubusercontent.com/u/2810941?v=3&s=96" alt="Google Cloud Platform logo" title="Google Cloud Platform" align="right" height="96" width="96"/>
2+
3+
# Eventarc – Generic – PHP Sample
4+
5+
This directory contains a sample for receiving a generic event using Cloud Run and Eventarc with PHP. For testing purposes, we use Cloud Pub/Sub as an event source for our sample.
6+
7+
## Sample
8+
9+
| Sample | Description | Deploy |
10+
| --------------------------------------- | ------------------------ | ------------- |
11+
|[Generic][generic] | Quickstart | [<img src="https://storage.googleapis.com/cloudrun/button.svg" alt="Run on Google Cloud" height="30"/>][run_button_generic] |
12+
13+
## Setup
14+
15+
1. [Set up for Cloud Run development](https://cloud.google.com/run/docs/setup)
16+
17+
1. Install the gcloud command-line tool beta components:
18+
19+
```sh
20+
gcloud components install beta
21+
```
22+
23+
1. Set the following gcloud configurations, where `PROJECT_ID` is your Google Cloud project ID:
24+
25+
```sh
26+
gcloud config set project PROJECT_ID
27+
gcloud config set run/region us-central1
28+
gcloud config set run/platform managed
29+
gcloud config set eventarc/location us-central1
30+
```
31+
32+
1. [Enable the Cloud Run, Cloud Logging, Cloud Build, Pub/Sub, and Eventarc APIs][enable_apis_url].
33+
34+
1. Clone this repository and navigate to this directory:
35+
36+
```sh
37+
git clone https://github.com/GoogleCloudPlatform/php-docs-samples.git
38+
cd php-docs-samples/eventarc/generic
39+
```
40+
41+
## Run the sample locally
42+
43+
1. [Install docker locally](https://docs.docker.com/install/)
44+
45+
1. [Build the container locally](https://cloud.google.com/run/docs/building/containers#building_locally_and_pushing_using_docker):
46+
47+
```sh
48+
export SAMPLE='generic'
49+
docker build --tag "eventarc-$SAMPLE" .
50+
```
51+
52+
1. [Run containers locally](https://cloud.google.com/run/docs/testing/local)
53+
54+
With the built container:
55+
56+
```sh
57+
PORT=8080 && docker run --rm -p 8080:${PORT} -e PORT=${PORT} $SAMPLE
58+
```
59+
60+
Test the web server with `cURL`:
61+
62+
```sh
63+
curl -XPOST localhost:8080 -d '{ "test": "foo" }'
64+
```
65+
66+
Observe the output logs your HTTP request:
67+
68+
```
69+
Event received!
70+
71+
HEADERS:
72+
Host: localhost:8080
73+
User-Agent: curl/7.64.1
74+
Accept: */*
75+
Content-Length: 17
76+
Content-Type: application/x-www-form-urlencoded
77+
78+
BODY:
79+
{ "test": "foo" }
80+
```
81+
82+
Exit the container with `Ctrl-D`.
83+
84+
## Run the sample on Cloud Run
85+
86+
1. [Build the container using Cloud Build](https://cloud.google.com/run/docs/building/containers#builder)
87+
88+
```sh
89+
gcloud builds submit --tag gcr.io/$(gcloud config get-value project)/eventarc-generic-php
90+
```
91+
92+
1. [Deploy the container](https://cloud.google.com/run/docs/deploying#service)
93+
94+
```sh
95+
gcloud run deploy eventarc-generic-php \
96+
--image gcr.io/$(gcloud config get-value project)/eventarc-generic-php \
97+
--allow-unauthenticated
98+
```
99+
100+
The command line will display the service URL when deployment is complete.
101+
102+
### Create an Eventarc Trigger
103+
104+
1. Create an Eventarc trigger for your Cloud Run service
105+
106+
```sh
107+
gcloud beta eventarc triggers create eventarc-generic-php-trigger \
108+
--destination-run-service=eventarc-generic-php \
109+
--destination-run-region=us-central1 \
110+
--matching-criteria="type=google.cloud.pubsub.topic.v1.messagePublished"
111+
```
112+
113+
1. Confirm the trigger was successfully created, run:
114+
115+
```sh
116+
gcloud beta eventarc triggers describe eventarc-generic-php-trigger
117+
```
118+
119+
> Note: It can take up to 10 minutes for triggers to be fully functional.
120+
121+
### Send an Event
122+
123+
1. Find and set the Pub/Sub topic as an environment variable:
124+
125+
```sh
126+
export RUN_TOPIC=$(gcloud beta eventarc triggers describe eventarc-generic-php-trigger \
127+
--format='value(transport.pubsub.topic)')
128+
```
129+
130+
1. Send a message to the Pub/Sub topic to generate an event:
131+
132+
```sh
133+
gcloud pubsub topics publish $RUN_TOPIC --message="Hello, PHP"
134+
```
135+
136+
The event is sent to the Cloud Run (fully managed) service, which logs the generic HTTP request.
137+
138+
### View an Event in Logs
139+
140+
1. To view the event, go to the Cloud Run (fully managed) service logs:
141+
142+
1. Go to the [Google Cloud Console](https://console.cloud.google.com/run).
143+
144+
1. Click the `eventarc-generic-php` service.
145+
146+
1. Select the **Logs** tab.
147+
148+
> Logs might take a few moments to appear. If you don't see them immediately, check again after a few moments.
149+
150+
1. Look for the log message "Event received!" followed by other log entries. This log entry indicates a request was sent by Eventarc to your Cloud Run service.
151+
152+
### Cleaning Up
153+
154+
To clean up, delete the resources created above:
155+
156+
1. Delete the Cloud Build container:
157+
158+
```sh
159+
gcloud container images delete gcr.io/$(gcloud config get-value project)/eventarc-generic-php
160+
```
161+
162+
1. Delete the Cloud Run service:
163+
164+
```sh
165+
gcloud run services delete eventarc-generic-php
166+
```
167+
168+
1. Delete the Eventarc trigger:
169+
170+
```sh
171+
gcloud beta eventarc triggers delete eventarc-generic-php-trigger
172+
```
173+
174+
1. Delete the Pub/Sub topic:
175+
176+
```sh
177+
gcloud pubsub topics delete $RUN_TOPIC
178+
```
179+
180+
[enable_apis_url]: https://console.cloud.google.com/flows/enableapi?apiid=run.googleapis.com,logging.googleapis.com,cloudbuild.googleapis.com,pubsub.googleapis.com,eventarc.googleapis.com
181+
[generic]: .
182+
[run_button_generic]: https://deploy.cloud.run/?dir=eventarc/generic

eventarc/generic/index.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
2+
/**
3+
* Copyright 2020 Google LLC.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
// [START eventarc_generic_server]
19+
$log = fopen('php://stderr', 'wb');
20+
// [END eventarc_generic_server]
21+
22+
// [START eventarc_generic_handler]
23+
echo "Event received!\n";
24+
fwrite($log, "Event received!\n");
25+
26+
echo "\nHEADERS:\n";
27+
fwrite($log, "\nHEADERS:\n");
28+
$headers = getallheaders();
29+
unset($headers['Authorization']); // do not log authorization header
30+
foreach ($headers as $name => $value) {
31+
echo "$name: $value\n";
32+
fwrite($log, "$name: $value\n");
33+
}
34+
35+
echo "\nBODY:\n";
36+
fwrite($log, "\nBODY:\n");
37+
$body = file_get_contents('php://input');
38+
echo $body . "\n";
39+
fwrite($log, $body . "\n");
40+
// [END eventarc_generic_handler]

eventarc/generic/phpunit.xml.dist

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
xml version="1.0" encoding="UTF-8"?>
2+
14+
<phpunit bootstrap="../../testing/vendor/autoload.php" convertWarningsToExceptions="false">
15+
<testsuites>
16+
<testsuite name="Eventarc generic tests">
17+
<directory>testdirectory>
18+
testsuite>
19+
testsuites>
20+
<logging>
21+
<log type="coverage-clover" target="build/logs/clover.xml"/>
22+
logging>
23+
phpunit>

0 commit comments

Comments
 (0)