Skip to content

Commit be3c777

Browse files
authored
chore: Add gcp skeleton (#290)
1 parent de995c1 commit be3c777

File tree

8 files changed

+139
-1
lines changed

8 files changed

+139
-1
lines changed

cmake/third_party.cmake

+1-1
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@ add_third_party(
352352
add_third_party(
353353
rapidjson
354354
GIT_REPOSITORY https://github.com/Tencent/rapidjson.git
355-
GIT_TAG 1a803826f1197b5e30703afe4b9c0e7dd48074f5
355+
GIT_TAG ab1842a
356356
CMAKE_PASS_FLAGS "-DRAPIDJSON_BUILD_TESTS=OFF -DRAPIDJSON_BUILD_EXAMPLES=OFF \
357357
-DRAPIDJSON_BUILD_DOC=OFF"
358358
LIB "none"

examples/CMakeLists.txt

+3
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,8 @@ cxx_link(echo_server base fibers2 http_server_lib TRDP::gperf)
1313
add_executable(s3_demo s3_demo.cc)
1414
cxx_link(s3_demo base awsv2_lib)
1515

16+
add_executable(gcs_demo gcs_demo.cc)
17+
cxx_link(gcs_demo gcp_lib)
18+
1619
add_executable(https_client_cli https_client_cli.cc)
1720
cxx_link(https_client_cli base fibers2 http_client_lib tls_lib)

examples/gcs_demo.cc

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// Copyright 2024, Roman Gershman. All rights reserved.
2+
// See LICENSE for licensing terms.
3+
4+
#include "base/flags.h"
5+
#include "base/init.h"
6+
#include "base/logging.h"
7+
#include "util/cloud/gcp/gcs.h"
8+
#include "util/fibers/pool.h"
9+
10+
using namespace std;
11+
using namespace boost;
12+
using namespace util;
13+
14+
using absl::GetFlag;
15+
16+
ABSL_FLAG(string, bucket, "", "");
17+
ABSL_FLAG(string, access_token, "", "");
18+
ABSL_FLAG(uint32_t, connect_ms, 2000, "");
19+
ABSL_FLAG(bool, epoll, false, "Whether to use epoll instead of io_uring");
20+
21+
22+
void Run(SSL_CTX* ctx) {
23+
fb2::ProactorBase* pb = fb2::ProactorBase::me();
24+
cloud::GCS gcs(ctx, pb);
25+
error_code ec = gcs.Connect(GetFlag(FLAGS_connect_ms));
26+
CHECK(!ec) << "Could not connect " << ec;
27+
auto res = gcs.ListBuckets();
28+
CHECK(res) << res.error();
29+
for (auto v : *res) {
30+
CONSOLE_INFO << v;
31+
}
32+
}
33+
34+
int main(int argc, char** argv) {
35+
MainInitGuard guard(&argc, &argv);
36+
37+
std::unique_ptr<util::ProactorPool> pp;
38+
39+
#ifdef __linux__
40+
if (absl::GetFlag(FLAGS_epoll)) {
41+
pp.reset(util::fb2::Pool::Epoll());
42+
} else {
43+
pp.reset(util::fb2::Pool::IOUring(256));
44+
}
45+
#else
46+
pp.reset(util::fb2::Pool::Epoll());
47+
#endif
48+
49+
pp->Run();
50+
51+
SSL_CTX* ctx = util::http::TlsClient::CreateSslContext();
52+
pp->GetNextProactor()->Await([ctx] {
53+
Run(ctx);
54+
});
55+
util::http::TlsClient::FreeContext(ctx);
56+
57+
58+
return 0;
59+
}

util/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ add_subdirectory(html)
77
add_subdirectory(metrics)
88
add_subdirectory(tls)
99
add_subdirectory(http)
10+
add_subdirectory(cloud)
1011

1112
if (WITH_AWS)
1213
add_subdirectory(aws)

util/cloud/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
add_subdirectory(gcp)

util/cloud/gcp/CMakeLists.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
add_library(gcp_lib gcs.cc)
2+
3+
cxx_link(gcp_lib http_client_lib)

util/cloud/gcp/gcs.cc

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Copyright 2024, Roman Gershman. All rights reserved.
2+
// See LICENSE for licensing terms.
3+
4+
#include "util/cloud/gcp/gcs.h"
5+
6+
namespace util {
7+
namespace cloud {
8+
9+
namespace {
10+
constexpr char kDomain[] = "www.googleapis.com";
11+
} // namespace
12+
13+
14+
GCS::GCS(SSL_CTX* ssl_cntx, fb2::ProactorBase* pb) {
15+
client_.reset(new http::TlsClient(pb));
16+
}
17+
18+
GCS::~GCS() {
19+
}
20+
21+
std::error_code GCS::Connect(unsigned msec) {
22+
client_->set_connect_timeout_ms(msec);
23+
24+
return client_->Connect(kDomain, "443");
25+
}
26+
27+
auto GCS::ListBuckets() -> ListBucketResult {
28+
return {};
29+
}
30+
31+
} // namespace cloud
32+
} // namespace util

util/cloud/gcp/gcs.h

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright 2024, Roman Gershman. All rights reserved.
2+
// See LICENSE for licensing terms.
3+
4+
#pragma once
5+
6+
#include <io/io.h>
7+
8+
#include <vector>
9+
10+
#include "util/http/http_client.h"
11+
12+
typedef struct ssl_ctx_st SSL_CTX;
13+
14+
namespace util {
15+
16+
namespace fb2 {
17+
class ProactorBase;
18+
} // namespace fb2
19+
20+
namespace cloud {
21+
22+
class GCS {
23+
public:
24+
using ListBucketResult = io::Result<std::vector<std::string>>;
25+
26+
GCS(SSL_CTX* ssl_cntx, fb2::ProactorBase* pb);
27+
~GCS();
28+
29+
std::error_code Connect(unsigned msec);
30+
31+
ListBucketResult ListBuckets();
32+
33+
private:
34+
SSL_CTX* ssl_ctx_;
35+
std::unique_ptr<http::Client> client_;
36+
};
37+
38+
} // namespace cloud
39+
} // namespace util

0 commit comments

Comments
 (0)