Skip to content

Commit

Permalink
cpp: Add examples for C++
Browse files Browse the repository at this point in the history
  • Loading branch information
marcsiftstack committed Nov 13, 2024
1 parent 1a8ffbb commit a25844d
Show file tree
Hide file tree
Showing 7 changed files with 601 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,6 @@ For manual installation instructions for a particular supported programming lang
- [Go](/docs/go.md)
- [Rust](/docs/rust.md)
- [Python](/docs/python.md)
- [C++](/cpp/README.md)

Please keep in mind that the manual installation instructions aims to be general and do not need to be strictly followed. Users are encouraged to modify any of the steps or proceed with a custom setup if it better suits the needs of their project.
10 changes: 10 additions & 0 deletions buf_templates/buf.gen.cpp.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
version: v1
plugins:
- name: grpc_cpp
out: gen/proto/cpp
# grpc_cpp_plugin must be installed and accessible in PATH environment variable
path: grpc_cpp_plugin
- name: cpp
out: gen/proto/cpp
# Add path to protoc
protoc_path: <todo>
80 changes: 80 additions & 0 deletions cpp/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
# Sift C++

This tutorial shows how to build a C++ application that uses the Sift gRPC API.

## Installation

##### Build gRPC and Protocol Buffers

Follow the [official gRPC C++ tutorial](https://grpc.io/docs/languages/cpp/quickstart/#install-grpc) to build and locally install gRPC and Protocol Buffers. You should be able to build and run the [helloworld](https://grpc.io/docs/languages/cpp/quickstart/#build-the-example) example before continuing with the next section.

Ensure that `$ which grpc_cpp_plugin` and `$ which protoc` generate a path to executables.

You will also need to ensure that you have the [buf CLI](https://buf.build/docs/installation) installed.

Verify that `$ which buf` generates a path to the executable, before proceeding to the compilation steps.

##### C++ Protobuf Compilation

1. Clone this repository onto your local machine and `cd` into it:

```bash
$ git clone https://github.com/sift-stack/sift
$ cd sift
```

2. Assuming the path to the directory where we'll build the package is `$PACKAGE_DIR`, run the following command in the `sift` directory that you just cloned:

```bash
$ buf export protos --output=$PACKAGE_DIR/protos --config protos/buf.yaml
```

The Sift protos and its imports can now be found in your `$PACKAGE_DIR/protos` directory.

3. Copy the `buf` template for C++ to `$PACKAGE_DIR` and update the `protoc_path` variable to point to the `protoc` executable.

```bash
$ cp buf_templates/buf.gen.cpp.yaml $PACKAGE_DIR/buf.gen.yaml
```

4. `cd` into `$PACKAGE_DIR`.

5. Once inside of `$PACKAGE_DIR`, ensure that `buf.gen.yaml` is at the root.

6. Compile your protobufs.

```bash
$ buf generate protos
```

The generated code will be in the `gen` directory. Copy `gen` into to any C++ projects that you want to build against.


## Examples

Various examples can be found in the [examples](./examples) directory. To run any of those examples clone this repo, follow the steps above, then do the following:

1. Copy the generated code into the example directory that you want to build:
```
$ cp -r $PACKAGE_DIR/gen $EXAMPLE
```

2. `cd` into the example
```
$ cd $EXAMPLE
```

3. Ensure the environment variable `MY_INSTALL_DIR` holds the path to [locally installed packages](https://grpc.io/docs/languages/cpp/quickstart/#setup).

4. Build the example using `cmake`:
```
mkdir build
cd build
cmake -DCMAKE_INSTALL_PREFIX=$MY_INSTALL_DIR ..
cmake --build . -j 4
```

5. Run the example with required environment variables:
```
$ SIFT_URL=<grpc_api_url>:443 SIFT_API_KEY=<api_key> SIFT_ORGANIZATION_ID=<organization_id> ./Example
```
23 changes: 23 additions & 0 deletions cpp/ingestion/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
cmake_minimum_required(VERSION 3.14)
project(IngestionExample)

set(CMAKE_CXX_STANDARD 17)

find_package(Protobuf CONFIG REQUIRED)
find_package(gRPC CONFIG REQUIRED)

# Include the generated files directory
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/gen/proto/cpp)

# Gather all generated .pb.cc and .grpc.pb.cc files
file(GLOB_RECURSE PROTO_SRC "${CMAKE_CURRENT_SOURCE_DIR}/gen/proto/cpp/*.pb.cc"
"${CMAKE_CURRENT_SOURCE_DIR}/gen/proto/cpp/*.grpc.pb.cc")
file(GLOB_RECURSE PROTO_HDR "${CMAKE_CURRENT_SOURCE_DIR}/gen/proto/cpp/*.pb.h"
"${CMAKE_CURRENT_SOURCE_DIR}/gen/proto/cpp/*.grpc.pb.h")

# Add your application source files here
set(SOURCE_FILES ingest.cc ${PROTO_SRC} ${PROTO_HDR})

add_executable(IngestionExample ${SOURCE_FILES})

target_link_libraries(IngestionExample PRIVATE gRPC::grpc++ protobuf::libprotobuf)
Loading

0 comments on commit a25844d

Please sign in to comment.