mirror of
https://github.com/actix/examples
synced 2025-02-08 20:06:07 +01:00
Merge pull request #236 from Dowwie/master
fixed formatting for async_pg
This commit is contained in:
commit
050b3a2b1e
@ -1,4 +1,3 @@
|
|||||||
|
|
||||||
mod models {
|
mod models {
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use tokio_pg_mapper_derive::PostgresMapper;
|
use tokio_pg_mapper_derive::PostgresMapper;
|
||||||
@ -9,118 +8,110 @@ mod models {
|
|||||||
pub email: String,
|
pub email: String,
|
||||||
pub first_name: String,
|
pub first_name: String,
|
||||||
pub last_name: String,
|
pub last_name: String,
|
||||||
pub username: String
|
pub username: String,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
mod errors {
|
mod errors {
|
||||||
use actix_web::{HttpResponse, ResponseError};
|
use actix_web::{HttpResponse, ResponseError};
|
||||||
use derive_more::{Display, From};
|
use deadpool_postgres::PoolError;
|
||||||
use deadpool_postgres::PoolError;
|
use derive_more::{Display, From};
|
||||||
use tokio_postgres::error::Error as PGError;
|
use tokio_pg_mapper::Error as PGMError;
|
||||||
use tokio_pg_mapper::Error as PGMError;
|
use tokio_postgres::error::Error as PGError;
|
||||||
|
|
||||||
|
#[derive(Display, From, Debug)]
|
||||||
|
pub enum MyError {
|
||||||
|
NotFound,
|
||||||
|
PGError(PGError),
|
||||||
|
PGMError(PGMError),
|
||||||
|
PoolError(PoolError),
|
||||||
|
}
|
||||||
|
impl std::error::Error for MyError {}
|
||||||
|
|
||||||
#[derive(Display, From, Debug)]
|
impl ResponseError for MyError {
|
||||||
pub enum MyError {
|
fn error_response(&self) -> HttpResponse {
|
||||||
NotFound,
|
match *self {
|
||||||
PGError(PGError),
|
MyError::NotFound => HttpResponse::NotFound().finish(),
|
||||||
PGMError(PGMError),
|
_ => HttpResponse::InternalServerError().finish(),
|
||||||
PoolError(PoolError)
|
}
|
||||||
}
|
}
|
||||||
impl std::error::Error for MyError {}
|
}
|
||||||
|
|
||||||
impl ResponseError for MyError {
|
|
||||||
fn error_response(&self) -> HttpResponse {
|
|
||||||
match *self {
|
|
||||||
MyError::NotFound => HttpResponse::NotFound().finish(),
|
|
||||||
_ => HttpResponse::InternalServerError().finish()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
mod db {
|
mod db {
|
||||||
use crate::{errors::MyError, models::User};
|
use crate::{errors::MyError, models::User};
|
||||||
use deadpool_postgres::Client;
|
use deadpool_postgres::Client;
|
||||||
use tokio_pg_mapper::FromTokioPostgresRow;
|
use tokio_pg_mapper::FromTokioPostgresRow;
|
||||||
|
|
||||||
|
pub async fn add_user(client: &Client, user_info: User) -> Result<User, MyError> {
|
||||||
|
let _stmt = include_str!("../sql/add_user.sql");
|
||||||
|
let _stmt = _stmt.replace("$table_fields", &User::sql_table_fields());
|
||||||
|
let stmt = client.prepare(&_stmt).await.unwrap();
|
||||||
|
|
||||||
pub async fn add_user(client: &Client, user_info: User) -> Result<User, MyError> {
|
client
|
||||||
let _stmt = include_str!("../sql/add_user.sql");
|
.query(
|
||||||
let _stmt = _stmt.replace("$table_fields", &User::sql_table_fields());
|
&stmt,
|
||||||
let stmt = client.prepare(&_stmt)
|
&[
|
||||||
.await
|
&user_info.email,
|
||||||
.unwrap();
|
&user_info.first_name,
|
||||||
|
&user_info.last_name,
|
||||||
client.query(&stmt,
|
&user_info.username,
|
||||||
&[&user_info.email,
|
],
|
||||||
&user_info.first_name,
|
)
|
||||||
&user_info.last_name,
|
.await?
|
||||||
&user_info.username
|
.iter()
|
||||||
])
|
.map(|row| User::from_row_ref(row).unwrap())
|
||||||
.await?
|
.collect::<Vec<User>>()
|
||||||
.iter()
|
.pop()
|
||||||
.map(|row| User::from_row_ref(row).unwrap())
|
.ok_or(MyError::NotFound) // more applicable for SELECTs
|
||||||
.collect::<Vec<User>>()
|
}
|
||||||
.pop()
|
|
||||||
.ok_or(MyError::NotFound) // more applicable for SELECTs
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
mod handlers {
|
mod handlers {
|
||||||
use actix_web::{HttpResponse, web, Error};
|
|
||||||
use deadpool_postgres::{Client, Pool};
|
|
||||||
use crate::{db, errors::MyError, models::User};
|
use crate::{db, errors::MyError, models::User};
|
||||||
|
use actix_web::{web, Error, HttpResponse};
|
||||||
|
use deadpool_postgres::{Client, Pool};
|
||||||
|
|
||||||
|
pub async fn add_user(
|
||||||
|
user: web::Json<User>,
|
||||||
|
db_pool: web::Data<Pool>,
|
||||||
|
) -> Result<HttpResponse, Error> {
|
||||||
|
let user_info: User = user.into_inner();
|
||||||
|
|
||||||
pub async fn add_user(user: web::Json<User>, db_pool: web::Data<Pool>)
|
let client: Client =
|
||||||
-> Result<HttpResponse, Error> {
|
db_pool.get().await.map_err(|err| MyError::PoolError(err))?;
|
||||||
|
|
||||||
let user_info: User = user.into_inner();
|
let new_user = db::add_user(&client, user_info).await?;
|
||||||
|
|
||||||
let client: Client =
|
Ok(HttpResponse::Ok().json(new_user))
|
||||||
db_pool.get()
|
}
|
||||||
.await
|
|
||||||
.map_err(|err| MyError::PoolError(err))?;
|
|
||||||
|
|
||||||
let new_user = db::add_user(&client, user_info).await?;
|
|
||||||
|
|
||||||
Ok(HttpResponse::Ok().json(new_user))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
use actix_web::{web, App, HttpServer};
|
||||||
use actix_web::{App, HttpServer, web};
|
use deadpool_postgres::{Manager, Pool};
|
||||||
use deadpool_postgres::{Pool, Manager};
|
|
||||||
use handlers::add_user;
|
use handlers::add_user;
|
||||||
use tokio_postgres::{Config, NoTls};
|
use tokio_postgres::{Config, NoTls};
|
||||||
|
|
||||||
|
|
||||||
#[actix_rt::main]
|
#[actix_rt::main]
|
||||||
async fn main() -> std::io::Result<()> {
|
async fn main() -> std::io::Result<()> {
|
||||||
const SERVER_ADDR: &str = "127.0.0.1:8080";
|
const SERVER_ADDR: &str = "127.0.0.1:8080";
|
||||||
|
|
||||||
let pg_config = "postgres://test_user:testing@127.0.0.1:5432/testing_db"
|
let pg_config = "postgres://test_user:testing@127.0.0.1:5432/testing_db"
|
||||||
.parse::<Config>()
|
.parse::<Config>()
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
let pool = Pool::new(
|
let pool = Pool::new(
|
||||||
Manager::new(pg_config, NoTls),
|
Manager::new(pg_config, NoTls),
|
||||||
16 // # of connections in pool
|
16, // # of connections in pool
|
||||||
);
|
);
|
||||||
|
|
||||||
|
let server = HttpServer::new(move || {
|
||||||
let server = HttpServer::new(move ||
|
App::new()
|
||||||
App::new()
|
.data(pool.clone())
|
||||||
.data(pool.clone())
|
.service(web::resource("/users").route(web::post().to(add_user)))
|
||||||
.service(web::resource("/users").route(web::post().to(add_user)))
|
})
|
||||||
)
|
.bind(SERVER_ADDR)?
|
||||||
.bind(SERVER_ADDR)?
|
.run();
|
||||||
.run();
|
|
||||||
println!("Server running at http://{}/", SERVER_ADDR);
|
println!("Server running at http://{}/", SERVER_ADDR);
|
||||||
|
|
||||||
server.await
|
server.await
|
||||||
|
Loading…
x
Reference in New Issue
Block a user