mirror of
https://github.com/actix/examples
synced 2025-06-28 18:00:37 +02:00
upgrade to 2.0 alpha.3
This commit is contained in:
@ -1,3 +1,5 @@
|
||||
use std::pin::Pin;
|
||||
|
||||
use actix_identity::Identity;
|
||||
use actix_web::{
|
||||
dev::Payload, error::BlockingError, web, Error, FromRequest, HttpRequest,
|
||||
@ -5,7 +7,7 @@ use actix_web::{
|
||||
};
|
||||
use diesel::prelude::*;
|
||||
use diesel::PgConnection;
|
||||
use futures::Future;
|
||||
use futures::future::Future;
|
||||
|
||||
use crate::errors::ServiceError;
|
||||
use crate::models::{Pool, SlimUser, User};
|
||||
@ -24,43 +26,47 @@ pub type LoggedUser = SlimUser;
|
||||
impl FromRequest for LoggedUser {
|
||||
type Config = ();
|
||||
type Error = Error;
|
||||
type Future = Result<LoggedUser, Error>;
|
||||
type Future = Pin<Box<dyn Future<Output = Result<LoggedUser, Error>>>>;
|
||||
|
||||
fn from_request(req: &HttpRequest, pl: &mut Payload) -> Self::Future {
|
||||
if let Some(identity) = Identity::from_request(req, pl)?.identity() {
|
||||
let user: LoggedUser = serde_json::from_str(&identity)?;
|
||||
return Ok(user);
|
||||
}
|
||||
Err(ServiceError::Unauthorized.into())
|
||||
let fut = Identity::from_request(req, pl);
|
||||
|
||||
Box::pin(async move {
|
||||
if let Some(identity) = fut.await?.identity() {
|
||||
let user: LoggedUser = serde_json::from_str(&identity)?;
|
||||
return Ok(user);
|
||||
};
|
||||
Err(ServiceError::Unauthorized.into())
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
pub fn logout(id: Identity) -> HttpResponse {
|
||||
pub async fn logout(id: Identity) -> HttpResponse {
|
||||
id.forget();
|
||||
HttpResponse::Ok().finish()
|
||||
}
|
||||
|
||||
pub fn login(
|
||||
pub async fn login(
|
||||
auth_data: web::Json<AuthData>,
|
||||
id: Identity,
|
||||
pool: web::Data<Pool>,
|
||||
) -> impl Future<Item = HttpResponse, Error = ServiceError> {
|
||||
web::block(move || query(auth_data.into_inner(), pool)).then(
|
||||
move |res: Result<SlimUser, BlockingError<ServiceError>>| match res {
|
||||
Ok(user) => {
|
||||
let user_string = serde_json::to_string(&user).unwrap();
|
||||
id.remember(user_string);
|
||||
Ok(HttpResponse::Ok().finish())
|
||||
}
|
||||
Err(err) => match err {
|
||||
BlockingError::Error(service_error) => Err(service_error),
|
||||
BlockingError::Canceled => Err(ServiceError::InternalServerError),
|
||||
},
|
||||
) -> Result<HttpResponse, ServiceError> {
|
||||
let res = web::block(move || query(auth_data.into_inner(), pool)).await;
|
||||
|
||||
match res {
|
||||
Ok(user) => {
|
||||
let user_string = serde_json::to_string(&user).unwrap();
|
||||
id.remember(user_string);
|
||||
Ok(HttpResponse::Ok().finish())
|
||||
}
|
||||
Err(err) => match err {
|
||||
BlockingError::Error(service_error) => Err(service_error),
|
||||
BlockingError::Canceled => Err(ServiceError::InternalServerError),
|
||||
},
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_me(logged_user: LoggedUser) -> HttpResponse {
|
||||
pub async fn get_me(logged_user: LoggedUser) -> HttpResponse {
|
||||
HttpResponse::Ok().json(logged_user)
|
||||
}
|
||||
/// Diesel query
|
||||
|
@ -1,6 +1,5 @@
|
||||
use actix_web::{error::BlockingError, web, HttpResponse};
|
||||
use diesel::{prelude::*, PgConnection};
|
||||
use futures::Future;
|
||||
|
||||
use crate::email_service::send_invitation;
|
||||
use crate::errors::ServiceError;
|
||||
@ -11,20 +10,22 @@ pub struct InvitationData {
|
||||
pub email: String,
|
||||
}
|
||||
|
||||
pub fn post_invitation(
|
||||
pub async fn post_invitation(
|
||||
invitation_data: web::Json<InvitationData>,
|
||||
pool: web::Data<Pool>,
|
||||
) -> impl Future<Item = HttpResponse, Error = ServiceError> {
|
||||
) -> Result<HttpResponse, ServiceError> {
|
||||
// run diesel blocking code
|
||||
web::block(move || create_invitation(invitation_data.into_inner().email, pool)).then(
|
||||
|res| match res {
|
||||
Ok(_) => Ok(HttpResponse::Ok().finish()),
|
||||
Err(err) => match err {
|
||||
BlockingError::Error(service_error) => Err(service_error),
|
||||
BlockingError::Canceled => Err(ServiceError::InternalServerError),
|
||||
},
|
||||
let res =
|
||||
web::block(move || create_invitation(invitation_data.into_inner().email, pool))
|
||||
.await;
|
||||
|
||||
match res {
|
||||
Ok(_) => Ok(HttpResponse::Ok().finish()),
|
||||
Err(err) => match err {
|
||||
BlockingError::Error(service_error) => Err(service_error),
|
||||
BlockingError::Canceled => Err(ServiceError::InternalServerError),
|
||||
},
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
fn create_invitation(
|
||||
|
@ -17,7 +17,8 @@ mod register_handler;
|
||||
mod schema;
|
||||
mod utils;
|
||||
|
||||
fn main() -> std::io::Result<()> {
|
||||
#[actix_rt::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
dotenv::dotenv().ok();
|
||||
std::env::set_var(
|
||||
"RUST_LOG",
|
||||
@ -52,22 +53,23 @@ fn main() -> std::io::Result<()> {
|
||||
// everything under '/api/' route
|
||||
.service(
|
||||
web::scope("/api")
|
||||
.service(web::resource("/invitation").route(
|
||||
web::post().to_async(invitation_handler::post_invitation),
|
||||
))
|
||||
.service(
|
||||
web::resource("/register/{invitation_id}").route(
|
||||
web::post().to_async(register_handler::register_user),
|
||||
),
|
||||
web::resource("/invitation")
|
||||
.route(web::post().to(invitation_handler::post_invitation)),
|
||||
)
|
||||
.service(
|
||||
web::resource("/register/{invitation_id}")
|
||||
.route(web::post().to(register_handler::register_user)),
|
||||
)
|
||||
.service(
|
||||
web::resource("/auth")
|
||||
.route(web::post().to_async(auth_handler::login))
|
||||
.route(web::post().to(auth_handler::login))
|
||||
.route(web::delete().to(auth_handler::logout))
|
||||
.route(web::get().to(auth_handler::get_me)),
|
||||
),
|
||||
)
|
||||
})
|
||||
.bind("127.0.0.1:3000")?
|
||||
.run()
|
||||
.start()
|
||||
.await
|
||||
}
|
||||
|
@ -1,6 +1,5 @@
|
||||
use actix_web::{error::BlockingError, web, HttpResponse};
|
||||
use diesel::prelude::*;
|
||||
use futures::Future;
|
||||
|
||||
use crate::errors::ServiceError;
|
||||
use crate::models::{Invitation, Pool, SlimUser, User};
|
||||
@ -11,25 +10,27 @@ pub struct UserData {
|
||||
pub password: String,
|
||||
}
|
||||
|
||||
pub fn register_user(
|
||||
pub async fn register_user(
|
||||
invitation_id: web::Path<String>,
|
||||
user_data: web::Json<UserData>,
|
||||
pool: web::Data<Pool>,
|
||||
) -> impl Future<Item = HttpResponse, Error = ServiceError> {
|
||||
web::block(move || {
|
||||
) -> Result<HttpResponse, ServiceError> {
|
||||
let res = web::block(move || {
|
||||
query(
|
||||
invitation_id.into_inner(),
|
||||
user_data.into_inner().password,
|
||||
pool,
|
||||
)
|
||||
})
|
||||
.then(|res| match res {
|
||||
.await;
|
||||
|
||||
match res {
|
||||
Ok(user) => Ok(HttpResponse::Ok().json(&user)),
|
||||
Err(err) => match err {
|
||||
BlockingError::Error(service_error) => Err(service_error),
|
||||
BlockingError::Canceled => Err(ServiceError::InternalServerError),
|
||||
},
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
fn query(
|
||||
|
Reference in New Issue
Block a user