actix-web

Do a synchronous http client fetch within an actix thread

对着背影说爱祢 提交于 2021-02-11 06:57:19
问题 I have an actix endpoint, and I need to do a synchronous http client fetch to get some results, and return some data. My endpoints cannot use async , so I can't use any .await methods. I've tried using reqwests blocking client in my endpoint like so: { ... let res = reqwest::blocking::get(&fetch_url)? .json::<MyResp>()?; ... But it gives me the error: thread 'main' panicked at 'Cannot start a runtime from within a runtime. This happens because a function (like `block_on`) attempted to block

Do a synchronous http client fetch within an actix thread

吃可爱长大的小学妹 提交于 2021-02-11 06:57:19
问题 I have an actix endpoint, and I need to do a synchronous http client fetch to get some results, and return some data. My endpoints cannot use async , so I can't use any .await methods. I've tried using reqwests blocking client in my endpoint like so: { ... let res = reqwest::blocking::get(&fetch_url)? .json::<MyResp>()?; ... But it gives me the error: thread 'main' panicked at 'Cannot start a runtime from within a runtime. This happens because a function (like `block_on`) attempted to block

Get Body of Response in Actix web test request in Rust

青春壹個敷衍的年華 提交于 2020-12-12 05:38:36
问题 I'm building a web api service with rust and actix. I want to test a route and check if the received response body is what i expect. But I'm strugeling with converting the received body (ResponseBody) into a Json/Bson. The called route actually returns application/json. Thanks for your help. let mut app = test::init_service(App::new() .data(AppState { database: db.clone() }) .route("/recipes/{id}", web::post().to(add_one_recipe))).await; let payload = create_one_recipe().as_document().unwrap(

How do I resolve “implementation of serde::Deserialize is not general enough” with actix-web's Json type?

跟風遠走 提交于 2020-04-12 19:44:37
问题 I'm writing a server using actix-web: use actix_web::{post, web, Responder}; use serde::Deserialize; #[derive(Deserialize)] struct UserModel<'a, 'b> { username: &'a str, password: &'b str, } #[post("/")] pub fn register(user_model: web::Json<UserModel>) -> impl Responder {} The compiler gives this error: error: implementation of `user::_IMPL_DESERIALIZE_FOR_UserModel::_serde::Deserialize` is not general enough --> src/user.rs:31:1 | 31 | #[post("/")] | ^^^^^^^^^^^^ | = note: `user::UserModel<

Borrowed value does not live long enough, moved due to use in closure E0597

可紊 提交于 2020-03-17 03:17:04
问题 I am taking my first steps on Actix-Web. But this closure causes me errors #[derive(Deserialize, Serialize, Debug, Copy, Clone)] pub struct PaginationQuery { pub limit: Option<u32>, pub offset: Option<u32>, } pub fn get_all_trainings_2( query: web::Query<PaginationQuery>, pool: web::Data<Pool>, ) -> impl Future<Item = HttpResponse, Error = Error> { let mut pagination = query.0; // Thread Blocking web::block(move || database::get_exercises(pool, pagination)).then(|res| { match res { Ok(

Borrowed value does not live long enough, moved due to use in closure E0597

限于喜欢 提交于 2020-03-17 03:16:42
问题 I am taking my first steps on Actix-Web. But this closure causes me errors #[derive(Deserialize, Serialize, Debug, Copy, Clone)] pub struct PaginationQuery { pub limit: Option<u32>, pub offset: Option<u32>, } pub fn get_all_trainings_2( query: web::Query<PaginationQuery>, pool: web::Data<Pool>, ) -> impl Future<Item = HttpResponse, Error = Error> { let mut pagination = query.0; // Thread Blocking web::block(move || database::get_exercises(pool, pagination)).then(|res| { match res { Ok(

Does actix_web's App::register_data create a single instance or one instance per thread?

余生颓废 提交于 2020-01-26 04:44:31
问题 I am trying to set up a global state for my actix_web::HttpServer , and it seems like register_data is the proper API (I could be wrong). From the documentation, it is not clear to me how to create a single instance of application data shared by all HttpServer threads. Here is my code piece: HttpServer::new(|| { App::new() .register_data(web::Data::new(Mutex::new(MyServer::new()))) .service(web::resource("/myservice").route(web::post().to(my_service))) .service(web::resource("/list").to(list

HTTP request inside actix-web handler -> Multiple executors at once: EnterError

狂风中的少年 提交于 2019-12-23 21:02:59
问题 When creating a hyper post request inside an actix-web resolver, the following error is thrown - how can one send one a http request by spawning the request into the existing executor? thread 'actix-rt:worker:1' panicked at 'Multiple executors at once: EnterError { reason: "attempted to run an executor while another executor is already running" }', src/libcore/result.rs:999:5 note: Run with `RUST_BACKTRACE=1` environment variable to display a backtrace. Panic in Arbiter thread, shutting down