diff --git a/.kokoro/functions/speech-to-speech.cfg b/.kokoro/functions/speech-to-speech.cfg index df598f3850..fc5a1498c6 100644 --- a/.kokoro/functions/speech-to-speech.cfg +++ b/.kokoro/functions/speech-to-speech.cfg @@ -3,7 +3,7 @@ # Set the folder in which the tests are run env_vars: { key: "PROJECT" - value: "functions/speech-to-speech" + value: "functions/speech-to-speech/functions/" } # Tell the trampoline which build file to use. diff --git a/functions/speech-to-speech/.firebaserc b/functions/speech-to-speech/.firebaserc new file mode 100644 index 0000000000..a4378ccafc --- /dev/null +++ b/functions/speech-to-speech/.firebaserc @@ -0,0 +1,5 @@ +{ + "projects": { + "default": "" + } +} diff --git a/functions/speech-to-speech/.gitignore b/functions/speech-to-speech/.gitignore new file mode 100644 index 0000000000..f62685251b --- /dev/null +++ b/functions/speech-to-speech/.gitignore @@ -0,0 +1,65 @@ +# Logs +logs +*.log +npm-debug.log* +yarn-debug.log* +yarn-error.log* +firebase-debug.log* + +# Firebase cache +.firebase/ + +# Firebase config + +# Uncomment this if you'd like others to create their own Firebase project. +# For a team working on the same Firebase project(s), it is recommended to leave +# it commented so all members can deploy to the same project(s) in .firebaserc. +# .firebaserc + +# Runtime data +pids +*.pid +*.seed +*.pid.lock + +# Directory for instrumented libs generated by jscoverage/JSCover +lib-cov + +# Coverage directory used by tools like istanbul +coverage + +# nyc test coverage +.nyc_output + +# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) +.grunt + +# Bower dependency directory (https://bower.io/) +bower_components + +# node-waf configuration +.lock-wscript + +# Compiled binary addons (http://nodejs.org/api/addons.html) +build/Release + +# Dependency directories +node_modules/ + +# Optional npm cache directory +.npm + +# Optional eslint cache +.eslintcache + +# Optional REPL history +.node_repl_history + +# Output of 'npm pack' +*.tgz + +# Yarn Integrity file +.yarn-integrity + +# dotenv environment variables file +.env diff --git a/functions/speech-to-speech/firebase.json b/functions/speech-to-speech/firebase.json new file mode 100644 index 0000000000..0967ef424b --- /dev/null +++ b/functions/speech-to-speech/firebase.json @@ -0,0 +1 @@ +{} diff --git a/functions/speech-to-speech/index.js b/functions/speech-to-speech/functions/index.js similarity index 91% rename from functions/speech-to-speech/index.js rename to functions/speech-to-speech/functions/index.js index 0ad2cbf09d..086ea7a0ec 100644 --- a/functions/speech-to-speech/index.js +++ b/functions/speech-to-speech/functions/index.js @@ -17,10 +17,19 @@ // This sample uses the UUID library to generate the output filename. const uuid = require('uuid/v4'); +const functions = require('firebase-functions'); const googleCloudProject = process.env.GOOGLE_CLOUD_PROJECT; -const supportedLanguageCodes = process.env.SUPPORTED_LANGUAGE_CODES.split(','); -const outputBucket = process.env.OUTPUT_BUCKET; +// The supportedLanguageCodes and outputBucket parameters take the value from +// environment variables by default. +const firebaseConfigured = typeof functions.config().playchat !== 'undefined'; +const languageCodesParam = firebaseConfigured + ? functions.config().playchat.supported_language_codes + : process.env.SUPPORTED_LANGUAGE_CODES; +const supportedLanguageCodes = languageCodesParam.split(','); +const outputBucket = firebaseConfigured + ? functions.config().playchat.output_bucket + : process.env.OUTPUT_BUCKET; const outputAudioEncoding = 'MP3'; const voiceSsmlGender = 'NEUTRAL'; // Declare the API clients as global variables to allow them to initiaze at cold start. @@ -29,7 +38,7 @@ const textTranslationClient = getTextTranslationClient(); const textToSpeechClient = getTextToSpeechClient(); const storageClient = getStorageClient(); -exports.speechTranslate = (request, response) => { +exports.speechTranslate = functions.https.onRequest((request, response) => { let responseBody = {}; validateRequest(request) @@ -105,7 +114,7 @@ exports.speechTranslate = (request, response) => { console.error(error); response.status(400).send(error.message); }); -}; +}); // [START call_speech_to_text] function callSpeechToText( diff --git a/functions/speech-to-speech/package.json b/functions/speech-to-speech/functions/package.json similarity index 77% rename from functions/speech-to-speech/package.json rename to functions/speech-to-speech/functions/package.json index c0070aed6b..8c882bfab2 100644 --- a/functions/speech-to-speech/package.json +++ b/functions/speech-to-speech/functions/package.json @@ -5,13 +5,12 @@ "homepage": "https://cloud.google.com/solutions/mobile/", "license": "Apache-2.0", "author": "Google LLC", + "private": true, + "node": "6", "repository": { "type": "git", "url": "https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git" }, - "engines": { - "npm": ">= 4.0.0" - }, "files": [ "index.js" ], @@ -27,14 +26,21 @@ "local-test": "mocha test/index.test.js", "system-test": "mocha --timeout 20000 test/sample.integration.http.test.js", "pretest": "sh test/updateFunctions.sh", - "test": "npm run local-test && npm run system-test" + "test": "npm run local-test && npm run system-test", + "serve": "firebase serve --only functions", + "shell": "firebase functions:shell", + "start": "npm run shell", + "deploy": "firebase deploy --only functions", + "logs": "firebase functions:log" }, "dependencies": { "@google-cloud/speech": "^2.1.0", "@google-cloud/storage": "^2.0.3", "@google-cloud/text-to-speech": "^0.4.0", "@google-cloud/translate": "^2.1.2", - "uuid": "^3.3.2" + "uuid": "^3.3.2", + "firebase-admin": "~6.0.0", + "firebase-functions": "^2.1.0" }, "devDependencies": { "@google-cloud/functions-emulator": "^1.0.0-beta.5", diff --git a/functions/speech-to-speech/test/index.test.js b/functions/speech-to-speech/functions/test/index.test.js similarity index 100% rename from functions/speech-to-speech/test/index.test.js rename to functions/speech-to-speech/functions/test/index.test.js diff --git a/functions/speech-to-speech/test/request-body.json b/functions/speech-to-speech/functions/test/request-body.json similarity index 100% rename from functions/speech-to-speech/test/request-body.json rename to functions/speech-to-speech/functions/test/request-body.json diff --git a/functions/speech-to-speech/test/sample.integration.http.test.js b/functions/speech-to-speech/functions/test/sample.integration.http.test.js similarity index 100% rename from functions/speech-to-speech/test/sample.integration.http.test.js rename to functions/speech-to-speech/functions/test/sample.integration.http.test.js diff --git a/functions/speech-to-speech/test/speech-recording.b64 b/functions/speech-to-speech/functions/test/speech-recording.b64 similarity index 100% rename from functions/speech-to-speech/test/speech-recording.b64 rename to functions/speech-to-speech/functions/test/speech-recording.b64 diff --git a/functions/speech-to-speech/test/speech-recording.wav b/functions/speech-to-speech/functions/test/speech-recording.wav similarity index 100% rename from functions/speech-to-speech/test/speech-recording.wav rename to functions/speech-to-speech/functions/test/speech-recording.wav diff --git a/functions/speech-to-speech/test/updateFunctions.sh b/functions/speech-to-speech/functions/test/updateFunctions.sh similarity index 100% rename from functions/speech-to-speech/test/updateFunctions.sh rename to functions/speech-to-speech/functions/test/updateFunctions.sh