I have set up a database connection but I want to share it with my warp API handlers.
my Cargo.toml
[package]
name = "mongo-warp"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
dotenv = "0.15.0"
mongodb = "2.2.2"
bson = { version = "2", features = ["chrono-0_4"] }
tokio = "1"
serde = "1"
warp = "0.3"
serde_json = "1.0"
and in main I have the database setup and working along with the routes that I'm importing from auth.rs
mod auth;
use dotenv;
use tokio;
use std::{env, error::Error};
use mongodb::{options::{ClientOptions}, Client, bson::doc};
use warp::Filter;
use crate::auth::auth_filter;
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
dotenv::dotenv().ok();
let client_uri = env::var("MONGODB_URI").expect("You must set the MONGODB_URI .env");
let options = ClientOptions::parse(&client_uri).await?;
let client = Client::with_options(options)?;
// TODO: Do this bit in the join_handler
let users = client.database("rusty_db").collection("users");
let user = doc! { "password": "1984", "username": "GeorgeOrwell" };
let insert_result = users.insert_one(user, None).await?;
println!("New document ID: {}", insert_result.inserted_id);
// End
let apis = auth_filter();
let welcome = warp::path::end().map(|| "Welcome to my api");
let routes = apis.or(welcome);
warp::serve(routes).run(([127, 0, 0, 1], 3000)).await;
Ok(())
}
and finally, the route which is where I want to have access to the database with join_handler and signin_handler
use serde_json::{json, Value};
use warp::{Filter, reply::Json};
pub fn auth_filter() -> impl Filter<Extract = impl warp::Reply, Error = warp::Rejection> + Clone {
let join = warp::path("join")
.and(warp::get())
.and(warp::path::end())
.and_then(join_handler);
let signin = warp::path("signin")
.and(warp::post())
.and(warp::body::json())
.and_then(signin_handler);
join.or(signin)
}
async fn join_handler() -> Result<Json, warp::Rejection> {
let user = json!({"username":"GeorgeOrwell", "password": "1984", "id": "62a5108336aaebf431faa522"});
let user = warp::reply::json(&user);
Ok(user)
}
async fn signin_handler(data: Value) -> Result<Json, warp::Rejection> {
let credentials = data;
let credentials = warp::reply::json(&credentials);
Ok(credentials)
}