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]
actix-http = "=3.0.0-beta.8"
actix-web = "=4.0.0-beta.8"
serde = "1.0.115"
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"] }
chrono = "0.4.15"
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>,
email_client: web::Data<EmailClient>,
base_url: web::Data<ApplicationBaseUrl>,
) -> Result<HttpResponse, HttpResponse> {
let new_subscriber = form
.0
.try_into()
.map_err(|_| HttpResponse::BadRequest().finish())?;
let mut transaction = pool
.begin()
.await
.map_err(|_| HttpResponse::InternalServerError().finish())?;
let subscriber_id = insert_subscriber(&mut transaction, &new_subscriber)
.await
.map_err(|_| HttpResponse::InternalServerError().finish())?;
// We are swallowing the error for the time being.
) -> HttpResponse {
let new_subscriber = match form.0.try_into() {
Ok(form) => form,
Err(_) => return HttpResponse::BadRequest().finish(),
};
let mut transaction = match pool.begin().await {
Ok(transaction) => transaction,
Err(_) => return HttpResponse::InternalServerError().finish(),
};
let subscriber_id = match insert_subscriber(&mut transaction, &new_subscriber).await {
Ok(subscriber_id) => subscriber_id,
Err(_) => return HttpResponse::InternalServerError().finish(),
};
let subscription_token = generate_subscription_token();
store_token(&mut transaction, subscriber_id, &subscription_token)
if store_token(&mut transaction, subscriber_id, &subscription_token)
.await
.map_err(|_| HttpResponse::InternalServerError().finish())?;
transaction
.commit()
.await
.map_err(|_| HttpResponse::InternalServerError().finish())?;
send_confirmation_email(
.is_err()
{
return HttpResponse::InternalServerError().finish();
}
if transaction.commit().await.is_err() {
return HttpResponse::InternalServerError().finish();
}
if send_confirmation_email(
&email_client,
new_subscriber,
&base_url.0,
&subscription_token,
)
.await
.map_err(|_| HttpResponse::InternalServerError().finish())?;
Ok(HttpResponse::Ok().finish())
.is_err()
{
return HttpResponse::InternalServerError().finish();
}
HttpResponse::Ok().finish()
}
fn generate_subscription_token() -> String {

View file

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