mirror of
https://github.com/actix/examples
synced 2025-06-28 18:00:37 +02:00
Restructure folders (#411)
This commit is contained in:
committed by
GitHub
parent
9db98162b2
commit
c3407627d0
3
database_interactions/pg/.gitignore
vendored
Normal file
3
database_interactions/pg/.gitignore
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
/target
|
||||
**/*.rs.bk
|
||||
.env
|
16
database_interactions/pg/Cargo.toml
Normal file
16
database_interactions/pg/Cargo.toml
Normal file
@ -0,0 +1,16 @@
|
||||
[package]
|
||||
name = "async_pg"
|
||||
version = "0.1.0"
|
||||
authors = ["dowwie <dkcdkg@gmail.com>"]
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
actix-web = "3"
|
||||
config = "0.10.1"
|
||||
deadpool-postgres = "0.5.0"
|
||||
derive_more = "0.99.2"
|
||||
dotenv = "0.15.0"
|
||||
serde = { version = "1.0.104", features = ["derive"] }
|
||||
tokio-pg-mapper = "0.1"
|
||||
tokio-pg-mapper-derive = "0.1"
|
||||
tokio-postgres = "0.5.1"
|
72
database_interactions/pg/README.md
Normal file
72
database_interactions/pg/README.md
Normal file
@ -0,0 +1,72 @@
|
||||
# async_pg example
|
||||
|
||||
## This example illustrates
|
||||
|
||||
- `tokio_postgres`
|
||||
- use of `tokio_pg_mapper` for postgres data mapping
|
||||
- `deadpool_postgres` for connection pooling
|
||||
- `dotenv` + `config` for configuration
|
||||
|
||||
## Instructions
|
||||
|
||||
1. Create database user
|
||||
|
||||
```shell
|
||||
createuser -P test_user
|
||||
```
|
||||
|
||||
Enter a password of your choice. The following instructions assume you
|
||||
used `testing` as password.
|
||||
|
||||
This step is **optional** and you can also use an existing database user
|
||||
for that. Just make sure to replace `test_user` by the database user
|
||||
of your choice in the following steps and change the `.env` file
|
||||
containing the configuration accordingly.
|
||||
|
||||
2. Create database
|
||||
|
||||
```shell
|
||||
createdb -O test_user testing_db
|
||||
```
|
||||
|
||||
3. Initialize database
|
||||
|
||||
```shell
|
||||
psql -f sql/schema.sql testing_db
|
||||
```
|
||||
|
||||
This step can be repeated and clears the database as it drops and
|
||||
recreates the schema `testing` which is used within the database.
|
||||
|
||||
4. Create `.env` file:
|
||||
|
||||
```ini
|
||||
SERVER_ADDR=127.0.0.1:8080
|
||||
PG.USER=test_user
|
||||
PG.PASSWORD=testing
|
||||
PG.HOST=127.0.0.1
|
||||
PG.PORT=5432
|
||||
PG.DBNAME=testing_db
|
||||
PG.POOL.MAX_SIZE=16
|
||||
```
|
||||
|
||||
5. Run the server:
|
||||
|
||||
```shell
|
||||
cargo run
|
||||
```
|
||||
|
||||
6. Using a different terminal send an HTTP POST request to the running server:
|
||||
|
||||
```shell
|
||||
echo '{"email": "ferris@thecrab.com", "first_name": "ferris", "last_name": "crab", "username": "ferreal"}' | http -f --json --print h POST http://127.0.0.1:8080/users
|
||||
```
|
||||
|
||||
**...or using curl...**
|
||||
|
||||
```shell
|
||||
curl -d '{"email": "ferris@thecrab.com", "first_name": "ferris", "last_name": "crab", "username": "ferreal"}' -H 'Content-Type: application/json' http://127.0.0.1:8080/users
|
||||
```
|
||||
|
||||
A unique constraint exists for username, so sending this request twice
|
||||
will return an internal server error (HTTP 500).
|
3
database_interactions/pg/sql/add_user.sql
Normal file
3
database_interactions/pg/sql/add_user.sql
Normal file
@ -0,0 +1,3 @@
|
||||
INSERT INTO testing.users(email, first_name, last_name, username)
|
||||
VALUES ($1, $2, $3, $4)
|
||||
RETURNING $table_fields;
|
1
database_interactions/pg/sql/get_users.sql
Normal file
1
database_interactions/pg/sql/get_users.sql
Normal file
@ -0,0 +1 @@
|
||||
SELECT $table_fields FROM testing.users;
|
11
database_interactions/pg/sql/schema.sql
Normal file
11
database_interactions/pg/sql/schema.sql
Normal file
@ -0,0 +1,11 @@
|
||||
DROP SCHEMA IF EXISTS testing CASCADE;
|
||||
CREATE SCHEMA testing;
|
||||
|
||||
CREATE TABLE testing.users (
|
||||
id BIGSERIAL PRIMARY KEY,
|
||||
email VARCHAR(200) NOT NULL,
|
||||
first_name VARCHAR(200) NOT NULL,
|
||||
last_name VARCHAR(200) NOT NULL,
|
||||
username VARCHAR(50) UNIQUE NOT NULL,
|
||||
UNIQUE (username)
|
||||
);
|
131
database_interactions/pg/src/main.rs
Normal file
131
database_interactions/pg/src/main.rs
Normal file
@ -0,0 +1,131 @@
|
||||
mod config {
|
||||
pub use ::config::ConfigError;
|
||||
use serde::Deserialize;
|
||||
#[derive(Deserialize)]
|
||||
pub struct Config {
|
||||
pub server_addr: String,
|
||||
pub pg: deadpool_postgres::Config,
|
||||
}
|
||||
impl Config {
|
||||
pub fn from_env() -> Result<Self, ConfigError> {
|
||||
let mut cfg = ::config::Config::new();
|
||||
cfg.merge(::config::Environment::new())?;
|
||||
cfg.try_into()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
mod models {
|
||||
use serde::{Deserialize, Serialize};
|
||||
use tokio_pg_mapper_derive::PostgresMapper;
|
||||
|
||||
#[derive(Deserialize, PostgresMapper, Serialize)]
|
||||
#[pg_mapper(table = "users")] // singular 'user' is a keyword..
|
||||
pub struct User {
|
||||
pub email: String,
|
||||
pub first_name: String,
|
||||
pub last_name: String,
|
||||
pub username: String,
|
||||
}
|
||||
}
|
||||
|
||||
mod errors {
|
||||
use actix_web::{HttpResponse, ResponseError};
|
||||
use deadpool_postgres::PoolError;
|
||||
use derive_more::{Display, From};
|
||||
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 {}
|
||||
|
||||
impl ResponseError for MyError {
|
||||
fn error_response(&self) -> HttpResponse {
|
||||
match *self {
|
||||
MyError::NotFound => HttpResponse::NotFound().finish(),
|
||||
MyError::PoolError(ref err) => {
|
||||
HttpResponse::InternalServerError().body(err.to_string())
|
||||
}
|
||||
_ => HttpResponse::InternalServerError().finish(),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
mod db {
|
||||
use crate::{errors::MyError, models::User};
|
||||
use deadpool_postgres::Client;
|
||||
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();
|
||||
|
||||
client
|
||||
.query(
|
||||
&stmt,
|
||||
&[
|
||||
&user_info.email,
|
||||
&user_info.first_name,
|
||||
&user_info.last_name,
|
||||
&user_info.username,
|
||||
],
|
||||
)
|
||||
.await?
|
||||
.iter()
|
||||
.map(|row| User::from_row_ref(row).unwrap())
|
||||
.collect::<Vec<User>>()
|
||||
.pop()
|
||||
.ok_or(MyError::NotFound) // more applicable for SELECTs
|
||||
}
|
||||
}
|
||||
|
||||
mod handlers {
|
||||
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();
|
||||
|
||||
let client: Client = db_pool.get().await.map_err(MyError::PoolError)?;
|
||||
|
||||
let new_user = db::add_user(&client, user_info).await?;
|
||||
|
||||
Ok(HttpResponse::Ok().json(new_user))
|
||||
}
|
||||
}
|
||||
|
||||
use actix_web::{web, App, HttpServer};
|
||||
use dotenv::dotenv;
|
||||
use handlers::add_user;
|
||||
use tokio_postgres::NoTls;
|
||||
|
||||
#[actix_web::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
dotenv().ok();
|
||||
|
||||
let config = crate::config::Config::from_env().unwrap();
|
||||
let pool = config.pg.create_pool(NoTls).unwrap();
|
||||
|
||||
let server = HttpServer::new(move || {
|
||||
App::new()
|
||||
.data(pool.clone())
|
||||
.service(web::resource("/users").route(web::post().to(add_user)))
|
||||
})
|
||||
.bind(config.server_addr.clone())?
|
||||
.run();
|
||||
println!("Server running at http://{}/", config.server_addr);
|
||||
|
||||
server.await
|
||||
}
|
Reference in New Issue
Block a user