Skip to content

How to create a connexion pool with rocket_contrib #1766

Answered by folivi
folivi asked this question in Questions
Discussion options

You must be logged in to vote

I ended up doing this.
Hope it helps anyone.

If this is not correct/safe to do so, please advise.

#![feature(proc_macro_hygiene, decl_macro)]
use mongodb::{
    Client, Database,
};
use rocket::{self, get, State};

#[get("/")]
async fn index(mongo_db: &State<AppDataPool>) -> &'static str {
    for coll_name in mongo_db.mongo.list_collection_names(None).await {
        println!("collection: {:?}", coll_name);
    }
    "helloooo"
}

struct AppDataPool {
    mongo: Database,
}

#[rocket::main]
async fn main() {
    let client = Client::with_uri_str("----").await;

    let db = client.unwrap().database("my-db");
    for coll_name in db.list_collection_names(None).await {
        println!("co…

Replies: 2 comments 2 replies

Comment options

You must be logged in to vote
1 reply
@folivi
Comment options

Comment options

You must be logged in to vote
1 reply
@jebrosen
Comment options

Answer selected by jebrosen
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
3 participants