From 3f2186ef1958c48ad3d3ed74b96009d8833565aa Mon Sep 17 00:00:00 2001 From: Rob Ede Date: Wed, 9 Apr 2025 23:49:30 +0100 Subject: [PATCH] chore: tweaks to diesel-async --- databases/diesel-async/README.md | 21 ++++++++----------- databases/diesel-async/diesel.toml | 3 --- .../2025-01-18-144029_create_items/down.sql | 1 - .../2025-01-18-144029_create_items/up.sql | 1 - databases/diesel-async/src/main.rs | 8 +++---- 5 files changed, 12 insertions(+), 22 deletions(-) diff --git a/databases/diesel-async/README.md b/databases/diesel-async/README.md index 01116f4d..a5d4f1ec 100644 --- a/databases/diesel-async/README.md +++ b/databases/diesel-async/README.md @@ -24,7 +24,7 @@ docker ps --format "table {{.ID}}\t{{.Names}}\t{{.Status}}\t{{.Ports}}" cd databases/diesel-async cargo install diesel_cli --no-default-features --features postgres -echo DATABASE_URL=postgres://test-user:password@localhost:5432/test_db > .env +echo DATABASE_URL=postgres://test-user:password@localhost:5432/actix_example_diesel_async > .env diesel setup diesel migration run ``` @@ -32,7 +32,7 @@ diesel migration run The database will now be created in your PostgreSQL instance. ```sh -docker exec -i postgresql psql -U test-user -c "\l" +docker exec -i postgresql psql -U test-user -l ``` ### Running Server @@ -68,16 +68,16 @@ On success, a response like the following is returned:
Client Examples -Using [HTTPie]: +Using [HTTPie] / [xh]: ```sh -http POST localhost:8080/items name=thingamajig +http POST :8080/items name=thingamajig ``` Using cURL: ```sh -curl -S -X POST --header "Content-Type: application/json" --data '{"name":"thingamajig"}' http://localhost:8080/items +curl --show-error -X POST --header "Content-Type: application/json" --data '{"name":"thingamajig"}' http://localhost:8080/items ```
@@ -89,7 +89,7 @@ Gets an item from the DB using its UID (returned from the insert request or take
Client Examples -Using [HTTPie]: +Using [HTTPie] / [xh]: ```sh http localhost:8080/items/9e46baba-a001-4bb3-b4cf-4b3e5bab5e97 @@ -98,7 +98,7 @@ http localhost:8080/items/9e46baba-a001-4bb3-b4cf-4b3e5bab5e97 Using cURL: ```sh -curl -S http://localhost:8080/items/9e46baba-a001-4bb3-b4cf-4b3e5bab5e97 +curl --show-error http://localhost:8080/items/9e46baba-a001-4bb3-b4cf-4b3e5bab5e97 ```
@@ -106,11 +106,8 @@ curl -S http://localhost:8080/items/9e46baba-a001-4bb3-b4cf-4b3e5bab5e97 ### Explore The PostgreSQL DB ```sh -docker exec -i postgresql psql -U test-user -d test_db -c "select * from public.items" +docker exec -i postgresql psql -U test-user -d actix_example_diesel_async -c "SELECT * from public.items" ``` -## Using Other Databases - -You can find a complete example of Diesel + PostgreSQL at: [https://github.com/TechEmpower/FrameworkBenchmarks/tree/master/frameworks/Rust/actix](https://github.com/TechEmpower/FrameworkBenchmarks/tree/master/frameworks/Rust/actix) - +[xh]: https://httpie.io/cli [httpie]: https://httpie.io/cli diff --git a/databases/diesel-async/diesel.toml b/databases/diesel-async/diesel.toml index 34c0a182..de532845 100644 --- a/databases/diesel-async/diesel.toml +++ b/databases/diesel-async/diesel.toml @@ -4,6 +4,3 @@ [print_schema] file = "src/schema.rs" custom_type_derives = ["diesel::query_builder::QueryId", "Clone"] - -[migrations_directory] -dir = "/home/alex/CLionProjects/actix-with-async-diesel/migrations" diff --git a/databases/diesel-async/migrations/2025-01-18-144029_create_items/down.sql b/databases/diesel-async/migrations/2025-01-18-144029_create_items/down.sql index 6839ef8e..f3f9b647 100644 --- a/databases/diesel-async/migrations/2025-01-18-144029_create_items/down.sql +++ b/databases/diesel-async/migrations/2025-01-18-144029_create_items/down.sql @@ -1,2 +1 @@ --- This file should undo anything in `up.sql` DROP TABLE IF EXISTS items; diff --git a/databases/diesel-async/migrations/2025-01-18-144029_create_items/up.sql b/databases/diesel-async/migrations/2025-01-18-144029_create_items/up.sql index 7066001b..719260a7 100644 --- a/databases/diesel-async/migrations/2025-01-18-144029_create_items/up.sql +++ b/databases/diesel-async/migrations/2025-01-18-144029_create_items/up.sql @@ -1,4 +1,3 @@ --- Your SQL goes here CREATE TABLE IF NOT EXISTS items ( id uuid DEFAULT gen_random_uuid() PRIMARY KEY, diff --git a/databases/diesel-async/src/main.rs b/databases/diesel-async/src/main.rs index 82630220..fa9ae3c7 100644 --- a/databases/diesel-async/src/main.rs +++ b/databases/diesel-async/src/main.rs @@ -3,10 +3,10 @@ extern crate diesel; use std::{env, io}; -use actix_web::{error, get, middleware, post, web, App, HttpResponse, HttpServer, Responder}; +use actix_web::{App, HttpResponse, HttpServer, Responder, error, get, middleware, post, web}; use diesel_async::{ - pooled_connection::{bb8::Pool, AsyncDieselConnectionManager}, AsyncPgConnection, + pooled_connection::{AsyncDieselConnectionManager, bb8::Pool}, }; use dotenvy::dotenv; use uuid::Uuid; @@ -86,11 +86,9 @@ async fn main() -> io::Result<()> { App::new() // add DB pool handle to app data; enables use of `web::Data` extractor .app_data(web::Data::new(pool.clone())) - // add request logger middleware - .wrap(middleware::Logger::default()) - // add route handlers .service(add_item) .service(get_item) + .wrap(middleware::Logger::default()) }) .bind(("127.0.0.1", 8080))? .run()