mirror of
https://github.com/actix/actix-website
synced 2024-11-23 16:31:08 +01:00
update database examples
This commit is contained in:
parent
8ea575194e
commit
1a3fedc78f
@ -2,6 +2,7 @@
|
||||
members = [
|
||||
"application",
|
||||
"async-handlers",
|
||||
"databases",
|
||||
"easy-form-handling",
|
||||
"either",
|
||||
"errors",
|
||||
@ -23,5 +24,3 @@ members = [
|
||||
"url-dispatch",
|
||||
"websockets",
|
||||
]
|
||||
# TODO: update databases
|
||||
exclude = ["databases"]
|
||||
|
@ -5,3 +5,6 @@ edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
actix-web = "4"
|
||||
diesel = { version = "2", features = ["sqlite", "r2d2"] }
|
||||
serde = { version = "1", features = ["derive"] }
|
||||
uuid = { version = "1", features = ["v4"] }
|
||||
|
@ -1,43 +1,76 @@
|
||||
use std::io;
|
||||
|
||||
use actix_web::{error, web, App, HttpResponse, HttpServer, Responder};
|
||||
use diesel::{prelude::*, r2d2, Insertable, Queryable, SqliteConnection};
|
||||
use serde::Serialize;
|
||||
|
||||
mod schema {
|
||||
diesel::table! {
|
||||
users {
|
||||
id -> VarChar,
|
||||
name -> VarChar,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Queryable)]
|
||||
struct User {
|
||||
id: String,
|
||||
name: String,
|
||||
}
|
||||
|
||||
// <handler>
|
||||
fn insert_new_user(db: &SqliteConnection, user: CreateUser) -> Result<User, Error> {
|
||||
use self::schema::users::dsl::*;
|
||||
#[derive(Debug, Insertable)]
|
||||
#[diesel(table_name = self::schema::users)]
|
||||
struct NewUser<'a> {
|
||||
id: &'a str,
|
||||
name: &'a str,
|
||||
}
|
||||
|
||||
fn insert_new_user(
|
||||
conn: &mut SqliteConnection,
|
||||
user_name: String,
|
||||
) -> diesel::QueryResult<User> {
|
||||
use crate::schema::users::dsl::*;
|
||||
|
||||
// Create insertion model
|
||||
let uuid = format!("{}", uuid::Uuid::new_v4());
|
||||
let new_user = models::NewUser {
|
||||
id: &uuid,
|
||||
name: &user.name,
|
||||
let uid = format!("{}", uuid::Uuid::new_v4());
|
||||
let new_user = NewUser {
|
||||
id: &uid,
|
||||
name: &user_name,
|
||||
};
|
||||
|
||||
// normal diesel operations
|
||||
diesel::insert_into(users)
|
||||
.values(&new_user)
|
||||
.execute(&self.0)
|
||||
.execute(conn)
|
||||
.expect("Error inserting person");
|
||||
|
||||
let mut items = users
|
||||
.filter(id.eq(&uuid))
|
||||
.load::<models::User>(&self.0)
|
||||
.expect("Error loading person");
|
||||
let user = users
|
||||
.filter(id.eq(&uid))
|
||||
.first::<User>(conn)
|
||||
.expect("Error loading person that was just inserted");
|
||||
|
||||
Ok(items.pop().unwrap())
|
||||
Ok(user)
|
||||
}
|
||||
// </handler>
|
||||
|
||||
// <main>
|
||||
type DbPool = r2d2::Pool<ConnectionManager<SqliteConnection>>;
|
||||
type DbPool = r2d2::Pool<r2d2::ConnectionManager<SqliteConnection>>;
|
||||
|
||||
#[actix_web::main]
|
||||
async fn main() -> io::Result<()> {
|
||||
// Create connection pool
|
||||
// connect to SQLite DB
|
||||
let manager = r2d2::ConnectionManager::<SqliteConnection>::new("app.db");
|
||||
let pool = r2d2::Pool::builder()
|
||||
.build(manager)
|
||||
.expect("Failed to create pool.");
|
||||
.expect("database URL should be valid path to SQLite DB file");
|
||||
|
||||
// Start HTTP server
|
||||
// start HTTP server on port 8080
|
||||
HttpServer::new(move || {
|
||||
App::new().app_data(web::Data::new(pool.clone()))
|
||||
.resource("/{name}", web::get().to(index))
|
||||
App::new()
|
||||
.app_data(web::Data::new(pool.clone()))
|
||||
.route("/{name}", web::get().to(index))
|
||||
})
|
||||
.bind(("127.0.0.1", 8080))?
|
||||
.run()
|
||||
@ -46,18 +79,21 @@ async fn main() -> io::Result<()> {
|
||||
// </main>
|
||||
|
||||
// <index>
|
||||
async fn index(pool: web::Data<DbPool>, name: web::Path<(String)>) -> impl Responder {
|
||||
let name = name.into_inner();
|
||||
async fn index(
|
||||
pool: web::Data<DbPool>,
|
||||
name: web::Path<(String,)>,
|
||||
) -> actix_web::Result<impl Responder> {
|
||||
let (name,) = name.into_inner();
|
||||
|
||||
let user = web::block(move || {
|
||||
let conn = pool.get().expect("couldn't get db connection from pool");
|
||||
actions::insert_new_user(&conn, &user)
|
||||
.await
|
||||
.map_err(|e| {
|
||||
eprintln!("{}", e);
|
||||
HttpResponse::InternalServerError().finish()
|
||||
}
|
||||
})?;
|
||||
// Obtaining a connection from the pool is also a potentially blocking operation.
|
||||
// So, it should be called within the `web::block` closure, as well.
|
||||
let mut conn = pool.get().expect("couldn't get db connection from pool");
|
||||
|
||||
insert_new_user(&mut conn, name)
|
||||
})
|
||||
.await?
|
||||
.map_err(error::ErrorInternalServerError)?;
|
||||
|
||||
Ok(HttpResponse::Ok().json(user))
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user