diff --git a/Cargo.lock b/Cargo.lock index 4770ca1..20b20a1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5766,6 +5766,7 @@ dependencies = [ "actix-files", "actix-session 0.5.0", "actix-web", + "actix-web-lab", "dotenv", "env_logger", "log", diff --git a/auth/redis-session/src/main.rs b/auth/redis-session/src/main.rs index a897da8..07b144d 100644 --- a/auth/redis-session/src/main.rs +++ b/auth/redis-session/src/main.rs @@ -13,7 +13,7 @@ use actix_web::{ }; use serde::{Deserialize, Serialize}; -#[derive(Serialize, Deserialize, Debug, PartialEq)] +#[derive(Serialize, Deserialize, Debug, PartialEq, Eq)] pub struct IndexResponse { user_id: Option, counter: i32, diff --git a/basics/basics/src/main.rs b/basics/basics/src/main.rs index 33a0edd..6f47aae 100644 --- a/basics/basics/src/main.rs +++ b/basics/basics/src/main.rs @@ -43,7 +43,9 @@ async fn welcome(req: HttpRequest, session: Session) -> Result { async fn default_handler(req_method: Method) -> Result { match req_method { Method::GET => { - let file = NamedFile::open("static/404.html")?.set_status_code(StatusCode::NOT_FOUND); + let file = NamedFile::open("static/404.html")? + .customize() + .with_status(StatusCode::NOT_FOUND); Ok(Either::Left(file)) } _ => Ok(Either::Right(HttpResponse::MethodNotAllowed().finish())), diff --git a/basics/todo/Cargo.toml b/basics/todo/Cargo.toml index fcb609b..2722635 100644 --- a/basics/todo/Cargo.toml +++ b/basics/todo/Cargo.toml @@ -4,9 +4,10 @@ version = "1.0.0" edition = "2021" [dependencies] -actix-web = "4" actix-files = "0.6" actix-session = "0.5" +actix-web = "4" +actix-web-lab = "0.16" dotenv = "0.15" env_logger = "0.9" diff --git a/basics/todo/src/api.rs b/basics/todo/src/api.rs index 173e48a..ae8900d 100644 --- a/basics/todo/src/api.rs +++ b/basics/todo/src/api.rs @@ -1,8 +1,9 @@ use actix_files::NamedFile; use actix_session::Session; use actix_web::{ - dev, error, http, middleware::ErrorHandlerResponse, web, Error, HttpResponse, Result, + dev, error, middleware::ErrorHandlerResponse, web, Error, HttpResponse, Responder, Result, }; +use actix_web_lab::web::Redirect; use serde::Deserialize; use sqlx::SqlitePool; use tera::{Context, Tera}; @@ -24,8 +25,7 @@ pub async fn index( let mut context = Context::new(); context.insert("tasks", &tasks); - //Session is set during operations on other endpoints - //that can redirect to index + // Session is set during operations on other endpoints that can redirect to index if let Some(flash) = session::get_flash(&session)? { context.insert("msg", &(flash.kind, flash.message)); session::clear_flash(&session); @@ -38,7 +38,7 @@ pub async fn index( Ok(HttpResponse::Ok().body(rendered)) } -#[derive(Deserialize)] +#[derive(Debug, Deserialize)] pub struct CreateForm { description: String, } @@ -47,25 +47,27 @@ pub async fn create( params: web::Form, pool: web::Data, session: Session, -) -> Result { +) -> Result { if params.description.is_empty() { session::set_flash(&session, FlashMessage::error("Description cannot be empty"))?; - Ok(redirect_to("/")) + Ok(Redirect::to("/")) } else { db::create_task(params.into_inner().description, &pool) .await .map_err(error::ErrorInternalServerError)?; + session::set_flash(&session, FlashMessage::success("Task successfully added"))?; - Ok(redirect_to("/")) + + Ok(Redirect::to("/")) } } -#[derive(Deserialize)] +#[derive(Debug, Deserialize)] pub struct UpdateParams { id: i32, } -#[derive(Deserialize)] +#[derive(Debug, Deserialize)] pub struct UpdateForm { _method: String, } @@ -75,65 +77,71 @@ pub async fn update( params: web::Path, form: web::Form, session: Session, -) -> Result { - match form._method.as_ref() { - "put" => toggle(db, params).await, - "delete" => delete(db, params, session).await, +) -> Result { + Ok(Redirect::to(match form._method.as_ref() { + "put" => toggle(db, params).await?, + "delete" => delete(db, params, session).await?, unsupported_method => { let msg = format!("Unsupported HTTP method: {unsupported_method}"); - Err(error::ErrorBadRequest(msg)) + return Err(error::ErrorBadRequest(msg)); } - } + })) } async fn toggle( pool: web::Data, params: web::Path, -) -> Result { +) -> Result<&'static str, Error> { db::toggle_task(params.id, &pool) .await .map_err(error::ErrorInternalServerError)?; - Ok(redirect_to("/")) + + Ok("/") } async fn delete( pool: web::Data, params: web::Path, session: Session, -) -> Result { +) -> Result<&'static str, Error> { db::delete_task(params.id, &pool) .await .map_err(error::ErrorInternalServerError)?; - session::set_flash(&session, FlashMessage::success("Task was deleted."))?; - Ok(redirect_to("/")) -} -fn redirect_to(location: &str) -> HttpResponse { - HttpResponse::Found() - .append_header((http::header::LOCATION, location)) - .finish() + session::set_flash(&session, FlashMessage::success("Task was deleted."))?; + + Ok("/") } pub fn bad_request(res: dev::ServiceResponse) -> Result> { let new_resp = NamedFile::open("static/errors/400.html")? - .set_status_code(res.status()) - .into_response(res.request()) + .customize() + .with_status(res.status()) + .respond_to(res.request()) + .map_into_boxed_body() .map_into_right_body(); + Ok(ErrorHandlerResponse::Response(res.into_response(new_resp))) } pub fn not_found(res: dev::ServiceResponse) -> Result> { let new_resp = NamedFile::open("static/errors/404.html")? - .set_status_code(res.status()) - .into_response(res.request()) + .customize() + .with_status(res.status()) + .respond_to(res.request()) + .map_into_boxed_body() .map_into_right_body(); + Ok(ErrorHandlerResponse::Response(res.into_response(new_resp))) } pub fn internal_server_error(res: dev::ServiceResponse) -> Result> { let new_resp = NamedFile::open("static/errors/500.html")? - .set_status_code(res.status()) - .into_response(res.request()) + .customize() + .with_status(res.status()) + .respond_to(res.request()) + .map_into_boxed_body() .map_into_right_body(); + Ok(ErrorHandlerResponse::Response(res.into_response(new_resp))) } diff --git a/databases/mongodb/src/model.rs b/databases/mongodb/src/model.rs index 1e39dc3..40018a4 100644 --- a/databases/mongodb/src/model.rs +++ b/databases/mongodb/src/model.rs @@ -1,6 +1,6 @@ use serde::{Deserialize, Serialize}; -#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)] +#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)] pub struct User { pub first_name: String, pub last_name: String, diff --git a/json/jsonrpc/src/main.rs b/json/jsonrpc/src/main.rs index 91388bc..f4ad7ec 100644 --- a/json/jsonrpc/src/main.rs +++ b/json/jsonrpc/src/main.rs @@ -62,13 +62,14 @@ async fn rpc_select( if params.len() != 1 || !params[0].is_u64() { return Err(convention::ErrorData::std(-32602)); } - match app_state + + let fut = app_state .network .read() .unwrap() - .wait(params[0].as_u64().unwrap()) - .await - { + .wait(params[0].as_u64().unwrap()); + + match fut.await { Ok(ok) => Ok(Value::from(ok)), Err(e) => Err(convention::ErrorData::new(500, &format!("{e:?}")[..])), } diff --git a/protobuf/src/main.rs b/protobuf/src/main.rs index 3fa291a..10b56c0 100644 --- a/protobuf/src/main.rs +++ b/protobuf/src/main.rs @@ -2,7 +2,7 @@ use actix_protobuf::{ProtoBuf, ProtoBufResponseBuilder as _}; use actix_web::{middleware, web, App, HttpResponse, HttpServer, Result}; use prost_derive::Message; -#[derive(Clone, PartialEq, Message)] +#[derive(Clone, PartialEq, Eq, Message)] pub struct MyObj { #[prost(int32, tag = "1")] pub number: i32, diff --git a/websockets/chat-tcp/src/server.rs b/websockets/chat-tcp/src/server.rs index a23b57b..2ccd628 100644 --- a/websockets/chat-tcp/src/server.rs +++ b/websockets/chat-tcp/src/server.rs @@ -82,7 +82,7 @@ impl ChatServer { for id in sessions { if *id != skip_id { if let Some(addr) = self.sessions.get(id) { - let _ = addr.do_send(session::Message(message.to_owned())); + addr.do_send(session::Message(message.to_owned())); } } } diff --git a/websockets/chat/src/server.rs b/websockets/chat/src/server.rs index 6861cb1..87be846 100644 --- a/websockets/chat/src/server.rs +++ b/websockets/chat/src/server.rs @@ -97,7 +97,7 @@ impl ChatServer { for id in sessions { if *id != skip_id { if let Some(addr) = self.sessions.get(id) { - let _ = addr.do_send(Message(message.to_owned())); + addr.do_send(Message(message.to_owned())); } } }