Skip to content

Commit 165e1a0

Browse files
author
Ace Nassri
authored
Merge branch 'master' into functions-rtdb
2 parents a449e6f + 5264a4a commit 165e1a0

File tree

9 files changed

+505
-0
lines changed

9 files changed

+505
-0
lines changed

eventarc/README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
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 – PHP Samples
4+
5+
This directory contains samples for using Eventarc with PHP.
6+
7+
## Sample
8+
9+
| Sample | Description |
10+
| --------------------------------------- | ------------------------ |
11+
|[Generic](generic) | Quickstart |

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