Update to latest actix-web beta

This commit is contained in:
LukeMathWalker 2021-07-11 16:10:55 +01:00
parent a522e13ab9
commit c8913d195d
4 changed files with 273 additions and 261 deletions

458
Cargo.lock generated

File diff suppressed because it is too large Load diff

View file

@ -15,10 +15,9 @@ name = "zero2prod"
[dependencies] [dependencies]
actix-http = "=3.0.0-beta.8" actix-http = "=3.0.0-beta.8"
actix-web = "=4.0.0-beta.8" actix-web = "=4.0.0-beta.8"
serde = "1.0.115" serde = "1.0.115"
config = { version = "0.10.1", default-features = false, features = ["yaml"] } config = { version = "0.10.1", default-features = false, features = ["yaml"] }
sqlx = { version = "0.5.1", default-features = false, features = [ "runtime-actix-rustls", "macros", "postgres", "uuid", "chrono", "migrate", "offline"] } sqlx = { version = "0.5.5", default-features = false, features = [ "runtime-actix-rustls", "macros", "postgres", "uuid", "chrono", "migrate"] }
uuid = { version = "0.8.1", features = ["v4"] } uuid = { version = "0.8.1", features = ["v4"] }
chrono = "0.4.15" chrono = "0.4.15"
reqwest = { version = "0.11", default-features = false, features = ["json", "rustls-tls"] } reqwest = { version = "0.11", default-features = false, features = ["json", "rustls-tls"] }

View file

@ -38,36 +38,41 @@ pub async fn subscribe(
pool: web::Data<PgPool>, pool: web::Data<PgPool>,
email_client: web::Data<EmailClient>, email_client: web::Data<EmailClient>,
base_url: web::Data<ApplicationBaseUrl>, base_url: web::Data<ApplicationBaseUrl>,
) -> Result<HttpResponse, HttpResponse> { ) -> HttpResponse {
let new_subscriber = form let new_subscriber = match form.0.try_into() {
.0 Ok(form) => form,
.try_into() Err(_) => return HttpResponse::BadRequest().finish(),
.map_err(|_| HttpResponse::BadRequest().finish())?; };
let mut transaction = pool let mut transaction = match pool.begin().await {
.begin() Ok(transaction) => transaction,
.await Err(_) => return HttpResponse::InternalServerError().finish(),
.map_err(|_| HttpResponse::InternalServerError().finish())?; };
let subscriber_id = insert_subscriber(&mut transaction, &new_subscriber) let subscriber_id = match insert_subscriber(&mut transaction, &new_subscriber).await {
.await Ok(subscriber_id) => subscriber_id,
.map_err(|_| HttpResponse::InternalServerError().finish())?; Err(_) => return HttpResponse::InternalServerError().finish(),
// We are swallowing the error for the time being. };
let subscription_token = generate_subscription_token(); let subscription_token = generate_subscription_token();
store_token(&mut transaction, subscriber_id, &subscription_token) if store_token(&mut transaction, subscriber_id, &subscription_token)
.await .await
.map_err(|_| HttpResponse::InternalServerError().finish())?; .is_err()
transaction {
.commit() return HttpResponse::InternalServerError().finish();
.await }
.map_err(|_| HttpResponse::InternalServerError().finish())?; if transaction.commit().await.is_err() {
send_confirmation_email( return HttpResponse::InternalServerError().finish();
}
if send_confirmation_email(
&email_client, &email_client,
new_subscriber, new_subscriber,
&base_url.0, &base_url.0,
&subscription_token, &subscription_token,
) )
.await .await
.map_err(|_| HttpResponse::InternalServerError().finish())?; .is_err()
Ok(HttpResponse::Ok().finish()) {
return HttpResponse::InternalServerError().finish();
}
HttpResponse::Ok().finish()
} }
fn generate_subscription_token() -> String { fn generate_subscription_token() -> String {

View file

@ -1,5 +1,5 @@
use actix_web::{web, HttpResponse}; use actix_web::{web, HttpResponse};
use sqlx::PgPool; use sqlx::{PgPool};
use uuid::Uuid; use uuid::Uuid;
#[derive(serde::Deserialize)] #[derive(serde::Deserialize)]
@ -8,21 +8,19 @@ pub struct Parameters {
} }
#[tracing::instrument(name = "Confirm a pending subscriber", skip(parameters, pool))] #[tracing::instrument(name = "Confirm a pending subscriber", skip(parameters, pool))]
pub async fn confirm( pub async fn confirm(parameters: web::Query<Parameters>, pool: web::Data<PgPool>) -> HttpResponse {
parameters: web::Query<Parameters>, let id = match get_subscriber_id_from_token(&pool, &parameters.subscription_token).await {
pool: web::Data<PgPool>, Ok(id) => id,
) -> Result<HttpResponse, HttpResponse> { Err(_) => return HttpResponse::InternalServerError().finish(),
let id = get_subscriber_id_from_token(&pool, &parameters.subscription_token) };
.await
.map_err(|_| HttpResponse::InternalServerError().finish())?;
match id { match id {
// Non-existing token! // Non-existing token!
None => Err(HttpResponse::Unauthorized().finish()), None => HttpResponse::Unauthorized().finish(),
Some(subscriber_id) => { Some(subscriber_id) => {
confirm_subscriber(&pool, subscriber_id) if confirm_subscriber(&pool, subscriber_id).await.is_err() {
.await return HttpResponse::InternalServerError().finish();
.map_err(|_| HttpResponse::InternalServerError().finish())?; }
Ok(HttpResponse::Ok().finish()) HttpResponse::Ok().finish()
} }
} }
} }