1
0
mirror of https://github.com/actix/examples synced 2025-06-28 09:50:36 +02:00

reenable uds example; cargo fmt

This commit is contained in:
Nikolay Kim
2019-07-18 18:03:19 +06:00
parent 7525903fe6
commit 44053504ad
11 changed files with 216 additions and 146 deletions

View File

@ -1,6 +1,7 @@
use actix_identity::Identity;
use actix_web::{
dev::Payload, error::BlockingError, web, Error, FromRequest, HttpRequest, HttpResponse,
dev::Payload, error::BlockingError, web, Error, FromRequest, HttpRequest,
HttpResponse,
};
use diesel::prelude::*;
use diesel::PgConnection;

View File

@ -11,8 +11,8 @@ static ref API_KEY: String = std::env::var("SPARKPOST_API_KEY").expect("SPARKPOS
pub fn send_invitation(invitation: &Invitation) -> Result<(), ServiceError> {
let tm = Transmission::new_eu(API_KEY.as_str());
let sending_email =
std::env::var("SENDING_EMAIL_ADDRESS").expect("SENDING_EMAIL_ADDRESS must be set");
let sending_email = std::env::var("SENDING_EMAIL_ADDRESS")
.expect("SENDING_EMAIL_ADDRESS must be set");
// new email message with sender name and email
let mut email = Message::new(EmailAddress::new(sending_email, "Let's Organise"));

View File

@ -22,10 +22,12 @@ impl ResponseError for ServiceError {
match self {
ServiceError::InternalServerError => HttpResponse::InternalServerError()
.json("Internal Server Error, Please try later"),
ServiceError::BadRequest(ref message) => HttpResponse::BadRequest()
.json(message),
ServiceError::Unauthorized => HttpResponse::Unauthorized()
.json("Unauthorized"),
ServiceError::BadRequest(ref message) => {
HttpResponse::BadRequest().json(message)
}
ServiceError::Unauthorized => {
HttpResponse::Unauthorized().json("Unauthorized")
}
}
}
}
@ -45,7 +47,8 @@ impl From<DBError> for ServiceError {
match error {
DBError::DatabaseError(kind, info) => {
if let DatabaseErrorKind::UniqueViolation = kind {
let message = info.details().unwrap_or_else(|| info.message()).to_string();
let message =
info.details().unwrap_or_else(|| info.message()).to_string();
return ServiceError::BadRequest(message);
}
ServiceError::InternalServerError

View File

@ -16,15 +16,15 @@ pub fn post_invitation(
pool: web::Data<Pool>,
) -> impl Future<Item = HttpResponse, Error = ServiceError> {
// run diesel blocking code
web::block(move || create_invitation(invitation_data.into_inner().email, pool)).then(|res| {
match res {
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),
},
}
})
},
)
}
fn create_invitation(
@ -36,10 +36,13 @@ fn create_invitation(
}
/// Diesel query
fn query(eml: String, pool: web::Data<Pool>) -> Result<Invitation, crate::errors::ServiceError> {
fn query(
eml: String,
pool: web::Data<Pool>,
) -> Result<Invitation, crate::errors::ServiceError> {
use crate::schema::invitations::dsl::invitations;
let new_invitation : Invitation = eml.into();
let new_invitation: Invitation = eml.into();
let conn: &PgConnection = &pool.get().unwrap();
let inserted_invitation = diesel::insert_into(invitations)

View File

@ -31,7 +31,8 @@ fn main() -> std::io::Result<()> {
let pool: models::Pool = r2d2::Pool::builder()
.build(manager)
.expect("Failed to create pool.");
let domain: String = std::env::var("DOMAIN").unwrap_or_else(|_| "localhost".to_string());
let domain: String =
std::env::var("DOMAIN").unwrap_or_else(|_| "localhost".to_string());
// Start http server
HttpServer::new(move || {
@ -51,13 +52,13 @@ 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("/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("/register/{invitation_id}").route(
web::post().to_async(register_handler::register_user),
),
)
.service(
web::resource("/auth")

View File

@ -31,8 +31,10 @@ pub struct Invitation {
}
// any type that implements Into<String> can be used to create Invitation
impl<T> From<T> for Invitation where
T: Into<String> {
impl<T> From<T> for Invitation
where
T: Into<String>,
{
fn from(email: T) -> Self {
Invitation {
id: uuid::Uuid::new_v4(),