This project demonstrates how to implement a CRUD application for a restaurant database using MongoDB and various Rust web frameworks.
- Rust (latest stable version)
- MongoDB running locally or accessible via network
- MongoDB Atlas (sample dataset)[https://www.mongodb.com/docs/atlas/sample-data/] loaded in
- Clone the repository
- Copy
.env.example
to.env
and configure your MongoDB connection string - Load the sample restaurants database if not already loaded:
mongosh
use sample_restaurants
cargo run
The application will prompt you to choose a web framework implementation:
- None (MongoDB driver only) - Interactive CLI
- Actix Web (http://localhost:8080)
- Axum (http://localhost:8081)
- Rocket (http://localhost:8082)
- Warp (http://localhost:8083)
- Tide (http://localhost:8084)
All web framework implementations expose the same REST API endpoints:
- POST
/api/restaurants
- Body: Restaurant JSON
- GET
/api/restaurants
- Returns first 10 restaurants
- GET
/api/restaurants/{id}
- Returns restaurant by ObjectId
- PUT
/api/restaurants/{id}
- Body: Update JSON document
- DELETE
/api/restaurants/{id}
- Deletes restaurant by ObjectId
{
"address": {
"building": "8825",
"coord": [-73.8803827, 40.7643124],
"street": "Astoria Boulevard",
"zipcode": "11369"
},
"borough": "Queens",
"cuisine": "American",
"grades": [
{
"date": "2014-11-15T00:00:00.000Z",
"grade": "Z",
"score": 38
}
],
"name": "Brunos On The Boulevard",
"restaurant_id": "40356151"
}
src/models/restaurant.rs
- Restaurant data modelsrc/db/mongodb.rs
- MongoDB repository implementationsrc/frameworks/
- Web framework implementationssrc/error.rs
- Error handlingsrc/main.rs
- Framework selection and startup
You can test the API endpoints using curl, Postman, or any HTTP client. Example curl commands:
# List restaurants
curl http://localhost:8080/api/restaurants
# Get restaurant by ID
curl http://localhost:8080/api/restaurants/5eb3d668b31de5d588f42930
# Create restaurant
curl -X POST http://localhost:8080/api/restaurants \
-H "Content-Type: application/json" \
-d '{"name":"New Restaurant","borough":"Manhattan",...}'
# Update restaurant
curl -X PUT http://localhost:8080/api/restaurants/5eb3d668b31de5d588f42930 \
-H "Content-Type: application/json" \
-d '{"name":"Updated Name"}'
# Delete restaurant
curl -X DELETE http://localhost:8080/api/restaurants/5eb3d668b31de5d588f42930
Replace the port number (8080) with the appropriate port for your chosen framework:
- Actix Web: 8080
- Axum: 8081
- Rocket: 8082
- Warp: 8083
- Tide: 8084