(by Paul Done - @TheDonester)
A project to demonstrate some of the best practices for building resilient yet evolvable shared data applications using a flexible data model based database like MongoDB. Based on the The Six Principles for Building Robust Yet Flexible Shared Data Applications.
For this example, uses a library books scenario where app1 is a books inventory management application exposed as a REST API and app2 is a books ratings/scores management application exposed as a REST API. These two apps operate on and require different overlapping subsets of attributes about each book. For the sake of simplicity, both apps are part of the same Rust Cargo project with a shared main bootstrap function, but when run each of the two apps listens on a different localhost HTTP port (8181 & 8282 respectively).
(ensure you've cloned/copied this GitHub project first to your local machine)
-
Install the latest version of the Rust development environment, if it isn't already installed, via the rustup utility, including the rustc compiler & the cargo package/build manager. NOTE: If building on Windows 10, first ensure you have Microsoft's Build Tools for Visual Studio 2019 installed (and importantly, when running Microsoft's build tools installer, choose the C++ build tools option)
-
From a terminal/prompt/shell, from this project's root folder, run Rust's cargo command to build the project, as shown below:
cargo build
(ensure you have the URL of an MongoDB database accessible, to enable the reading & writing of records in the database collection library.books)
- Load the book data into a MongoDB database using the MongoDB Shell (the example command shown assumes the database is listening on localhost:27017):
mongosh data/book-data-prep-for-app1.js
- To run the first application (listening for REST API calls), execute the following command (example URL shown assumes you are running a MongoDB single server unauthenticated database on your local machine listening on localhost:27017 - change this URL, containing appropriate credentials, to match the location of your remote MongoDB database):
cargo run app1 mongodb://localhost:27017
- From a browser test the first application's REST API Get operation:
- http://127.0.0.1:8181/v1/books
- http://127.0.0.1:8181/v1/books?title=The%20Day%20of%20the%20Triffids&author=John%20Wyndham
- From a new terminal, run the first application's test script (tests retrieving all books + adding/modifying/removing a book via the app's REST API) by executing the following command:
tests/test_app1.sh
- Modify some of the book data to include some review scores in the MongoDB database using the MongoDB Shell (the example command shown assumes the database is listening on localhost:27017):
mongosh data/book-data-prep-for-app2.js
- To run the second application (listening for REST API calls), keep the existing first application running and in a new terminal execute the following command (change this URL to match the location of your remote MongoDB database):
cargo run app2 mongodb://localhost:27017
- From a browser test the second application's REST API Get operation:
- http://127.0.0.1:8282/v1/books?title=The%20Last%20Man&author=Mary%20Shelley
- http://127.0.0.1:8282/v1/books?title=The%20Day%20of%20the%20Triffids&author=John%20Wyndham
- Run the second application's test script (tests adding/modifying/removing some book reviews via the app's REST API) by executing the following command:
tests/test_app2.sh