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

delete db/r2d2 example

This commit is contained in:
Rob Ede
2022-02-06 08:25:38 +00:00
parent a4d43c0ff8
commit da60a30fd9
22 changed files with 29 additions and 123 deletions

View File

@ -1,6 +1,6 @@
# diesel
Basic integration of [Diesel](https://diesel.rs/) using SQLite for Actix web.
Basic integration of [Diesel](https://diesel.rs/) using SQLite for Actix Web.
## Usage

View File

@ -26,7 +26,7 @@ pub fn insert_new_user(
nm: &str,
conn: &SqliteConnection,
) -> Result<models::User, DbError> {
// It is common when using Diesel with Actix web to import schema-related
// It is common when using Diesel with Actix Web to import schema-related
// modules inside a function's scope (rather than the normal module's scope)
// to prevent import collisions and namespace pollution.
use crate::schema::users::dsl::*;

View File

@ -1,4 +1,4 @@
//! Actix web Diesel integration example
//! Actix Web Diesel integration example
//!
//! Diesel does not support tokio, so we have to run it in separate threads using the web::block
//! function which offloads blocking code (like Diesel's) in order to not block the server's thread.

View File

@ -1,6 +1,6 @@
# MongoDB
Simple example of MongoDB usage with Actix web. For more information on the MongoDB Rust driver,
Simple example of MongoDB usage with Actix Web. For more information on the MongoDB Rust driver,
visit the [documentation](https://docs.rs/mongodb/2.0.0/mongodb/index.html) and
[source code](https://github.com/mongodb/mongo-rust-driver).
@ -22,4 +22,3 @@ MongoDB manual.
### Run the example
To execute the example code, run `cargo run` in the `database_interactions/mongodb` directory.

View File

@ -1,14 +0,0 @@
[package]
name = "r2d2-example"
version = "1.0.0"
edition = "2021"
[dependencies]
actix-web = "4.0.0-rc.1"
env_logger = "0.9.0"
uuid = { version = "1.0.0-alpha.1", features = ["v4"] }
r2d2 = "0.8"
r2d2_sqlite = "0.18.0"
rusqlite = "0.25.4"

View File

@ -1,57 +0,0 @@
//! Actix web r2d2 example
use std::io;
use actix_web::{
error::InternalError, http::StatusCode, middleware, web, App, Error, HttpResponse,
HttpServer,
};
use r2d2::Pool;
use r2d2_sqlite::SqliteConnectionManager;
/// Async request handler. Ddb pool is stored in application state.
async fn index(
path: web::Path<String>,
db: web::Data<Pool<SqliteConnectionManager>>,
) -> Result<HttpResponse, Error> {
// execute sync code in threadpool
let res = web::block(move || {
let conn = db.get().unwrap();
let uuid = format!("{}", uuid::Uuid::new_v4());
conn.execute(
"INSERT INTO users (id, name) VALUES ($1, $2)",
&[&uuid, &path.into_inner()],
)
.unwrap();
conn.query_row("SELECT name FROM users WHERE id=$1", &[&uuid], |row| {
row.get::<_, String>(0)
})
})
.await
.unwrap()
.map(|user| HttpResponse::Ok().json(user))
.map_err(|e| InternalError::new(e, StatusCode::INTERNAL_SERVER_ERROR))?;
Ok(res)
}
#[actix_web::main]
async fn main() -> io::Result<()> {
std::env::set_var("RUST_LOG", "actix_web=debug");
env_logger::init();
// r2d2 pool
let manager = SqliteConnectionManager::file("test.db");
let pool = r2d2::Pool::new(manager).unwrap();
// start http server
HttpServer::new(move || {
App::new()
.app_data(web::Data::new(pool.clone())) // <- store db pool in app state
.wrap(middleware::Logger::default())
.route("/{name}", web::get().to(index))
})
.bind("127.0.0.1:8080")?
.run()
.await
}

View File

@ -17,7 +17,7 @@
### Crates Used
- [actix-web](https://crates.io/crates/actix-web) // Actix web is a simple, pragmatic and extremely fast web framework for Rust.
- [actix-web](https://crates.io/crates/actix-web) // Actix Web is a simple, pragmatic and extremely fast web framework for Rust.
- [rust-argon2](https://crates.io/crates/rust-argon2) // crate for hashing passwords using the cryptographically-secure Argon2 hashing algorithm.
- [chrono](https://crates.io/crates/chrono) // Date and time library for Rust.
- [diesel](https://crates.io/crates/diesel) // A safe, extensible ORM and Query Builder for PostgreSQL, SQLite, and MySQL.