Actix/Diesel API未响应Postman的请求

huangapple go评论48阅读模式
英文:

Actix/Diesel API not responding to requests from Postman

问题

我的代码 看起来 是正确的,因为它能够正确编译并且相当简单。但是当我使用 cargo run 运行我的应用程序时,尽管程序正常执行并输出一些调试信息,它不会响应任何请求。

这是我的 main.rs 文件:

use actix_web::{web, App, HttpServer};
use diesel::r2d2::{ConnectionManager, Pool};
use diesel::sqlite::SqliteConnection;
use dotenv::dotenv;

#[path = "api/books/books_handlers.rs"]
mod books_handlers;
#[path = "api/books_relationships/books_relationships_handlers.rs"]
mod books_relationships_handlers;
mod models;
mod routes;
mod schema;
mod logger;

#[actix_rt::main]
async fn main() -> std::io::Result<()> {

    // 加载 .env 文件并设置初始化变量
    dotenv().ok();
    std::env::set_var("RUST_LOG", "actix_web=debug");
    let database_url = std::env::var("DATABASE_URL").expect("DATABASE_URL 必须设置");

    // 创建 SQLite 的数据库连接池
    let manager = ConnectionManager::<SqliteConnection>::new(database_url);
    let pool: Pool<ConnectionManager<SqliteConnection>> = r2d2::Pool::builder()
        .build(manager)
        .expect("无法创建连接池。");

    // 启动 HTTP 服务器并注册路由
    println!("在 http://localhost:8080 上启动服务器");
    HttpServer::new(move || {
        App::new()
            .app_data(pool.clone())
            // Book 类
            .route("/create_book", web::post().to(books_handlers::create_book_handler))
            .route("/list_books", web::get().to(books_handlers::list_books_handler))
            .route("/get_book/{id}", web::post().to(books_handlers::read_book_by_id_handler))
            .route("/update_book/{id}", web::put().to(books_handlers::update_book_handler))
            .route("/delete_book/{id}", web::delete().to(books_handlers::delete_book_handler))
            // BookRelationships 类
            .route("/create_book_relationship", web::post().to(books_relationships_handlers::create_book_relationship_handler))
            .route("/list_book_relationships", web::get().to(books_relationships_handlers::list_books_handler))
            .route("/get_book_relationship/{id}", web::post().to(books_relationships_handlers::read_book_by_id_handler))
            .route("/update_book_relationship/{id}", web::put().to(books_relationships_handlers::update_book_handler))
            .route("/delete_book_relationship/{id}", web::delete().to(books_relationships_handlers::delete_book_handler))
    })
    .bind("127.0.0.1:8080")?
    .run()
    .await
}

这是你正在使用 Postman 尝试的第一个处理程序:

pub async fn create_book_handler(book_data: web::Json<Book>, pool: web::Data<DbPool>) -> HttpResponse {
    println!("create_book_handler: {:#?}", book_data);  // <-- 这里从未执行
    let result = books_dao::create_book(book_data, pool).await;
    match result {
        Ok(book) => {
            println!("create_book_handler, OK. Book: {:#?}", book);
            HttpResponse::Ok()
                .content_type(ContentType::json())
                .json(&book)
        },
        Err(err) => {
            println!("create_book_handler, ERROR: {:#?}", err);
            log(LogType::Error, err.to_string());
            HttpResponse::InternalServerError()
                .content_type(ContentType::json())
                .body("{err: '无法将书插入到数据库中'")
        }
    }
}

然后代码执行此函数,调用 Diesel 并更改数据库:

pub async fn create_book(book: web::Json<Book>, pool: web::Data<DbPool>) -> Result<usize, Error> {
    let mut conn = pool
        .get()
        .expect("无法从连接池获取数据库连接");

    diesel::insert_into(books::table)
        .values(book.into_inner())
        .execute(&mut conn)
}

但问题似乎甚至发生在此之前:即使在处理程序的开始处的 println! 也不执行。当我启动应用程序并向 http://127.0.0.1:8080/create_book 发送 POST 请求时,Postman 中会出现以下错误:

"请求的应用程序数据配置不正确。查看/启用调试日志以获取更多详细信息。"

我是否以错误的方式发送请求,或者是 API 发生故障?

英文:

My code seems to be ok, as it properly compiles and is quite simple. But when I run my app with cargo run, even though the program executes properly and outputs some debug printlns, it won't answer to any request.

This is my main.rs:

use actix_web::{web, App, HttpServer};
use diesel::r2d2::{ConnectionManager, Pool};
use diesel::sqlite::SqliteConnection;
use dotenvy::dotenv;

#[path = &quot;api/books/books_handlers.rs&quot;]
mod books_handlers;
#[path = &quot;api/books_relationships/books_relationships_handlers.rs&quot;]
mod books_relationships_handlers;
mod models;
mod routes;
mod schema;
mod logger;

#[actix_rt::main]
async fn main() -&gt; std::io::Result&lt;()&gt; {

    // Load .env file and set initialization variables
    dotenv().ok();
    std::env::set_var(&quot;RUST_LOG&quot;, &quot;actix_web=debug&quot;);
    let database_url = std::env::var(&quot;DATABASE_URL&quot;).expect(&quot;DATABASE_URL must be set&quot;);


    // Create db connection pool with SQLite
    let manager = ConnectionManager::&lt;SqliteConnection&gt;::new(database_url);
    let pool: Pool&lt;ConnectionManager&lt;SqliteConnection&gt;&gt; = r2d2::Pool::builder()
        .build(manager)
        .expect(&quot;Failed to create pool.&quot;);

    // Start HTTP server and register routes
    println!(&quot;Starting server at http://localhost:8080&quot;);
    HttpServer::new(move || {
        App::new()
            .app_data(pool.clone())
            // Book class
            .route(&quot;/create_book&quot;, web::post().to(books_handlers::create_book_handler))
            .route(&quot;/list_books&quot;, web::get().to(books_handlers::list_books_handler))
            .route(&quot;/get_book/{id}&quot;, web::post().to(books_handlers::read_book_by_id_handler))
            .route(&quot;/update_book/{id}&quot;, web::put().to(books_handlers::update_book_handler))
            .route(&quot;/delete_book/{id}&quot;, web::delete().to(books_handlers::delete_book_handler))
            // BookRelationships class
            .route(&quot;/create_book_relationship&quot;, web::post().to(books_relationships_handlers::create_book_relationship_handler))
            .route(&quot;/list_book_relationships&quot;, web::get().to(books_relationships_handlers::list_books_handler))
            .route(&quot;/get_book_relationship/{id}&quot;, web::post().to(books_relationships_handlers::read_book_by_id_handler))
            .route(&quot;/update_book_relationship/{id}&quot;, web::put().to(books_relationships_handlers::update_book_handler))
            .route(&quot;/delete_book_relationship/{id}&quot;, web::delete().to(books_relationships_handlers::delete_book_handler))

        })
    .bind(&quot;127.0.0.1:8080&quot;)?
    .run()
    .await
}

This is the first handler, the one I'm trying with Postman:

pub async fn create_book_handler(book_data: web::Json&lt;Book&gt;, pool: web::Data&lt;DbPool&gt;) -&gt; HttpResponse {
    println!(&quot;create_book_handler: {:#?}&quot;, book_data);  // &lt;-- this never gets executed
    let result = books_dao::create_book(book_data, pool).await;
    match result {
        Ok(book) =&gt; {
            println!(&quot;create_book_handler, OK. Book: {:#?}&quot;, book);
            HttpResponse::Ok()
            .content_type(ContentType::json())
            .json(&amp;book)
        },
        Err(err) =&gt; {
            println!(&quot;create_book_handler, ERROR: {:#?}&quot;, err);
            log(LogType::Error, err.to_string());
            HttpResponse::InternalServerError()
                .content_type(ContentType::json())
                .body(&quot;{err: &#39;Unable to insert book into database&#39;&quot;)
        }
    }
}

Then the code executes this function, calling Diesel and altering the DB:

pub async fn create_book(book: web::Json&lt;Book&gt;, pool: web::Data&lt;DbPool&gt;) -&gt; Result&lt;usize, Error&gt; {
    let mut conn = pool
        .get()
        .expect(&quot;Failed to get database connection from pool&quot;);

    diesel::insert_into(books::table)
        .values(book.into_inner())
        .execute(&amp;mut conn)
}

But the problem seems to be even before: not even the println! at the beginning of the handler get executed. When I start the app and send a POST request to http://127.0.0.1:8080/create_book, I get the following error in Postman:

Requested application data is not configured correctly. View/enable debug logs for more details.

Am I sending the requests in a wrong way, or is the API malfunctioning?

答案1

得分: 2

The DbPool is wrapped incorrectly. It should look like

...
    App::new()
            .app_data(actix_web::web::Data::new(pool.clone()))
...

This correctly wraps the DB Pool in the smart pointer that the route handlers can then use across your application

英文:

The DbPool is wrapped incorrectly. It should look like

...
    App::new()
            .app_data(actix_web::web::Data::new(pool.clone()))
...

This correctly wraps the DB Pool in the smart pointer that the route handlers can then use across your application

huangapple
  • 本文由 发表于 2023年2月9日 00:05:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/75388536.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定