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

Skip to content

doc: examples use declarative configuration 4/N #370

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 1 commit into from
Jul 4, 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
10 changes: 0 additions & 10 deletions ci/build-examples.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,6 @@ steps:
waitFor: ['gcf-builder-ready']
id: 'site-env_vars'
args: ['build',
'--env', 'GOOGLE_FUNCTION_SIGNATURE_TYPE=http',
'--env', 'GOOGLE_FUNCTION_TARGET=env_vars',
'--path', 'examples/site/env_vars',
'site-env_vars',
Expand All @@ -211,7 +210,6 @@ steps:
waitFor: ['gcf-builder-ready']
id: 'site-hello_world_error'
args: ['build',
'--env', 'GOOGLE_FUNCTION_SIGNATURE_TYPE=http',
'--env', 'GOOGLE_FUNCTION_TARGET=hello_world_error',
'--path', 'examples/site/hello_world_error',
'site-hello_world_error',
Expand All @@ -220,7 +218,6 @@ steps:
waitFor: ['gcf-builder-ready']
id: 'site-hello_world_get'
args: ['build',
'--env', 'GOOGLE_FUNCTION_SIGNATURE_TYPE=http',
'--env', 'GOOGLE_FUNCTION_TARGET=hello_world_get',
'--path', 'examples/site/hello_world_get',
'site-hello_world_get',
Expand Down Expand Up @@ -253,7 +250,6 @@ steps:
waitFor: ['gcf-builder-ready']
id: 'site-http_content'
args: ['build',
'--env', 'GOOGLE_FUNCTION_SIGNATURE_TYPE=http',
'--env', 'GOOGLE_FUNCTION_TARGET=http_content',
'--path', 'examples/site/http_content',
'site-http_content',
Expand All @@ -262,7 +258,6 @@ steps:
waitFor: ['gcf-builder-ready']
id: 'site-http_cors'
args: ['build',
'--env', 'GOOGLE_FUNCTION_SIGNATURE_TYPE=http',
'--env', 'GOOGLE_FUNCTION_TARGET=http_cors',
'--path', 'examples/site/http_cors',
'site-http_cors',
Expand All @@ -271,7 +266,6 @@ steps:
waitFor: ['gcf-builder-ready']
id: 'site-http_cors_auth'
args: ['build',
'--env', 'GOOGLE_FUNCTION_SIGNATURE_TYPE=http',
'--env', 'GOOGLE_FUNCTION_TARGET=http_cors_auth',
'--path', 'examples/site/http_cors_auth',
'site-http_cors_auth',
Expand All @@ -280,7 +274,6 @@ steps:
waitFor: ['gcf-builder-ready']
id: 'site-http_form_data'
args: ['build',
'--env', 'GOOGLE_FUNCTION_SIGNATURE_TYPE=http',
'--env', 'GOOGLE_FUNCTION_TARGET=http_form_data',
'--path', 'examples/site/http_form_data',
'site-http_form_data',
Expand All @@ -289,7 +282,6 @@ steps:
waitFor: ['gcf-builder-ready']
id: 'site-http_method'
args: ['build',
'--env', 'GOOGLE_FUNCTION_SIGNATURE_TYPE=http',
'--env', 'GOOGLE_FUNCTION_TARGET=http_method',
'--path', 'examples/site/http_method',
'site-http_method',
Expand All @@ -298,7 +290,6 @@ steps:
waitFor: ['gcf-builder-ready']
id: 'site-http_xml'
args: ['build',
'--env', 'GOOGLE_FUNCTION_SIGNATURE_TYPE=http',
'--env', 'GOOGLE_FUNCTION_TARGET=http_xml',
'--path', 'examples/site/http_xml',
'site-http_xml',
Expand All @@ -307,7 +298,6 @@ steps:
waitFor: ['gcf-builder-ready']
id: 'site-log_helloworld'
args: ['build',
'--env', 'GOOGLE_FUNCTION_SIGNATURE_TYPE=http',
'--env', 'GOOGLE_FUNCTION_TARGET=log_helloworld',
'--path', 'examples/site/log_helloworld',
'site-log_helloworld',
Expand Down
13 changes: 7 additions & 6 deletions examples/site/env_vars/env_vars.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,16 @@
// limitations under the License.

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

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

gcf::HttpResponse env_vars(gcf::HttpRequest /*request*/) { // NOLINT
char const* value = std::getenv("FOO");
if (value == nullptr) value = "FOO environment variable is not set";
return gcf::HttpResponse{}.set_payload(value);
gcf::Function env_vars() {
return gcf::MakeFunction([](gcf::HttpRequest const& /*request*/) {
char const* value = std::getenv("FOO");
if (value == nullptr) value = "FOO environment variable is not set";
return gcf::HttpResponse{}.set_payload(value);
});
}
// [END functions_env_vars]
63 changes: 31 additions & 32 deletions examples/site/hello_world_error/hello_world_error.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,46 +13,45 @@
// limitations under the License.

// [START functions_helloworld_error]
#include <google/cloud/functions/http_request.h>
#include <google/cloud/functions/http_response.h>
#include <google/cloud/functions/function.h>
#include <nlohmann/json.hpp>
#include <iostream>

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

// Though not used in this example, the request is passed by value to support
// applications that move-out its data.
gcf::HttpResponse hello_world_error(gcf::HttpRequest request) { // NOLINT
if (request.target() == "/return500") {
// An error response code does NOT create entries in Error Reporting
return gcf::HttpResponse{}.set_result(
gcf::HttpResponse::kInternalServerError);
}
// Unstructured logs to stdout and/or stderr do NOT create entries in Error
// Reporting
std::cout << "An error occurred (stdout)\n";
std::cerr << "An error occurred (stderr)\n";
gcf::Function hello_world_error() {
return gcf::MakeFunction([](gcf::HttpRequest const& request) {
if (request.target() == "/return500") {
// An error response code does NOT create entries in Error Reporting
return gcf::HttpResponse{}.set_result(
gcf::HttpResponse::kInternalServerError);
}
// Unstructured logs to stdout and/or stderr do NOT create entries in Error
// Reporting
std::cout << "An error occurred (stdout)\n";
std::cerr << "An error occurred (stderr)\n";

if (request.target() == "/throw/exception") {
// Throwing an exception WILL create new entries in Error Reporting
throw std::runtime_error("I failed you");
}
if (request.target() == "/throw/exception") {
// Throwing an exception WILL create new entries in Error Reporting
throw std::runtime_error("I failed you");
}

// Structured logs MAY create entries in Error Reporting depending on their
// severity. You can create structured logs manually (as shown here), or using
// your favorite logging library with suitable formatting.
std::cerr << nlohmann::json{{"severity", "info"},
{"message", "informational message"}}
.dump()
<< std::endl;
// Structured logs MAY create entries in Error Reporting depending on their
// severity. You can create structured logs manually (as shown here), or
// using your favorite logging library with suitable formatting.
std::cerr << nlohmann::json{{"severity", "info"},
{"message", "informational message"}}
.dump()
<< std::endl;

std::cerr << nlohmann::json{{"severity", "error"},
{"message", "an error message"}}
.dump()
<< std::endl;
std::cerr << nlohmann::json{{"severity", "error"},
{"message", "an error message"}}
.dump()
<< std::endl;

return gcf::HttpResponse{}
.set_header("content-type", "text/plain")
.set_payload("Hello World!");
return gcf::HttpResponse{}
.set_header("content-type", "text/plain")
.set_payload("Hello World!");
});
}
// [END functions_helloworld_error]
15 changes: 7 additions & 8 deletions examples/site/hello_world_get/hello_world_get.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,15 @@
// limitations under the License.

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

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

// Though not used in this example, the request is passed by value to support
// applications that move-out its data.
gcf::HttpResponse hello_world_get(gcf::HttpRequest) { // NOLINT
return gcf::HttpResponse{}
.set_header("content-type", "text/plain")
.set_payload("Hello World!");
gcf::Function hello_world_get() {
return gcf::MakeFunction([](gcf::HttpRequest const&) {
return gcf::HttpResponse{}
.set_header("content-type", "text/plain")
.set_payload("Hello World!");
});
}
// [END functions_helloworld_get]
39 changes: 20 additions & 19 deletions examples/site/http_content/http_content.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@
// limitations under the License.

// [START functions_http_content]
#include <google/cloud/functions/http_request.h>
#include <google/cloud/functions/http_response.h>
#include <google/cloud/functions/function.h>
#include <nlohmann/json.hpp>
#include <charconv>
#include <map>
Expand All @@ -29,25 +28,27 @@ std::map<std::string, std::string> parse_www_form_urlencoded(
std::string const& text);
} // namespace

gcf::HttpResponse http_content(gcf::HttpRequest request) { // NOLINT
std::string name;
auto const& headers = request.headers();
if (auto f = headers.find("content-type"); f != headers.end()) {
if (f->second == "application/json") {
name = nlohmann::json::parse(request.payload()).value("name", "");
} else if (f->second == "application/octet-stream" ||
f->second == "text/plain") {
name = request.payload(); // treat contents as a string
} else if (f->second == "application/x-www-form-urlencoded") {
// Use your preferred parser, here we use some custom code.
auto form = parse_www_form_urlencoded(request.payload());
name = form["name"];
gcf::Function http_content() {
return gcf::MakeFunction([](gcf::HttpRequest const& request) {
std::string name;
auto const& headers = request.headers();
if (auto f = headers.find("content-type"); f != headers.end()) {
if (f->second == "application/json") {
name = nlohmann::json::parse(request.payload()).value("name", "");
} else if (f->second == "application/octet-stream" ||
f->second == "text/plain") {
name = request.payload(); // treat contents as a string
} else if (f->second == "application/x-www-form-urlencoded") {
// Use your preferred parser, here we use some custom code.
auto form = parse_www_form_urlencoded(request.payload());
name = form["name"];
}
}
}

return gcf::HttpResponse{}
.set_header("content-type", "text/plain")
.set_payload("Hello " + name);
return gcf::HttpResponse{}
.set_header("content-type", "text/plain")
.set_payload("Hello " + name);
});
}
// [END functions_http_content]

Expand Down
35 changes: 18 additions & 17 deletions examples/site/http_cors/http_cors.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,28 @@
// limitations under the License.

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

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

gcf::HttpResponse http_cors(gcf::HttpRequest request) { // NOLINT
// Set CORS headers for preflight request
if (request.verb() == "OPTIONS") {
// Allows GET requests from any origin with the Content-Type header and
// caches preflight response for an 3600s
gcf::Function http_cors() {
return gcf::MakeFunction([](gcf::HttpRequest const& request) {
// Set CORS headers for preflight request
if (request.verb() == "OPTIONS") {
// Allows GET requests from any origin with the Content-Type header and
// caches preflight response for an 3600s
return gcf::HttpResponse{}
.set_result(gcf::HttpResponse::kNoContent)
.set_header("Access-Control-Allow-Origin", "*")
.set_header("Access-Control-Allow-Methods", "GET")
.set_header("Access-Control-Allow-Headers", "Content-Type")
.set_header("Access-Control-Max-Age", "3600");
}

return gcf::HttpResponse{}
.set_result(gcf::HttpResponse::kNoContent)
.set_header("Access-Control-Allow-Origin", "*")
.set_header("Access-Control-Allow-Methods", "GET")
.set_header("Access-Control-Allow-Headers", "Content-Type")
.set_header("Access-Control-Max-Age", "3600");
}

return gcf::HttpResponse{}
.set_header("Access-Control-Allow-Origin", "*")
.set_header("content-type", "text/plain")
.set_payload("Hello World!");
.set_header("content-type", "text/plain")
.set_payload("Hello World!");
});
}
// [END functions_http_cors]
39 changes: 20 additions & 19 deletions examples/site/http_cors_auth/http_cors_auth.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,30 @@
// limitations under the License.

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

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

gcf::HttpResponse http_cors_auth(gcf::HttpRequest request) { // NOLINT
// Set CORS headers for preflight request
if (request.verb() == "OPTIONS") {
// Allows GET requests from any origin with the Content-Type header and
// caches preflight response for an 3600s
gcf::Function http_cors_auth() {
return gcf::MakeFunction([](gcf::HttpRequest const& request) {
// Set CORS headers for preflight request
if (request.verb() == "OPTIONS") {
// Allows GET requests from any origin with the Content-Type header and
// caches preflight response for an 3600s
return gcf::HttpResponse{}
.set_result(gcf::HttpResponse::kNoContent)
.set_header("Access-Control-Allow-Origin", "https://mydomain.com")
.set_header("Access-Control-Allow-Methods", "GET")
.set_header("Access-Control-Allow-Headers", "Authorization")
.set_header("Access-Control-Max-Age", "3600")
.set_header("Access-Control-Allow-Credentials", "true");
}

return gcf::HttpResponse{}
.set_result(gcf::HttpResponse::kNoContent)
.set_header("Access-Control-Allow-Origin", "https://mydomain.com")
.set_header("Access-Control-Allow-Methods", "GET")
.set_header("Access-Control-Allow-Headers", "Authorization")
.set_header("Access-Control-Max-Age", "3600")
.set_header("Access-Control-Allow-Credentials", "true");
}

return gcf::HttpResponse{}
.set_header("Access-Control-Allow-Origin", "https://mydomain.com")
.set_header("Access-Control-Allow-Credentials", "true")
.set_header("content-type", "text/plain")
.set_payload("Hello World!");
.set_header("Access-Control-Allow-Credentials", "true")
.set_header("content-type", "text/plain")
.set_payload("Hello World!");
});
}
// [END functions_http_cors_auth]
9 changes: 6 additions & 3 deletions examples/site/http_form_data/http_form_data.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@
// limitations under the License.

// [START functions_http_form_data]
#include <google/cloud/functions/http_request.h>
#include <google/cloud/functions/http_response.h>
#include <google/cloud/functions/function.h>
#include <absl/strings/str_split.h>
#include <absl/strings/string_view.h>
#include <nlohmann/json.hpp>
Expand Down Expand Up @@ -59,7 +58,7 @@ class FormDataDelimiter {

} // namespace

gcf::HttpResponse http_form_data(gcf::HttpRequest request) {
gcf::HttpResponse http_form_data_impl(gcf::HttpRequest request) {
if (request.verb() != "POST") {
return gcf::HttpResponse{}.set_result(gcf::HttpResponse::kMethodNotAllowed);
}
Expand Down Expand Up @@ -118,6 +117,10 @@ gcf::HttpResponse http_form_data(gcf::HttpRequest request) {
.set_header("content-type", "application/json")
.set_payload(result.dump());
}

gcf::Function http_form_data() {
return gcf::MakeFunction(http_form_data_impl);
}
// [END functions_http_form_data]

namespace {
Expand Down
Loading