Skip to content

Commit

Permalink
add fleshed out code examples and ref as includes
Browse files Browse the repository at this point in the history
  • Loading branch information
amalhotra-mdb committed Sep 26, 2024
1 parent e9a9862 commit a64eac7
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 20 deletions.
20 changes: 19 additions & 1 deletion source/connect/tls.txt
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,25 @@ in the following ways:
- Using the ``mongoc_uri_set_option_as_bool()`` function to set the ``MONGOC_URI_TLS`` connection
option to ``true``

.. include:: /includes/connect/tls-tabs.rst
.. tabs::

.. tab:: Connection String
:tabid: connectionstring

.. literalinclude:: /includes/connect/tls-connection-str.c
:language: c
:start-after: start-connect-to-atlas
:end-before: end-connect-to-atlas
:dedent:

.. tab:: MongoC URI Options
:tabid: mongocurioptions

.. literalinclude:: /includes/connect/tls-uri-options.c
:language: c
:start-after: start-connect-to-atlas
:end-before: end-connect-to-atlas
:dedent:

.. tip::

Expand Down
19 changes: 19 additions & 0 deletions source/includes/connect/tls-connection-str.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include <mongoc/mongoc.h>
#include <bson/bson.h>

int main (void) {

// start-connect-to-atlas
// Initialize the C Driver
mongoc_init ();

// Create a new client and set tls option to true
mongoc_client_t *client = mongoc_client_new ("mongodb+srv://<db_username>:<db_password>@<hostname/port>/?replicaSet=myRepl&tls=true");

// { Do database work here }

// Cleanup
mongoc_client_destroy (client);
mongoc_cleanup ();
// end-connect-to-atlas
}
19 changes: 0 additions & 19 deletions source/includes/connect/tls-tabs.rst

This file was deleted.

28 changes: 28 additions & 0 deletions source/includes/connect/tls-uri-options.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include <mongoc/mongoc.h>
#include <bson/bson.h>

int main(void) {

// start-connect
// Intialize the MongoDB C Driver
mongoc_init ();

bson_error_t error;

// Create a new client and set TLS option to true
mongoc_uri_t *uri = mongoc_uri_new_with_error ("mongodb://localhost:27017", &error);
mongoc_uri_set_option_as_bool (uri, MONGOC_URI_TLS, true);
if (!uri) {
fprintf (stderr, "failed to parse URI, error: %s\n", error.message);
goto cleanup;
}

mongoc_client_t *client = mongoc_client_new_from_uri (uri);

// { Do database work here }

cleanup:
mongoc_client_destroy (client);
mongoc_cleanup ();
// end-connect
}

0 comments on commit a64eac7

Please sign in to comment.