I'm trying to write an HTTP endpoint using actix-web 1.0. I've reduced the function so that it's just returning the user that is passed to it, but the compiler still gives an error.
extern crate actix_web;
extern crate chrono;
extern crate futures;
extern crate listenfd;
#[macro_use]
extern crate serde_derive;
extern crate dotenv;
use actix_web::{error, web, App, Error, HttpResponse, HttpServer};
use futures::future::Future;
#[derive(Debug, Deserialize, Serialize)]
pub struct LoginUser {
pub username: String,
pub password: String,
}
pub fn login(
login_user: web::Json<LoginUser>,
) -> impl Future<Item = HttpResponse, Error = error::BlockingError<Error>> {
web::block(move || {
let login_user = login_user.into_inner();
let user = LoginUser {
username: login_user.username,
password: login_user.password,
};
Ok(HttpResponse::Ok().json(user))
})
}
pub fn router(cfg: &mut web::ServiceConfig) {
cfg.service(web::scope("/").service(web::resource("").route(web::get().to(login))));
}
fn main() -> std::io::Result<()> {
HttpServer::new(move || App::new().configure(router))
.bind("127.0.0.1:3000")?
.run()
}
Here is my cargo.toml.
[package]
name = "log"
version = "0.1.0"
authors = ["[email protected]"
edition = "2018"
[dependencies]
actix-files = "~0.1"
actix-web = "~1.0"
chrono = { version = "0.4.6", features = ["serde"] }
listenfd = "0.3"
diesel = {version = "1.4.1", features = ["postgres", "uuid", "r2d2", "chrono"]}
dotenv = "0.13"
failure = "0.1"
futures = "0.1"
scrypt = "0.2.0"
serde_derive="1.0"
serde_json="1.0"
serde="1.0"
I'm getting the compilation error
| web::block(move || {
| ^^^^^^^^^^ `(dyn std::any::Any + 'static)` cannot be sent between threads safely
I think it has something to do with the use of login_user
in the web::block
, but it's hard to tell from the error. What is the preferred way to use request parameters asynchronously in Rust or actix safely?
main.rs
file. Thanks! – Shepmaster