Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/rust/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Rust 如何在actix web中使用tokio_postgres_Rust_Actix Web - Fatal编程技术网

Rust 如何在actix web中使用tokio_postgres

Rust 如何在actix web中使用tokio_postgres,rust,actix-web,Rust,Actix Web,所以我想在actix API中使用tokio_postgres,我刚刚开始,在get请求中共享客户端时遇到了困难。 这是我的密码: use actix_web::{web, App, HttpServer, HttpResponse}; use tokio_postgres::{NoTls}; use std::cell::RefCell; mod api; use api::routes::hello_actix; fn main() { actix_rt::System::ru

所以我想在actix API中使用tokio_postgres,我刚刚开始,在get请求中共享客户端时遇到了困难。 这是我的密码:

use actix_web::{web, App, HttpServer, HttpResponse};
use tokio_postgres::{NoTls};
use std::cell::RefCell;

mod api;

use api::routes::hello_actix;

fn main() {
    actix_rt::System::run(|| {
        println!("connecting to postgres");
        let pro = tokio::spawn(
            async
            {
                let (client , conn) = match tokio_postgres::connect(
                    "foo",
                    NoTls)
                    .await {
                    Ok(t) => t,
                    Err(e) => panic!("{}", e)
                };

                tokio::spawn(async move {
                    if let Err(e) = conn.await {
                        eprintln!("connection error: {}", e);
                    }
                });

                println!("connected to postgres");
                HttpServer::new(|| {
                    App::new()
                        .data(RefCell::new(conn))
                        .route("/hello", web::get().to(hello_actix))
                        .default_service(web::route().to(|| HttpResponse::NotFound().body("404 Not Found")))
                })
                    .bind("127.0.0.1:8080")
                    .unwrap()
                    .run();
                Ok(())
            });

    });
}
我基于此代码,但它似乎不起作用

我还想保持“纯”,所以如果可能的话,我不想拥有全球客户。 甚至可以做我想做的事吗