-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Migrate Sample: Cloud Run as Pub/Sub Push Handler #1419
Merged
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
91012e2
run/pubsub: add sample
grayside 0f03469
run/pubsub: READMEs and lockfile
grayside f5965b0
Merge branch 'master' into run/pubsub
grayside 07f201f
run/pubsub: reduce region tag whitespace
grayside d3bab35
Merge branch 'master' into run/pubsub
fhinkel 7dfb9f0
Merge branch 'master' into run/pubsub
fhinkel 9c6642d
Merge branch 'master' into run/pubsub
fhinkel 4107fdb
run/pubsub: s/nodejs-repo-tools/sinon + supertest
grayside 357fa46
Merge branch 'master' into run/pubsub
grayside 6240706
Merge branch 'master' into run/pubsub
fhinkel 9c7c959
run: standardize npm install code and remove package-lock.json
grayside ca2d987
run/pubsub: back to only production dependencies via npm install
grayside 6cf4b27
Merge branch 'master' into run/pubsub
grayside 4f82035
run: suppress (build-with-run.sh) cleanup errors
grayside f269fbb
Merge branch 'master' into run/pubsub
fhinkel 6d6e09d
run/kokoro: remove explicit exit
grayside 457933e
run/kokoro: use npm run --if-present flag
grayside b6bb05e
Merge branch 'master' into run/pubsub
fhinkel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# Format: //devtools/kokoro/config/proto/build.proto | ||
|
||
# Set the folder in which the tests are run | ||
env_vars: { | ||
key: "PROJECT" | ||
value: "run/pubsub" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
Dockerfile | ||
.dockerignore | ||
node_modules | ||
npm-debug.log |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
.gcloudignore | ||
node_modules | ||
npm-debug.log |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
node_modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# Copyright 2019 Google LLC. All rights reserved. | ||
# Use of this source code is governed by the Apache 2.0 | ||
# license that can be found in the LICENSE file. | ||
|
||
# [START run_pubsub_dockerfile] | ||
|
||
# Use the official Node.js 10 image. | ||
# https://hub.docker.com/_/node | ||
FROM node:10 | ||
|
||
# Create and change to the app directory. | ||
WORKDIR /usr/src/app | ||
|
||
# Copy application dependency manifests to the container image. | ||
# A wildcard is used to ensure both package.json AND package-lock.json are copied. | ||
# Copying this separately prevents re-running npm install on every code change. | ||
COPY package*.json ./ | ||
|
||
# Install dependencies. | ||
RUN npm install --production | ||
# If you add a package-lock.json, speed your build by switching to 'npm ci'. | ||
# RUN npm ci --only=production | ||
|
||
# Copy local code to the container image. | ||
COPY . . | ||
|
||
# Run the web service on container startup. | ||
CMD [ "npm", "start" ] | ||
|
||
# [END run_pubsub_dockerfile] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# Cloud Run Pub/Sub Tutorial Sample | ||
|
||
This sample shows how to create a service that processes Pub/Sub messages. | ||
|
||
Use it with the [Cloud Pub/Sub with Cloud Run tutorial](http://cloud.google.com/run/docs/tutorials/pubsub). | ||
|
||
For more details on how to work with this sample read the [Google Cloud Run Node.js Samples README](https://github.com/GoogleCloudPlatform/nodejs-docs-samples/run). | ||
|
||
## Dependencies | ||
|
||
* **express**: Web server framework. | ||
* **body-parser**: express middleware for request payload processing. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// Copyright 2019 Google LLC. All rights reserved. | ||
// Use of this source code is governed by the Apache 2.0 | ||
// license that can be found in the LICENSE file. | ||
|
||
// [START run_pubsub_server_setup] | ||
const express = require('express'); | ||
const bodyParser = require('body-parser'); | ||
const app = express(); | ||
|
||
app.use(bodyParser.json()); | ||
// [END run_pubsub_server_setup] | ||
|
||
// [START run_pubsub_handler] | ||
app.post('/', (req, res) => { | ||
if (!req.body) { | ||
const msg = 'no Pub/Sub message received'; | ||
console.error(`error: ${msg}`); | ||
res.status(400).send(`Bad Request: ${msg}`); | ||
return; | ||
} | ||
if (!req.body.message) { | ||
const msg = 'invalid Pub/Sub message format'; | ||
console.error(`error: ${msg}`); | ||
res.status(400).send(`Bad Request: ${msg}`); | ||
return; | ||
} | ||
|
||
const pubSubMessage = req.body.message; | ||
const name = pubSubMessage.data | ||
? Buffer.from(pubSubMessage.data, 'base64') | ||
.toString() | ||
.trim() | ||
: 'World'; | ||
|
||
console.log(`Hello ${name}!`); | ||
res.status(204).send(); | ||
}); | ||
// [END run_pubsub_handler] | ||
|
||
module.exports = app; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// Copyright 2019 Google LLC. All rights reserved. | ||
// Use of this source code is governed by the Apache 2.0 | ||
// license that can be found in the LICENSE file. | ||
|
||
// [START run_pubsub_server] | ||
const app = require('./app.js'); | ||
const PORT = process.env.PORT || 8080; | ||
|
||
app.listen(PORT, () => | ||
console.log(`nodejs-pubsub-tutorial listening on port ${PORT}`) | ||
); | ||
// [END run_pubsub_server] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
{ | ||
"name": "nodejs-pubsub", | ||
"version": "1.0.0", | ||
"private": true, | ||
"description": "Simple Pub/Sub subscriber service sample", | ||
"main": "index.js", | ||
"author": "Google LLC", | ||
"license": "Apache-2.0", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git" | ||
}, | ||
"engines": { | ||
"node": ">= 8.0.0" | ||
}, | ||
"scripts": { | ||
"start": "node index.js", | ||
"test": "mocha test/*.test.js --check-leaks" | ||
}, | ||
"dependencies": { | ||
"body-parser": "^1.19.0", | ||
"express": "^4.16.4" | ||
}, | ||
"devDependencies": { | ||
"mocha": "^6.1.4", | ||
"sinon": "^7.3.2", | ||
"supertest": "^4.0.2", | ||
"uuid": "^3.3.2" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
// Copyright 2019, Google LLC. | ||
// 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. | ||
|
||
// NOTE: | ||
// This app can only be fully tested when deployed, because | ||
// Pub/Sub requires a live endpoint URL to hit. Nevertheless, | ||
// these tests mock it and partially test it locally. | ||
|
||
'use strict'; | ||
|
||
const assert = require('assert'); | ||
const path = require('path'); | ||
const supertest = require('supertest'); | ||
const sinon = require('sinon'); | ||
const uuid = require('uuid'); | ||
|
||
let request; | ||
|
||
describe('Unit Tests', () => { | ||
before(() => { | ||
const app = require(path.join(__dirname, '..', 'app')); | ||
request = supertest(app); | ||
}); | ||
|
||
describe('should fail', () => { | ||
it(`on a Bad Request with an empty payload`, async () => { | ||
await request | ||
.post('/') | ||
.type('json') | ||
.send('') | ||
.expect(400); | ||
}); | ||
|
||
it(`on a Bad Request with an invalid payload`, async () => { | ||
await request | ||
.post('/') | ||
.type('json') | ||
.send({nomessage: 'invalid'}) | ||
.expect(400); | ||
}); | ||
|
||
it(`on a Bad Request with an invalid mimetype`, async () => { | ||
await request | ||
.post('/') | ||
.type('text') | ||
.send('{message: true}') | ||
.expect(400); | ||
}); | ||
}); | ||
|
||
describe('should succeed', () => { | ||
beforeEach(() => { | ||
sinon.spy(console, 'error'); | ||
sinon.spy(console, 'log'); | ||
}); | ||
afterEach(() => { | ||
console.error.restore(); | ||
console.log.restore(); | ||
}); | ||
|
||
it(`with a minimally valid Pub/Sub Message`, async () => { | ||
await request | ||
.post('/') | ||
.type('json') | ||
.send({message: true}) | ||
.expect(204) | ||
.expect(() => assert.ok(console.log.calledWith('Hello World!'))); | ||
}); | ||
|
||
it(`with a populated Pub/Sub Message`, async () => { | ||
const name = uuid.v4(); | ||
const data = Buffer.from(name).toString(`base64`); | ||
|
||
await request | ||
.post('/') | ||
.type('json') | ||
.send({message: {data}}) | ||
.expect(204) | ||
.expect(() => assert.ok(console.log.calledWith(`Hello ${name}!`))); | ||
}); | ||
}); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: in an ideal world, lines 19-20 wouldn't be repeated in the clause below (lines 26-27) - but I don't think there's a good way to avoid this repetition here. 🙁