Thanks to visit codestin.com
Credit goes to github.com

Skip to content

doc: examples use declarative configuration 5/N #371

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

Merged
merged 3 commits into from
Jul 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 0 additions & 8 deletions ci/build-examples.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,6 @@ steps:
waitFor: ['gcf-builder-ready']
id: 'site-bearer_token'
args: ['build',
'--env', 'GOOGLE_FUNCTION_SIGNATURE_TYPE=http',
'--env', 'GOOGLE_FUNCTION_TARGET=bearer_token',
'--path', 'examples/site/bearer_token',
'site-bearer_token',
Expand Down Expand Up @@ -306,7 +305,6 @@ steps:
waitFor: ['gcf-builder-ready']
id: 'site-log_stackdriver'
args: ['build',
'--env', 'GOOGLE_FUNCTION_SIGNATURE_TYPE=cloudevent',
'--env', 'GOOGLE_FUNCTION_TARGET=log_stackdriver',
'--path', 'examples/site/log_stackdriver',
'site-log_stackdriver',
Expand All @@ -315,7 +313,6 @@ steps:
waitFor: ['gcf-builder-ready']
id: 'site-pubsub_subscribe'
args: ['build',
'--env', 'GOOGLE_FUNCTION_SIGNATURE_TYPE=cloudevent',
'--env', 'GOOGLE_FUNCTION_TARGET=pubsub_subscribe',
'--path', 'examples/site/pubsub_subscribe',
'site-pubsub_subscribe',
Expand All @@ -324,7 +321,6 @@ steps:
waitFor: ['gcf-builder-ready']
id: 'site-tips_gcp_apis'
args: ['build',
'--env', 'GOOGLE_FUNCTION_SIGNATURE_TYPE=http',
'--env', 'GOOGLE_FUNCTION_TARGET=tips_gcp_apis',
'--path', 'examples/site/tips_gcp_apis',
'site-tips_gcp_apis',
Expand All @@ -333,7 +329,6 @@ steps:
waitFor: ['gcf-builder-ready']
id: 'site-tips_infinite_retries'
args: ['build',
'--env', 'GOOGLE_FUNCTION_SIGNATURE_TYPE=cloudevent',
'--env', 'GOOGLE_FUNCTION_TARGET=tips_infinite_retries',
'--path', 'examples/site/tips_infinite_retries',
'site-tips_infinite_retries',
Expand All @@ -342,7 +337,6 @@ steps:
waitFor: ['gcf-builder-ready']
id: 'site-tips_lazy_globals'
args: ['build',
'--env', 'GOOGLE_FUNCTION_SIGNATURE_TYPE=http',
'--env', 'GOOGLE_FUNCTION_TARGET=tips_lazy_globals',
'--path', 'examples/site/tips_lazy_globals',
'site-tips_lazy_globals',
Expand All @@ -351,7 +345,6 @@ steps:
waitFor: ['gcf-builder-ready']
id: 'site-tips_retry'
args: ['build',
'--env', 'GOOGLE_FUNCTION_SIGNATURE_TYPE=cloudevent',
'--env', 'GOOGLE_FUNCTION_TARGET=tips_retry',
'--path', 'examples/site/tips_retry',
'site-tips_retry',
Expand All @@ -360,7 +353,6 @@ steps:
waitFor: ['gcf-builder-ready']
id: 'site-tips_scopes'
args: ['build',
'--env', 'GOOGLE_FUNCTION_SIGNATURE_TYPE=http',
'--env', 'GOOGLE_FUNCTION_TARGET=tips_scopes',
'--path', 'examples/site/tips_scopes',
'site-tips_scopes',
Expand Down
7 changes: 4 additions & 3 deletions examples/site/bearer_token/bearer_token.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@
// limitations under the License.

// [START functions_bearer_token]
#include <google/cloud/functions/http_request.h>
#include <google/cloud/functions/http_response.h>
#include <google/cloud/functions/function.h>
#include <google/cloud/storage/oauth2/google_credentials.h>
#include <curl/curl.h>
#include <cstdlib>
Expand All @@ -31,7 +30,7 @@ gcf::HttpResponse HttpGet(std::string const& url,
std::string const& authorization_header);
} // namespace

gcf::HttpResponse bearer_token(gcf::HttpRequest request) { // NOLINT
gcf::HttpResponse bearer_token_impl(gcf::HttpRequest const& request) {
static auto const kTargetUrl = [] {
auto const* target_url = std::getenv("TARGET_URL");
if (target_url != nullptr) return std::string(target_url);
Expand Down Expand Up @@ -61,6 +60,8 @@ gcf::HttpResponse bearer_token(gcf::HttpRequest request) { // NOLINT
os << "Error creating authorization header: " << std::move(header).status();
throw std::runtime_error(std::move(os).str());
}

gcf::Function bearer_token() { return gcf::MakeFunction(bearer_token_impl); }
// [END functions_bearer_token]

namespace {
Expand Down
24 changes: 13 additions & 11 deletions examples/site/log_stackdriver/log_stackdriver.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,24 @@
// limitations under the License.

// [START functions_log_stackdriver]
#include <google/cloud/functions/cloud_event.h>
#include <google/cloud/functions/function.h>
#include <cppcodec/base64_rfc4648.hpp>
#include <nlohmann/json.hpp>
#include <iostream>

namespace gcf = ::google::cloud::functions;

void log_stackdriver(gcf::CloudEvent event) { // NOLINT
if (event.data_content_type().value_or("") != "application/json") {
std::cerr << "expected application/json data";
return;
}
auto const payload = nlohmann::json::parse(event.data().value_or("{}"));
auto const data = cppcodec::base64_rfc4648::decode<std::string>(
payload["message"]["data"].get<std::string>());
auto const log_entry = nlohmann::json::parse(data);
std::cout << "Log entry data: " << log_entry.dump(/*indent=*/2) << "\n";
gcf::Function log_stackdriver() {
return gcf::MakeFunction([](gcf::CloudEvent const& event) {
if (event.data_content_type().value_or("") != "application/json") {
std::cerr << "expected application/json data\n";
return;
}
auto const payload = nlohmann::json::parse(event.data().value_or("{}"));
auto const data = cppcodec::base64_rfc4648::decode<std::string>(
payload["message"]["data"].get<std::string>());
auto const log_entry = nlohmann::json::parse(data);
std::cout << "Log entry data: " << log_entry.dump(/*indent=*/2) << "\n";
});
}
// [END functions_log_stackdriver]
12 changes: 8 additions & 4 deletions examples/site/pubsub_subscribe/pubsub_subscribe.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,15 @@
// limitations under the License.

// [START functions_pubsub_subscribe]
#include <google/cloud/functions/cloud_event.h>
#include <google/cloud/functions/function.h>
#include <iostream>

void pubsub_subscribe(google::cloud::functions::CloudEvent event) { // NOLINT
// The framework converts the data from base64 if needed.
std::cout << event.data().value_or("") << "\n";
namespace gcf = ::google::cloud::functions;

gcf::Function pubsub_subscribe() {
return gcf::MakeFunction([](gcf::CloudEvent const& event) {
// The framework converts the data from base64 if needed.
std::cout << event.data().value_or("") << "\n";
});
}
// [END functions_pubsub_subscribe]
7 changes: 4 additions & 3 deletions examples/site/tips_gcp_apis/tips_gcp_apis.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@

// [START functions_pubsub_publish]
// [START functions_tips_gcp_apis]
#include <google/cloud/functions/http_request.h>
#include <google/cloud/functions/http_response.h>
#include <google/cloud/functions/function.h>
#include <google/cloud/pubsub/publisher.h>
#include <nlohmann/json.hpp>
#include <memory>
Expand Down Expand Up @@ -44,7 +43,7 @@ pubsub::Publisher GetPublisher(pubsub::Topic topic) {
}
} // namespace

gcf::HttpResponse tips_gcp_apis(gcf::HttpRequest request) { // NOLINT
gcf::HttpResponse tips_gcp_apis_impl(gcf::HttpRequest const& request) {
auto const* project = std::getenv("GCP_PROJECT");
if (project == nullptr) throw std::runtime_error("GCP_PROJECT is not set");

Expand All @@ -65,5 +64,7 @@ gcf::HttpResponse tips_gcp_apis(gcf::HttpRequest request) { // NOLINT
}
return response;
}

gcf::Function tips_gcp_apis() { return gcf::MakeFunction(tips_gcp_apis_impl); }
// [END functions_tips_gcp_apis]
// [END functions_pubsub_publish]
26 changes: 14 additions & 12 deletions examples/site/tips_infinite_retries/tips_infinite_retries.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// limitations under the License.

// [START functions_tips_infinite_retries]
#include <google/cloud/functions/cloud_event.h>
#include <google/cloud/functions/function.h>
#include <chrono>
#include <iostream>

Expand All @@ -23,17 +23,19 @@ namespace {
auto constexpr kMaxAge = std::chrono::seconds(10);
} // namespace

void tips_infinite_retries(gcf::CloudEvent event) { // NOLINT
using std::chrono::system_clock;
auto const age =
system_clock::now() - event.time().value_or(system_clock::time_point());
auto const seconds =
std::chrono::duration_cast<std::chrono::seconds>(age).count();
gcf::Function tips_infinite_retries() {
return gcf::MakeFunction([](gcf::CloudEvent const& event) {
using std::chrono::system_clock;
auto const age =
system_clock::now() - event.time().value_or(system_clock::time_point());
auto const seconds =
std::chrono::duration_cast<std::chrono::seconds>(age).count();

if (age >= kMaxAge) {
std::cout << "Dropped " << event.id() << " (age " << seconds << "s)\n";
return;
}
std::cout << "Processed " << event.id() << " (age " << seconds << "s)\n";
if (age >= kMaxAge) {
std::cout << "Dropped " << event.id() << " (age " << seconds << "s)\n";
return;
}
std::cout << "Processed " << event.id() << " (age " << seconds << "s)\n";
});
}
// [END functions_tips_infinite_retries]
11 changes: 6 additions & 5 deletions examples/site/tips_lazy_globals/tips_lazy_globals.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@
// limitations under the License.

// [START functions_tips_lazy_globals]
#include <google/cloud/functions/http_request.h>
#include <google/cloud/functions/http_response.h>
#include <google/cloud/functions/function.h>
#include <mutex>
#include <string>

Expand All @@ -27,8 +26,10 @@ std::string h;
void h_init() { h = "heavy computation"; }
} // namespace

gcf::HttpResponse tips_lazy_globals(gcf::HttpRequest /*request*/) { // NOLINT
std::call_once(h_init_flag, h_init);
return gcf::HttpResponse{}.set_payload("Global: " + h);
gcf::Function tips_lazy_globals() {
return gcf::MakeFunction([](gcf::HttpRequest const& /*request*/) {
std::call_once(h_init_flag, h_init);
return gcf::HttpResponse{}.set_payload("Global: " + h);
});
}
// [END functions_tips_lazy_globals]
22 changes: 12 additions & 10 deletions examples/site/tips_retry/tips_retry.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,22 @@
// limitations under the License.

// [START functions_tips_retry]
#include <google/cloud/functions/cloud_event.h>
#include <google/cloud/functions/function.h>
#include <nlohmann/json.hpp>
#include <iostream>

namespace gcf = ::google::cloud::functions;

void tips_retry(gcf::CloudEvent event) { // NOLINT
if (event.data_content_type().value_or("") != "application/json") {
std::cerr << "Error: expected application/json data\n";
return;
}
auto const payload = nlohmann::json::parse(event.data().value_or("{}"));
auto const retry = payload.value("retry", false);
if (retry) throw std::runtime_error("Throwing exception to force retry");
// Normal processing goes here.
gcf::Function tips_retry() {
return gcf::MakeFunction([](gcf::CloudEvent const& event) {
if (event.data_content_type().value_or("") != "application/json") {
std::cerr << "Error: expected application/json data\n";
return;
}
auto const payload = nlohmann::json::parse(event.data().value_or("{}"));
auto const retry = payload.value("retry", false);
if (retry) throw std::runtime_error("Throwing exception to force retry");
// Normal processing goes here.
});
}
// [END functions_tips_retry]
11 changes: 6 additions & 5 deletions examples/site/tips_scopes/tips_scopes.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@
// limitations under the License.

// [START functions_tips_scopes]
#include <google/cloud/functions/http_request.h>
#include <google/cloud/functions/http_response.h>
#include <google/cloud/functions/function.h>
#include <string>

namespace gcf = ::google::cloud::functions;
Expand All @@ -27,9 +26,11 @@ std::string light_computation();
std::string h = heavy_computation();
} // namespace

gcf::HttpResponse tips_scopes(gcf::HttpRequest /*request*/) { // NOLINT
auto l = light_computation();
return gcf::HttpResponse{}.set_payload("Global: " + h + ", Local: " + l);
gcf::Function tips_scopes() {
return gcf::MakeFunction([](gcf::HttpRequest const& /*request*/) {
auto l = light_computation();
return gcf::HttpResponse{}.set_payload("Global: " + h + ", Local: " + l);
});
}
// [END functions_tips_scopes]

Expand Down
Loading