Skip to content

Commit 7e42f6c

Browse files
committed
add a quickstart
1 parent 11e43da commit 7e42f6c

File tree

5 files changed

+117
-0
lines changed

5 files changed

+117
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ Feedback on the interface is welcome on GitHub issues and discussions. Though fu
3434
## Links
3535

3636
- [GitHub Repository][github]
37+
- [Quickstart](./etc/quickstart)
3738
- [Documentation Home][docs]
3839
- [Tutorials][docs-learn]
3940
- [Reference: Configuring, Building, and Using][docs-building]

etc/quickstart/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
cmake_minimum_required(VERSION 3.25)
2+
project(HelloAmongoc)
3+
add_executable(hello-amongoc hello-amongoc.c)
4+
find_package(amongoc REQUIRED)
5+
target_link_libraries(hello-amongoc PRIVATE amongoc::amongoc)

etc/quickstart/README.md

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# mongo-c-driver-async Quick Start
2+
3+
The following steps use vcpkg to install mongo-c-driver-async, build a test application, and connect to a local MongoDB server.
4+
5+
Steps were run on Debian 12.
6+
7+
Install vcpkg (if not already installed):
8+
```bash
9+
cd $HOME
10+
git clone https://github.com/microsoft/vcpkg.git
11+
VCPKG_ROOT=$HOME/vcpkg
12+
cd vcpkg
13+
./bootstrap-vcpkg.sh
14+
export PATH=$VCPKG_ROOT:$PATH
15+
```
16+
17+
Install amongoc:
18+
```bash
19+
cd $HOME
20+
AMONGOC_INSTALL="$HOME/mongo-c-driver-async-0.1.0"
21+
git clone https://github.com/mongodb-labs/mongo-c-driver-async/
22+
cd mongo-c-driver-async
23+
cmake -B_build
24+
cmake --build _build --parallel
25+
cmake --install _build --prefix "$AMONGOC_INSTALL"
26+
```
27+
28+
Build this test application:
29+
```bash
30+
# Run from this directory.
31+
cmake -DCMAKE_TOOLCHAIN_FILE=$VCPKG_ROOT/scripts/buildsystems/vcpkg.cmake \
32+
-DCMAKE_PREFIX_PATH=$AMONGOC_INSTALL \
33+
-Bcmake-build
34+
cmake --build cmake-build --target hello-amongoc
35+
```
36+
37+
Start a local MongoDB server:
38+
```bash
39+
cd $HOME
40+
# Get download links from: https://www.mongodb.com/try/download/community
41+
wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-debian12-8.0.4.tgz
42+
tar -xf mongodb-linux-x86_64-debian12-8.0.4.tgz
43+
rm mongodb-linux-x86_64-debian12-8.0.4.tgz
44+
# Start server
45+
mkdir mongodb-data
46+
./mongodb-linux-x86_64-debian12-8.0.4/bin/mongod --dbpath ./mongodb-data
47+
```
48+
49+
Run test application
50+
```bash
51+
./cmake-build/hello-amongoc
52+
```
53+
54+
Expect to see `Successfully connected!`.

etc/quickstart/hello-amongoc.c

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#include <amongoc/amongoc.h> // Make all APIs visible
2+
3+
#include <stdio.h>
4+
#include <stdlib.h>
5+
6+
amongoc_box on_connect(amongoc_box userdata, amongoc_status *status, amongoc_box result);
7+
8+
int main(void)
9+
{
10+
amongoc_loop loop;
11+
amongoc_status status = amongoc_default_loop_init(&loop);
12+
amongoc_if_error(status, msg)
13+
{
14+
fprintf(stderr, "Failed to prepare the event loop: %s\n", msg);
15+
return 2;
16+
}
17+
18+
// Initiate a connection
19+
amongoc_emitter em = amongoc_client_new(&loop, "mongodb://localhost:27017");
20+
// Set the continuation
21+
em = amongoc_then(em, &on_connect);
22+
// Run the program
23+
amongoc_detach_start(em);
24+
amongoc_default_loop_run(&loop);
25+
// Clean up
26+
amongoc_default_loop_destroy(&loop);
27+
return 0;
28+
}
29+
30+
amongoc_box on_connect(amongoc_box userdata, amongoc_status *status, amongoc_box result)
31+
{
32+
// We don't use the userdata
33+
(void)userdata;
34+
// Check for an error
35+
amongoc_if_error(*status, msg)
36+
{
37+
fprintf(stderr, "Error while connecting to server: %s\n", msg);
38+
}
39+
else
40+
{
41+
printf("Successfully connected!\n");
42+
amongoc_client *client;
43+
amongoc_box_take(client, result);
44+
// `client` now stores a valid client. We don't do anything else, so just delete it:
45+
amongoc_client_delete(client);
46+
}
47+
amongoc_box_destroy(result);
48+
return amongoc_nil;
49+
}

etc/quickstart/vcpkg.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"dependencies": [
3+
"fmt",
4+
"openssl",
5+
"boost-container",
6+
"boost-url"
7+
]
8+
}

0 commit comments

Comments
 (0)