0

这里是一个 Rust 新手,试图通过结合来编写一个 web 服务

https://github.com/seanmonstar/warp/blob/master/examples/todos.rshttps://github.com/launchbadge/sqlx/blob/master/examples/postgres/todos/src/main.rs

以下代码处于运行状态。我的问题是,我需要为每个处理程序克隆 dbpool 吗?Rust 的惯用方式是什么(我来自 Java->Kotlin->Go 背景,FWIW)

#![deny(warnings)]

use sqlx::postgres::{PgPoolOptions};
use std::env;
use warp::Filter;

#[tokio::main]
async fn main() -> Result<(), sqlx::Error> {
    let pool = PgPoolOptions::new()
        .max_connections(5)
        .connect("postgres://:@localhost/todo_db").await?;
    if env::var_os("RUST_LOG").is_none() {
        env::set_var("RUST_LOG", "todos=info");
    }
    pretty_env_logger::init();


    let api = filters::todos(pool);

    let routes = api.with(warp::log("todos"));
    // Start up the server...
    warp::serve(routes).run(([127, 0, 0, 1], 3030)).await;
    Ok(())
}

mod filters {
    use sqlx::{Pool, Postgres};
    use super::handlers;
    use super::models::{ListOptions, Todo};
    use warp::Filter;

    pub fn todos(
        db: Pool<Postgres>,
    ) -> impl Filter<Extract=impl warp::Reply, Error=warp::Rejection> + Clone {
        todos_list(db)
    }

    /// GET /todos?offset=3&limit=5
    pub fn todos_list(
        db: Pool<Postgres>,
    ) -> impl Filter<Extract=impl warp::Reply, Error=warp::Rejection> + Clone {
        warp::path!("todos")
            .and(warp::get())
            .and(warp::query::<ListOptions>())
            .and(with_db(db))
            .and_then(handlers::list_todos)
    }

    fn with_db(db: Pool<Postgres>) -> impl Filter<Extract=(Pool<Postgres>, ), Error=std::convert::Infallible> + Clone {
        warp::any().map(move || db.clone())
    }

    fn _json_body() -> impl Filter<Extract=(Todo, ), Error=warp::Rejection> + Clone {
        warp::body::content_length_limit(1024 * 16).and(warp::body::json())
    }
}

mod handlers {
    use super::models::{ListOptions};
    use std::convert::Infallible;
    use sqlx::{Pool, Postgres};
    use crate::models::Todo;

    pub async fn list_todos(_opts: ListOptions, db: Pool<Postgres>) -> Result<impl warp::Reply, Infallible> {
        let recs = sqlx::query!(
        r#"
SELECT id, description, done
FROM todos
ORDER BY id
        "#
    )
            .fetch_all(&db).await.expect("Some error message");

        let x: Vec<Todo> = recs.iter().map(|rec| {
            Todo { id: rec.id, text: rec.description.clone(), completed: rec.done }
        }).collect();


        Ok(warp::reply::json(&x))
    }
}

mod models {
    use serde_derive::{Deserialize, Serialize};


    #[derive(Debug, Deserialize, Serialize)]
    pub struct Todo {
        pub id: i64,
        pub text: String,
        pub completed: bool,
    }

    // The query parameters for list_todos.
    #[derive(Debug, Deserialize)]
    pub struct ListOptions {
        pub offset: Option<usize>,
        pub limit: Option<usize>,
    }
}
4

1 回答 1

1
于 2022-02-21T13:34:48.090 回答