Created
February 13, 2021 21:29
-
-
Save grant/b1d46232508d33df2ad9bac7061df4d9 to your computer and use it in GitHub Desktop.
Hello World, C++ Functions Framework
This file contains hidden or 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
#include <google/cloud/functions/http_request.h> | |
#include <google/cloud/functions/http_response.h> | |
#include <nlohmann/json.hpp> | |
namespace gcf = ::google::cloud::functions; | |
gcf::HttpResponse hello_world_http(gcf::HttpRequest request) { | |
auto greeting = [r = std::move(request)] { | |
auto request_json = nlohmann::json::parse(r.payload(), /*cb=*/nullptr, | |
/*allow_exceptions=*/false); | |
if (request_json.count("name") && request_json["name"].is_string()) { | |
return "Hello " + request_json.value("name", "World") + "!"; | |
} | |
return std::string("Hello World!"); | |
}; | |
gcf::HttpResponse response; | |
response.set_payload(greeting()); | |
response.set_header("content-type", "text/plain"); | |
return response; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment