Skip to content

Commit 42d8151

Browse files
authored
feat: updates backend processing to use google/cloud-functions-framework (#232)
1 parent 29081d5 commit 42d8151

5 files changed

Lines changed: 78 additions & 98 deletions

File tree

background-processing/backend/Dockerfile

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,27 @@ FROM gcr.io/gae-runtimes/php73:php73_20190827_7_3_7_RC03
22

33
WORKDIR /srv/
44

5-
# Copy over all application files
6-
COPY . .
5+
# NOTE: The entrypoint "/start", which starts up NGINX and PHP-FPM,
6+
# is configured by creating a `.googleconfig/app_start.json` file with the
7+
# contents:
8+
#
9+
# {"entrypointContents": "CUSTOM_ENTRYPOINT"}
10+
#
11+
# We configure it to use the `router.php` file included in this package.
12+
RUN mkdir .googleconfig && \
13+
echo '{"entrypointContents": "serve vendor/bin/router.php"}' > .googleconfig/app_start.json
714

8-
# Run "composer install"
15+
# Copy over composer files and run "composer install"
16+
COPY composer.* php.ini ./
917
COPY --from=composer:1 /usr/bin/composer /usr/local/bin
1018
RUN composer install --no-dev
1119

12-
# Set a runtime name and the function to use
13-
ENV GAE_RUNTIME php73
14-
ENV FUNCTION_TARGET translateString
20+
# Copy over all application files
21+
COPY . .
1522

16-
# Start NGINX and PHP-FPM
17-
# NOTE: You can configure the container to serve a different endpoint by
18-
# creating a `.googleconfig/app_start.json` file with the contents
19-
# {"entrypointContents": "some entrypoint"}
20-
ENTRYPOINT ["/start"]
23+
# Set a runtime name
24+
ENV GAE_RUNTIME=php73 \
25+
# Set the function to use
26+
FUNCTION_TARGET=translateString \
27+
# This function will respond to Pub/Sub events
28+
FUNCTION_SIGNATURE_TYPE=event

background-processing/backend/composer.json

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,6 @@
22
"require": {
33
"google/cloud-translate": "^1.4",
44
"google/cloud-firestore": "^1.7",
5-
"symfony/http-foundation": "^5.0.0"
6-
},
7-
"autoload": {
8-
"files": [
9-
"functions.php"
10-
]
5+
"google/cloud-functions-framework": "^0.4.0"
116
}
127
}

background-processing/backend/functions.php

Lines changed: 0 additions & 63 deletions
This file was deleted.
Lines changed: 57 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,63 @@
11
<?php
22

3-
require __DIR__ . '/vendor/autoload.php';
3+
# [START getting_started_background_translate]
4+
# [START getting_started_background_translate_setup]
5+
use Google\Cloud\Firestore\FirestoreClient;
6+
use Google\Cloud\Firestore\Transaction;
7+
use Google\Cloud\Translate\TranslateClient;
48

5-
use Symfony\Component\HttpFoundation\Request;
9+
# [END getting_started_background_translate_setup]
610

7-
(function () {
8-
$target = getenv('FUNCTION_TARGET', true);
9-
if ($target === false) {
10-
throw new \Exception('FUNCTION_TARGET is not set');
11+
/**
12+
* @param array $data {
13+
* The PubSub message data containing text and target language.
14+
*
15+
* @type string $text
16+
* The full text to translate.
17+
* @type string $language
18+
* The target language for the translation.
19+
* }
20+
*/
21+
function translateString(array $data)
22+
{
23+
if (empty($data['language']) || empty($data['text'])) {
24+
throw new Exception('Error parsing translation data');
1125
}
1226

13-
$request = Request::createFromGlobals();
14-
$message = json_decode($request->getContent(), true);
15-
if (empty($message['message']['data'])) {
16-
throw new \Exception('No message received');
17-
}
18-
$data = json_decode(base64_decode($message['message']['data']), true);
19-
if (!$data) {
20-
throw new \Exception('Error decoding data from message');
21-
}
22-
call_user_func_array($target, [$data]);
23-
})();
27+
# [START getting_started_background_translate_init]
28+
$firestore = new FirestoreClient();
29+
$translate = new TranslateClient();
30+
31+
$translation = [
32+
'original' => $data['text'],
33+
'lang' => $data['language'],
34+
];
35+
# [END getting_started_background_translate_init]
36+
37+
# [START getting_started_background_translate_transaction]
38+
$docId = sprintf('%s:%s', $data['language'], base64_encode($data['text']));
39+
$docRef = $firestore->collection('translations')->document($docId);
40+
41+
$firestore->runTransaction(
42+
function (Transaction $transaction) use ($translate, $translation, $docRef) {
43+
$snapshot = $transaction->snapshot($docRef);
44+
if ($snapshot->exists()) {
45+
return; // Do nothing if the document already exists
46+
}
47+
48+
# [START getting_started_background_translate_string]
49+
$result = $translate->translate($translation['original'], [
50+
'target' => $translation['lang'],
51+
]);
52+
# [END getting_started_background_translate_string]
53+
$transaction->set($docRef, $translation + [
54+
'translated' => $result['text'],
55+
'originalLang' => $result['source'],
56+
]);
57+
}
58+
);
59+
# [END getting_started_background_translate_transaction]
60+
61+
echo "Done.";
62+
}
63+
# [END getting_started_background_translate]

background-processing/test/backend/translateTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
use Google\Cloud\Firestore\FirestoreClient;
1919
use PHPUnit\Framework\TestCase;
2020

21-
require_once __DIR__ . '/../../backend/functions.php';
21+
require_once __DIR__ . '/../../backend/index.php';
2222

2323
class translateTest extends TestCase
2424
{

0 commit comments

Comments
 (0)