Skip to content

Commit

Permalink
Eventarc Quickstart Brushup (#1986)
Browse files Browse the repository at this point in the history
* feat: add event type conversion to run pubsub event sample

Signed-off-by: Grant Timmerman <[email protected]>

* feat: re-add invalid pub/sub error

Signed-off-by: Grant Timmerman <[email protected]>

* fix: invalid pub/sub case

Signed-off-by: Grant Timmerman <[email protected]>

* style: polish eventarc samples

Signed-off-by: Grant Timmerman <[email protected]>
  • Loading branch information
grant authored Sep 28, 2020
1 parent 2093e3e commit a3e0e4a
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 16 deletions.
2 changes: 1 addition & 1 deletion eventarc/audit-storage/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# 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
# 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,
Expand Down
2 changes: 1 addition & 1 deletion eventarc/audit-storage/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ const app = require('./app.js');
const PORT = process.env.PORT || 8080;

app.listen(PORT, () =>
console.log(`nodejs-events-storage listening on port ${PORT}`)
console.log(`nodejs-events-storage listening on port ${PORT}`)
);
// [END eventarc_gcs_server]
16 changes: 13 additions & 3 deletions eventarc/pubsub/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
# Copyright 2020 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.
# Copyright 2020 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.

# [START eventarc_pubsub_dockerfile]

Expand Down
26 changes: 18 additions & 8 deletions eventarc/pubsub/app.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
// Copyright 2020 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.
// Copyright 2020 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.

// [START eventarc_pubsub_handler]
// [START run_events_pubsub_handler]
const express = require('express');
const {toMessagePublishedEvent} = require('@google/events/cloud/pubsub/v1/MessagePublishedData');
const app = express();

app.use(express.json());
Expand All @@ -21,9 +31,10 @@ app.post('/', (req, res) => {
console.log(`Bad Request: ${errorMessage}`);
return;
}
const pubSubMessage = req.body.message;
const name = pubSubMessage.data
? Buffer.from(pubSubMessage.data, 'base64').toString().trim()
// Cast to MessagePublishedEvent for IDE autocompletion
const pubSubMessage = toMessagePublishedEvent(req.body);
const name = pubSubMessage.message && pubSubMessage.message.data
? Buffer.from(pubSubMessage.message.data, 'base64').toString().trim()
: 'World';

const result = `Hello, ${name}! ID: ${req.get('ce-id') || ''}`;
Expand All @@ -32,5 +43,4 @@ app.post('/', (req, res) => {
});

module.exports = app;
// [END run_events_pubsub_handler]
// [END eventarc_pubsub_handler]
16 changes: 13 additions & 3 deletions eventarc/pubsub/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
// Copyright 2020 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.
// Copyright 2020 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.

// [START eventarc_pubsub_server]
const app = require('./app.js');
Expand Down
1 change: 1 addition & 0 deletions eventarc/pubsub/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"system-test": "test/runner.sh mocha test/system.test.js --timeout=10000"
},
"dependencies": {
"@google/events": "^1.4.2",
"express": "^4.16.4"
},
"devDependencies": {
Expand Down

0 comments on commit a3e0e4a

Please sign in to comment.