Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 9 additions & 21 deletions appengine/flexible/pubsub/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,40 +5,28 @@
This sample demonstrates how to invoke PubSub from Google App Engine Flexible
Environment.

The sample code lives in [a parent pubsub directory](../../../pubsub).
The sample code lives in [a parent pubsub directory](../../../pubsub/app).
Only two configuration files differ: `app.yaml` and `nginx-app.conf`.

## Register your application

- Go to
[Google Developers Console](https://console.developers.google.com/project)
and create a new project. This will automatically enable an App
Engine application with the same ID as the project.

- Enable the "Google Cloud Pub/Sub" API under "APIs & auth > APIs."
- Enable the "Google Cloud Datastore" API under "APIs & auth > APIs."
- For local development also follow the instructions below.
- Go to "Credentials" and create a new Service Account.
- Select "Generate new JSON key", then download a new JSON file.
- Set the following environment variable:
- `GOOGLE_APPLICATION_CREDENTIALS`: the file path to the downloaded JSON file.


## Configuration

- Edit `app.yaml`. Replace `your-google-project-id` with your google project id.
- Edit `app.yaml`. Replace `YOUR_PROJECT_ID` with your google project id.

- Copy `app.yaml` and `nginx-app.conf` into [../../../pubsub](../../../pubsub). Ex:
- Copy `app.yaml` and `nginx-app.conf` into [../../../pubsub/app](../../../pubsub/app). Ex:
```sh
~/gitrepos/php-docs-samples/appengine/flexible/pubsub$ cp -f app.yaml nginx-app.conf ../../../pubsub
~/gitrepos/php-docs-samples/appengine/flexible/pubsub$ cd ../../../pubsub/
~/gitrepos/php-docs-samples/pubsub$
~/gitrepos/php-docs-samples/appengine/flexible/pubsub$ cp -f app.yaml nginx-app.conf ../../../pubsub/app
~/gitrepos/php-docs-samples/appengine/flexible/pubsub$ cd ../../../pubsub/app
~/gitrepos/php-docs-samples/pubsub$
```

- Follow the [Prerequisite Instructions](../../../pubsub/app/README.md#prerequisites)

## Deploy the application to App Engine

```
$ gcloud app deploy app.yaml --set-default --project YOUR_PROJECT_NAME
$ gcloud app deploy --set-default --project YOUR_PROJECT_ID
```

Then access the following URL:
Expand Down
5 changes: 1 addition & 4 deletions appengine/flexible/pubsub/app.yaml
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
runtime: php
vm: true

runtime_config:
document_root: web

env_variables:
GOOGLE_PROJECT_NAME: your-google-project-id
GOOGLE_PROJECT_NAME: YOUR_PROJECT_ID
67 changes: 0 additions & 67 deletions pubsub/README.md

This file was deleted.

12 changes: 0 additions & 12 deletions pubsub/app.yaml

This file was deleted.

85 changes: 85 additions & 0 deletions pubsub/app/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
# Google PubSub PHP Sample Application

## Description

Note: The push endpoints don't work with the App Engine's local
devserver. The push notifications will go to an HTTP URL on the App
Engine server even when you run this sample locally. So we recommend
you deploy and run the app on App Engine.

## Register your application

- Go to
[Google Developers Console](https://console.developers.google.com/project)
and create a new project. This will automatically enable an App
Engine application with the same ID as the project.

- Enable the "Google Cloud Pub/Sub" API under "APIs & auth > APIs."

## Prerequisites

- Install [`composer`](https://getcomposer.org)
- Install the App Engine Python SDK.
We recommend you install
[Cloud SDK](https://developers.google.com/cloud/sdk/) rather than
just installing App Engine SDK.
- Create the topic "php-example-topic" and create a subscription to that topic
with the name "php-example-subscription".
- Use the [pubsub CLI](../cli) or the
[Developer Console](https://console.developer.google.com)
- To use Push Subscriptions, register your subscription with the
endpoint `https://{YOUR_PROJECT_NAME}.appspot.com/receive_message`
- Install dependencies by running:

```
$ composer install
```

## Local Development

- Go to "Credentials" and create a new Service Account.
- Select "Generate new JSON key", then download a new JSON file.
- Set the following environment variable:
- `GOOGLE_APPLICATION_CREDENTIALS`: the file path to the downloaded JSON file.
- `GOOGLE_PROJECT_ID`: your project ID.

Run the PHP build-in web server with the following command:

```
$ php -S localhost:8080
```

Now browse to [localhost:8080](http://localhost:8080) in your browser.

## Deploy to App Engine Standard

- Change `YOUR_PROJECT_ID` in `app.yaml` to your project ID.

Run the following gcloud command to deploy your app:

```
$ gcloud app deploy
```

Then access the following URL:
https://{YOUR_PROJECT_NAME}.appspot.com/

## Deploy to App Engine Flexible

See the instructions [here](../appengine/flexible/pubsub/README.md).

## Run using Dev Appserver

```
$ dev_appserver.py -A your-project-name .
```

## Contributing changes

* See [CONTRIBUTING.md](../../CONTRIBUTING.md)

## Licensing

* See [LICENSE](../../LICENSE)


103 changes: 103 additions & 0 deletions pubsub/app/app.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
<?php

/**
* Copyright 2015 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
namespace Google\Cloud\Samples\PubSub;

use Silex\Application;
use Silex\Provider\TwigServiceProvider;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
use Google\Cloud\ServiceBuilder;
use Memcached;

$app = new Application();
$app->register(new TwigServiceProvider());
$app['twig.path'] = [ __DIR__ ];

$app->get('/', function () use ($app) {
return $app['twig']->render('pubsub.html.twig', [
'project_id' => $app['project_id'],
]);
});

$app->get('/fetch_messages', function () use ($app) {
$messages = $app['get_pull_messages'](true);
$builder = new ServiceBuilder([
'projectId' => $app['project_id'],
]);
$pubsub = $builder->pubsub();
$subscription = $pubsub->subscription($app['subscription']);
$ackIds = [];
foreach ($subscription->pull(['returnImmediately' => true]) as $message) {
$ackIds[] = $message['ackId'];
$messageData = $message['message']['data'];
$messages[] = base64_decode($messageData);
}
if ($ackIds) {
$subscription->acknowledgeBatch($ackIds);
}
return new JsonResponse($messages);
});

$app->post('/receive_message', function () use ($app) {
// pull the message from the post body
$json = $app['request']->getContent();
$request = json_decode($json, true);
if (
!isset($request['message']['data'])
|| !$message = base64_decode($request['message']['data'])
) {
return new Response('', 400);
}
// store the push message in memcache
$app['save_pull_message']($message);
return new Response();
});

$app->post('/send_message', function () use ($app) {
// send the pubsub message
if ($message = $app['request']->get('message')) {
$builder = new ServiceBuilder([
'projectId' => $app['project_id'],
]);
$pubsub = $builder->pubsub();
$topic = $pubsub->topic($app['topic']);
$response = $topic->publish(['data' => $message]);
return new Response('', 204);
}
return new Response('', 400);
});

$app['get_pull_messages'] = $app->protect(function ($clearMessages = false) {
$memcache = new Memcached;
if ($pullMessages = $memcache->get('pull-messages')) {
if ($clearMessages) {
$memcache->set('pull-messages', []);
}
return $pullMessages;
}
return [];
});

$app['save_pull_message'] = $app->protect(function ($message) use ($app) {
$memcache = new Memcached;
$messages = $app['get_pull_messages']();
$messages[] = $message;
$memcache->set('pull-messages', $messages);
});

return $app;
12 changes: 12 additions & 0 deletions pubsub/app/app.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
runtime: php55
api_version: 1
threadsafe: true

handlers:
- url: pubsub.js
static_file: pubsub.js
- url: /.*
script: index.php

env_variables:
GOOGLE_PROJECT_ID: "cloud-samples-tests-php"
7 changes: 1 addition & 6 deletions pubsub/composer.json → pubsub/app/composer.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
{
"autoload": {
"psr-4": {
"Google\\Cloud\\Samples\\Pubsub\\": "src"
}
},
"require": {
"php": ">=5.4",
"google/cloud": "0.8",
"silex/silex": "~1.3",
"google/apiclient": "^2.0",
"symfony/twig-bridge": "~2.7|3.0.*",
"twig/twig": "~1.8|~2.0"
},
Expand Down
Loading