From d98c936295a635ece37ed527c85b3c25b7d888a0 Mon Sep 17 00:00:00 2001 From: Dessalines Date: Tue, 21 Apr 2020 16:40:03 -0400 Subject: [PATCH 1/3] Adding web::blocks to routes. --- server/src/api/mod.rs | 2 +- server/src/routes/api.rs | 20 +++++++++++--------- server/src/routes/feeds.rs | 16 ++++++++-------- server/src/routes/index.rs | 2 +- server/src/routes/mod.rs | 3 +-- server/src/routes/nodeinfo.rs | 4 ++-- server/src/routes/webfinger.rs | 4 ++-- server/src/routes/websocket.rs | 3 +-- 8 files changed, 27 insertions(+), 27 deletions(-) diff --git a/server/src/api/mod.rs b/server/src/api/mod.rs index aab00c047..3488a8c42 100644 --- a/server/src/api/mod.rs +++ b/server/src/api/mod.rs @@ -69,7 +69,7 @@ impl Oper { } pub trait Perform { - type Response: serde::ser::Serialize; + type Response: serde::ser::Serialize + Send; fn perform( &self, diff --git a/server/src/routes/api.rs b/server/src/routes/api.rs index 74ad7239b..5ff53e270 100644 --- a/server/src/routes/api.rs +++ b/server/src/routes/api.rs @@ -5,7 +5,6 @@ use crate::api::post::*; use crate::api::site::*; use crate::api::user::*; use crate::rate_limit::RateLimit; -use actix_web::guard; pub fn config(cfg: &mut web::ServiceConfig, rate_limit: &RateLimit) { cfg.service( @@ -140,13 +139,14 @@ pub fn config(cfg: &mut web::ServiceConfig, rate_limit: &RateLimit) { ); } -fn perform( +async fn perform( data: Request, db: DbPoolParam, chat_server: ChatServerParam, ) -> Result where Oper: Perform, + Request: Send + 'static, { let ws_info = WebsocketInfo { chatserver: chat_server.get_ref().to_owned(), @@ -155,9 +155,11 @@ where let oper: Oper = Oper::new(data); - let res = oper.perform(db.get_ref().to_owned(), Some(ws_info)); - - Ok(HttpResponse::Ok().json(res?)) + let res = web::block(move || oper.perform(db.get_ref().to_owned(), Some(ws_info))) + .await + .map(|json| HttpResponse::Ok().json(json)) + .map_err(|e| ErrorBadRequest(e))?; + Ok(res) } async fn route_get( @@ -166,10 +168,10 @@ async fn route_get( chat_server: ChatServerParam, ) -> Result where - Data: Serialize, + Data: Serialize + Send + 'static, Oper: Perform, { - perform::(data.0, db, chat_server) + perform::(data.0, db, chat_server).await } async fn route_post( @@ -178,8 +180,8 @@ async fn route_post( chat_server: ChatServerParam, ) -> Result where - Data: Serialize, + Data: Serialize + Send + 'static, Oper: Perform, { - perform::(data.0, db, chat_server) + perform::(data.0, db, chat_server).await } diff --git a/server/src/routes/feeds.rs b/server/src/routes/feeds.rs index 8eefe02a0..b36755358 100644 --- a/server/src/routes/feeds.rs +++ b/server/src/routes/feeds.rs @@ -28,7 +28,7 @@ pub fn config(cfg: &mut web::ServiceConfig) { async fn get_all_feed( info: web::Query, db: web::Data>>, -) -> Result { +) -> Result { let res = web::block(move || { let conn = db.get()?; get_feed_all_data(&conn, &get_sort_type(info)?) @@ -39,7 +39,7 @@ async fn get_all_feed( .content_type("application/rss+xml") .body(rss) }) - .map_err(|_| HttpResponse::InternalServerError())?; + .map_err(|e| ErrorBadRequest(e))?; Ok(res) } @@ -70,7 +70,7 @@ async fn get_feed( path: web::Path<(String, String)>, info: web::Query, db: web::Data>>, -) -> Result { +) -> Result { let res = web::block(move || { let conn = db.get()?; @@ -100,7 +100,7 @@ async fn get_feed( .content_type("application/rss+xml") .body(rss) }) - .map_err(|_| HttpResponse::InternalServerError())?; + .map_err(|e| ErrorBadRequest(e))?; Ok(res) } @@ -116,7 +116,7 @@ fn get_feed_user( conn: &PgConnection, sort_type: &SortType, user_name: String, -) -> Result { +) -> Result { let site_view = SiteView::read(&conn)?; let user = User_::find_by_username(&conn, &user_name)?; let user_url = user.get_profile_url(); @@ -142,7 +142,7 @@ fn get_feed_community( conn: &PgConnection, sort_type: &SortType, community_name: String, -) -> Result { +) -> Result { let site_view = SiteView::read(&conn)?; let community = Community::read_from_name(&conn, community_name)?; let community_url = community.get_url(); @@ -172,7 +172,7 @@ fn get_feed_front( conn: &PgConnection, sort_type: &SortType, jwt: String, -) -> Result { +) -> Result { let site_view = SiteView::read(&conn)?; let user_id = Claims::decode(&jwt)?.claims.id; @@ -197,7 +197,7 @@ fn get_feed_front( Ok(channel_builder) } -fn get_feed_inbox(conn: &PgConnection, jwt: String) -> Result { +fn get_feed_inbox(conn: &PgConnection, jwt: String) -> Result { let site_view = SiteView::read(&conn)?; let user_id = Claims::decode(&jwt)?.claims.id; diff --git a/server/src/routes/index.rs b/server/src/routes/index.rs index 9f18b64d8..895af4350 100644 --- a/server/src/routes/index.rs +++ b/server/src/routes/index.rs @@ -41,7 +41,7 @@ pub fn config(cfg: &mut web::ServiceConfig) { .route("/password_change/{token}", web::get().to(index)); } -async fn index() -> Result { +async fn index() -> Result { Ok(NamedFile::open( Settings::get().front_end_dir + "/index.html", )?) diff --git a/server/src/routes/mod.rs b/server/src/routes/mod.rs index b1ea41679..37c56eba0 100644 --- a/server/src/routes/mod.rs +++ b/server/src/routes/mod.rs @@ -5,14 +5,13 @@ use crate::websocket::{server::ChatServer, WebsocketInfo}; use crate::{get_ip, markdown_to_html, version, Settings}; use actix::prelude::*; use actix_files::NamedFile; -use actix_web::{body::Body, web::Query, *}; +use actix_web::{body::Body, error::ErrorBadRequest, web::Query, *}; use actix_web_actors::ws; use chrono::{DateTime, NaiveDateTime, Utc}; use diesel::{ r2d2::{ConnectionManager, Pool}, PgConnection, }; -use failure::Error; use log::{error, info}; use regex::Regex; use rss::{CategoryBuilder, ChannelBuilder, GuidBuilder, Item, ItemBuilder}; diff --git a/server/src/routes/nodeinfo.rs b/server/src/routes/nodeinfo.rs index 829d56b1f..97863271b 100644 --- a/server/src/routes/nodeinfo.rs +++ b/server/src/routes/nodeinfo.rs @@ -18,7 +18,7 @@ async fn node_info_well_known() -> HttpResponse { async fn node_info( db: web::Data>>, -) -> Result { +) -> Result { let res = web::block(move || { let conn = db.get()?; let site_view = match SiteView::read(&conn) { @@ -49,7 +49,7 @@ async fn node_info( }) .await .map(|json| HttpResponse::Ok().json(json)) - .map_err(|_| HttpResponse::InternalServerError())?; + .map_err(|e| ErrorBadRequest(e))?; Ok(res) } diff --git a/server/src/routes/webfinger.rs b/server/src/routes/webfinger.rs index b35ddbf79..f39c6b67b 100644 --- a/server/src/routes/webfinger.rs +++ b/server/src/routes/webfinger.rs @@ -32,7 +32,7 @@ lazy_static! { async fn get_webfinger_response( info: Query, db: web::Data>>, -) -> Result { +) -> Result { let res = web::block(move || { let conn = db.get()?; @@ -84,6 +84,6 @@ async fn get_webfinger_response( }) .await .map(|json| HttpResponse::Ok().json(json)) - .map_err(|_| HttpResponse::InternalServerError())?; + .map_err(|e| ErrorBadRequest(e))?; Ok(res) } diff --git a/server/src/routes/websocket.rs b/server/src/routes/websocket.rs index 808859451..48b7d08fb 100644 --- a/server/src/routes/websocket.rs +++ b/server/src/routes/websocket.rs @@ -1,6 +1,5 @@ use super::*; use crate::websocket::server::*; -use actix_web::{Error, Result}; /// How often heartbeat pings are sent const HEARTBEAT_INTERVAL: Duration = Duration::from_secs(5); @@ -120,7 +119,7 @@ impl StreamHandler> for WSSession { .then(|res, _, ctx| { match res { Ok(Ok(res)) => ctx.text(res), - Ok(Err(e)) => match e {}, + Ok(Err(_)) => {} Err(e) => error!("{}", &e), } actix::fut::ready(()) From 809cc1049e5c50e7352f2df3d6fe1829558fdec7 Mon Sep 17 00:00:00 2001 From: Dessalines Date: Tue, 21 Apr 2020 16:48:54 -0400 Subject: [PATCH 2/3] Fixing closures. --- server/src/routes/api.rs | 2 +- server/src/routes/feeds.rs | 4 ++-- server/src/routes/nodeinfo.rs | 2 +- server/src/routes/webfinger.rs | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/server/src/routes/api.rs b/server/src/routes/api.rs index 5ff53e270..1565afb80 100644 --- a/server/src/routes/api.rs +++ b/server/src/routes/api.rs @@ -158,7 +158,7 @@ where let res = web::block(move || oper.perform(db.get_ref().to_owned(), Some(ws_info))) .await .map(|json| HttpResponse::Ok().json(json)) - .map_err(|e| ErrorBadRequest(e))?; + .map_err(ErrorBadRequest)?; Ok(res) } diff --git a/server/src/routes/feeds.rs b/server/src/routes/feeds.rs index b36755358..815953c55 100644 --- a/server/src/routes/feeds.rs +++ b/server/src/routes/feeds.rs @@ -39,7 +39,7 @@ async fn get_all_feed( .content_type("application/rss+xml") .body(rss) }) - .map_err(|e| ErrorBadRequest(e))?; + .map_err(ErrorBadRequest)?; Ok(res) } @@ -100,7 +100,7 @@ async fn get_feed( .content_type("application/rss+xml") .body(rss) }) - .map_err(|e| ErrorBadRequest(e))?; + .map_err(ErrorBadRequest)?; Ok(res) } diff --git a/server/src/routes/nodeinfo.rs b/server/src/routes/nodeinfo.rs index 97863271b..9ade80283 100644 --- a/server/src/routes/nodeinfo.rs +++ b/server/src/routes/nodeinfo.rs @@ -49,7 +49,7 @@ async fn node_info( }) .await .map(|json| HttpResponse::Ok().json(json)) - .map_err(|e| ErrorBadRequest(e))?; + .map_err(ErrorBadRequest)?; Ok(res) } diff --git a/server/src/routes/webfinger.rs b/server/src/routes/webfinger.rs index f39c6b67b..eaab1cb8c 100644 --- a/server/src/routes/webfinger.rs +++ b/server/src/routes/webfinger.rs @@ -84,6 +84,6 @@ async fn get_webfinger_response( }) .await .map(|json| HttpResponse::Ok().json(json)) - .map_err(|e| ErrorBadRequest(e))?; + .map_err(ErrorBadRequest)?; Ok(res) } From 6b58080067dbad22bfc08516ff3410a43874ca7e Mon Sep 17 00:00:00 2001 From: Dessalines Date: Tue, 21 Apr 2020 17:57:20 -0400 Subject: [PATCH 3/3] Adding websocket blocking. --- server/src/websocket/server.rs | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/server/src/websocket/server.rs b/server/src/websocket/server.rs index 0e7c1a4af..da5098e91 100644 --- a/server/src/websocket/server.rs +++ b/server/src/websocket/server.rs @@ -896,10 +896,18 @@ where let data = data.to_string(); let op2 = op.clone(); + let fut = async move { - let parsed_data: Data = serde_json::from_str(&data)?; - let res = Oper::new(parsed_data).perform(pool, Some(ws_info))?; - to_json_string(&op, &res) + actix_web::web::block(move || { + let parsed_data: Data = serde_json::from_str(&data)?; + let res = Oper::new(parsed_data).perform(pool, Some(ws_info))?; + to_json_string(&op, &res) + }) + .await + .map_err(|e| match e { + actix_web::error::BlockingError::Error(e) => e, + _ => APIError::err("Operation canceled").into(), + }) }; match op2 {