Skip to content

Commit df16171

Browse files
authored
adds pubsub CLI and updats pubsub app for veneer (GoogleCloudPlatform#163)
1 parent 76186e4 commit df16171

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+2979
-828
lines changed

appengine/flexible/pubsub/README.md

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,40 +5,28 @@
55
This sample demonstrates how to invoke PubSub from Google App Engine Flexible
66
Environment.
77

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

1111
## Register your application
1212

13-
- Go to
14-
[Google Developers Console](https://console.developers.google.com/project)
15-
and create a new project. This will automatically enable an App
16-
Engine application with the same ID as the project.
17-
18-
- Enable the "Google Cloud Pub/Sub" API under "APIs & auth > APIs."
19-
- Enable the "Google Cloud Datastore" API under "APIs & auth > APIs."
20-
- For local development also follow the instructions below.
21-
- Go to "Credentials" and create a new Service Account.
22-
- Select "Generate new JSON key", then download a new JSON file.
23-
- Set the following environment variable:
24-
- `GOOGLE_APPLICATION_CREDENTIALS`: the file path to the downloaded JSON file.
25-
26-
2713
## Configuration
2814

29-
- Edit `app.yaml`. Replace `your-google-project-id` with your google project id.
15+
- Edit `app.yaml`. Replace `YOUR_PROJECT_ID` with your google project id.
3016

31-
- Copy `app.yaml` and `nginx-app.conf` into [../../../pubsub](../../../pubsub). Ex:
17+
- Copy `app.yaml` and `nginx-app.conf` into [../../../pubsub/app](../../../pubsub/app). Ex:
3218
```sh
33-
~/gitrepos/php-docs-samples/appengine/flexible/pubsub$ cp -f app.yaml nginx-app.conf ../../../pubsub
34-
~/gitrepos/php-docs-samples/appengine/flexible/pubsub$ cd ../../../pubsub/
35-
~/gitrepos/php-docs-samples/pubsub$
19+
~/gitrepos/php-docs-samples/appengine/flexible/pubsub$ cp -f app.yaml nginx-app.conf ../../../pubsub/app
20+
~/gitrepos/php-docs-samples/appengine/flexible/pubsub$ cd ../../../pubsub/app
21+
~/gitrepos/php-docs-samples/pubsub$
3622
```
3723

24+
- Follow the [Prerequisite Instructions](../../../pubsub/app/README.md#prerequisites)
25+
3826
## Deploy the application to App Engine
3927

4028
```
41-
$ gcloud app deploy app.yaml --set-default --project YOUR_PROJECT_NAME
29+
$ gcloud app deploy --set-default --project YOUR_PROJECT_ID
4230
```
4331

4432
Then access the following URL:

appengine/flexible/pubsub/app.yaml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
runtime: php
22
vm: true
33

4-
runtime_config:
5-
document_root: web
6-
74
env_variables:
8-
GOOGLE_PROJECT_NAME: your-google-project-id
5+
GOOGLE_PROJECT_NAME: YOUR_PROJECT_ID

pubsub/README.md

Lines changed: 0 additions & 67 deletions
This file was deleted.

pubsub/app.yaml

Lines changed: 0 additions & 12 deletions
This file was deleted.

pubsub/app/README.md

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
# Google PubSub PHP Sample Application
2+
3+
## Description
4+
5+
Note: The push endpoints don't work with the App Engine's local
6+
devserver. The push notifications will go to an HTTP URL on the App
7+
Engine server even when you run this sample locally. So we recommend
8+
you deploy and run the app on App Engine.
9+
10+
## Register your application
11+
12+
- Go to
13+
[Google Developers Console](https://console.developers.google.com/project)
14+
and create a new project. This will automatically enable an App
15+
Engine application with the same ID as the project.
16+
17+
- Enable the "Google Cloud Pub/Sub" API under "APIs & auth > APIs."
18+
19+
## Prerequisites
20+
21+
- Install [`composer`](https://getcomposer.org)
22+
- Install the App Engine Python SDK.
23+
We recommend you install
24+
[Cloud SDK](https://developers.google.com/cloud/sdk/) rather than
25+
just installing App Engine SDK.
26+
- Create the topic "php-example-topic" and create a subscription to that topic
27+
with the name "php-example-subscription".
28+
- Use the [pubsub CLI](../cli) or the
29+
[Developer Console](https://console.developer.google.com)
30+
- To use Push Subscriptions, register your subscription with the
31+
endpoint `https://{YOUR_PROJECT_NAME}.appspot.com/receive_message`
32+
- Install dependencies by running:
33+
34+
```
35+
$ composer install
36+
```
37+
38+
## Local Development
39+
40+
- Go to "Credentials" and create a new Service Account.
41+
- Select "Generate new JSON key", then download a new JSON file.
42+
- Set the following environment variable:
43+
- `GOOGLE_APPLICATION_CREDENTIALS`: the file path to the downloaded JSON file.
44+
- `GOOGLE_PROJECT_ID`: your project ID.
45+
46+
Run the PHP build-in web server with the following command:
47+
48+
```
49+
$ php -S localhost:8080
50+
```
51+
52+
Now browse to [localhost:8080](http://localhost:8080) in your browser.
53+
54+
## Deploy to App Engine Standard
55+
56+
- Change `YOUR_PROJECT_ID` in `app.yaml` to your project ID.
57+
58+
Run the following gcloud command to deploy your app:
59+
60+
```
61+
$ gcloud app deploy
62+
```
63+
64+
Then access the following URL:
65+
https://{YOUR_PROJECT_NAME}.appspot.com/
66+
67+
## Deploy to App Engine Flexible
68+
69+
See the instructions [here](../appengine/flexible/pubsub/README.md).
70+
71+
## Run using Dev Appserver
72+
73+
```
74+
$ dev_appserver.py -A your-project-name .
75+
```
76+
77+
## Contributing changes
78+
79+
* See [CONTRIBUTING.md](../../CONTRIBUTING.md)
80+
81+
## Licensing
82+
83+
* See [LICENSE](../../LICENSE)
84+
85+

pubsub/app/app.php

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
2+
3+
/**
4+
* Copyright 2015 Google Inc. All Rights Reserved.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
namespace Google\Cloud\Samples\PubSub;
19+
20+
use Silex\Application;
21+
use Silex\Provider\TwigServiceProvider;
22+
use Symfony\Component\HttpFoundation\JsonResponse;
23+
use Symfony\Component\HttpFoundation\Response;
24+
use Google\Cloud\ServiceBuilder;
25+
use Memcached;
26+
27+
$app = new Application();
28+
$app->register(new TwigServiceProvider());
29+
$app['twig.path'] = [ __DIR__ ];
30+
31+
$app->get('/', function () use ($app) {
32+
return $app['twig']->render('pubsub.html.twig', [
33+
'project_id' => $app['project_id'],
34+
]);
35+
});
36+
37+
$app->get('/fetch_messages', function () use ($app) {
38+
$messages = $app['get_pull_messages'](true);
39+
$builder = new ServiceBuilder([
40+
'projectId' => $app['project_id'],
41+
]);
42+
$pubsub = $builder->pubsub();
43+
$subscription = $pubsub->subscription($app['subscription']);
44+
$ackIds = [];
45+
foreach ($subscription->pull(['returnImmediately' => true]) as $message) {
46+
$ackIds[] = $message['ackId'];
47+
$messageData = $message['message']['data'];
48+
$messages[] = base64_decode($messageData);
49+
}
50+
if ($ackIds) {
51+
$subscription->acknowledgeBatch($ackIds);
52+
}
53+
return new JsonResponse($messages);
54+
});
55+
56+
$app->post('/receive_message', function () use ($app) {
57+
// pull the message from the post body
58+
$json = $app['request']->getContent();
59+
$request = json_decode($json, true);
60+
if (
61+
!isset($request['message']['data'])
62+
|| !$message = base64_decode($request['message']['data'])
63+
) {
64+
return new Response('', 400);
65+
}
66+
// store the push message in memcache
67+
$app['save_pull_message']($message);
68+
return new Response();
69+
});
70+
71+
$app->post('/send_message', function () use ($app) {
72+
// send the pubsub message
73+
if ($message = $app['request']->get('message')) {
74+
$builder = new ServiceBuilder([
75+
'projectId' => $app['project_id'],
76+
]);
77+
$pubsub = $builder->pubsub();
78+
$topic = $pubsub->topic($app['topic']);
79+
$response = $topic->publish(['data' => $message]);
80+
return new Response('', 204);
81+
}
82+
return new Response('', 400);
83+
});
84+
85+
$app['get_pull_messages'] = $app->protect(function ($clearMessages = false) {
86+
$memcache = new Memcached;
87+
if ($pullMessages = $memcache->get('pull-messages')) {
88+
if ($clearMessages) {
89+
$memcache->set('pull-messages', []);
90+
}
91+
return $pullMessages;
92+
}
93+
return [];
94+
});
95+
96+
$app['save_pull_message'] = $app->protect(function ($message) use ($app) {
97+
$memcache = new Memcached;
98+
$messages = $app['get_pull_messages']();
99+
$messages[] = $message;
100+
$memcache->set('pull-messages', $messages);
101+
});
102+
103+
return $app;

pubsub/app/app.yaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
runtime: php55
2+
api_version: 1
3+
threadsafe: true
4+
5+
handlers:
6+
- url: pubsub.js
7+
static_file: pubsub.js
8+
- url: /.*
9+
script: index.php
10+
11+
env_variables:
12+
GOOGLE_PROJECT_ID: "cloud-samples-tests-php"

pubsub/composer.json renamed to pubsub/app/composer.json

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,8 @@
11
{
2-
"autoload": {
3-
"psr-4": {
4-
"Google\\Cloud\\Samples\\Pubsub\\": "src"
5-
}
6-
},
72
"require": {
83
"php": ">=5.4",
4+
"google/cloud": "0.8",
95
"silex/silex": "~1.3",
10-
"google/apiclient": "^2.0",
116
"symfony/twig-bridge": "~2.7|3.0.*",
127
"twig/twig": "~1.8|~2.0"
138
},

0 commit comments

Comments
 (0)