1
0
mirror of https://github.com/actix/examples synced 2025-04-22 08:34:52 +02:00

chore: tweaks to diesel-async

This commit is contained in:
Rob Ede 2025-04-09 23:49:30 +01:00
parent f2c51a8713
commit 3f2186ef19
No known key found for this signature in database
GPG Key ID: 97C636207D3EF933
5 changed files with 12 additions and 22 deletions

View File

@ -24,7 +24,7 @@ docker ps --format "table {{.ID}}\t{{.Names}}\t{{.Status}}\t{{.Ports}}"
cd databases/diesel-async cd databases/diesel-async
cargo install diesel_cli --no-default-features --features postgres 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 setup
diesel migration run diesel migration run
``` ```
@ -32,7 +32,7 @@ diesel migration run
The database will now be created in your PostgreSQL instance. The database will now be created in your PostgreSQL instance.
```sh ```sh
docker exec -i postgresql psql -U test-user -c "\l" docker exec -i postgresql psql -U test-user -l
``` ```
### Running Server ### Running Server
@ -68,16 +68,16 @@ On success, a response like the following is returned:
<details> <details>
<summary>Client Examples</summary> <summary>Client Examples</summary>
Using [HTTPie]: Using [HTTPie] / [xh]:
```sh ```sh
http POST localhost:8080/items name=thingamajig http POST :8080/items name=thingamajig
``` ```
Using cURL: Using cURL:
```sh ```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
``` ```
</details> </details>
@ -89,7 +89,7 @@ Gets an item from the DB using its UID (returned from the insert request or take
<details> <details>
<summary>Client Examples</summary> <summary>Client Examples</summary>
Using [HTTPie]: Using [HTTPie] / [xh]:
```sh ```sh
http localhost:8080/items/9e46baba-a001-4bb3-b4cf-4b3e5bab5e97 http localhost:8080/items/9e46baba-a001-4bb3-b4cf-4b3e5bab5e97
@ -98,7 +98,7 @@ http localhost:8080/items/9e46baba-a001-4bb3-b4cf-4b3e5bab5e97
Using cURL: Using cURL:
```sh ```sh
curl -S http://localhost:8080/items/9e46baba-a001-4bb3-b4cf-4b3e5bab5e97 curl --show-error http://localhost:8080/items/9e46baba-a001-4bb3-b4cf-4b3e5bab5e97
``` ```
</details> </details>
@ -106,11 +106,8 @@ curl -S http://localhost:8080/items/9e46baba-a001-4bb3-b4cf-4b3e5bab5e97
### Explore The PostgreSQL DB ### Explore The PostgreSQL DB
```sh ```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 [xh]: https://httpie.io/cli
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)
[httpie]: https://httpie.io/cli [httpie]: https://httpie.io/cli

View File

@ -4,6 +4,3 @@
[print_schema] [print_schema]
file = "src/schema.rs" file = "src/schema.rs"
custom_type_derives = ["diesel::query_builder::QueryId", "Clone"] custom_type_derives = ["diesel::query_builder::QueryId", "Clone"]
[migrations_directory]
dir = "/home/alex/CLionProjects/actix-with-async-diesel/migrations"

View File

@ -1,2 +1 @@
-- This file should undo anything in `up.sql`
DROP TABLE IF EXISTS items; DROP TABLE IF EXISTS items;

View File

@ -1,4 +1,3 @@
-- Your SQL goes here
CREATE TABLE IF NOT EXISTS items CREATE TABLE IF NOT EXISTS items
( (
id uuid DEFAULT gen_random_uuid() PRIMARY KEY, id uuid DEFAULT gen_random_uuid() PRIMARY KEY,

View File

@ -3,10 +3,10 @@ extern crate diesel;
use std::{env, io}; 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::{ use diesel_async::{
pooled_connection::{bb8::Pool, AsyncDieselConnectionManager},
AsyncPgConnection, AsyncPgConnection,
pooled_connection::{AsyncDieselConnectionManager, bb8::Pool},
}; };
use dotenvy::dotenv; use dotenvy::dotenv;
use uuid::Uuid; use uuid::Uuid;
@ -86,11 +86,9 @@ async fn main() -> io::Result<()> {
App::new() App::new()
// add DB pool handle to app data; enables use of `web::Data<DbPool>` extractor // add DB pool handle to app data; enables use of `web::Data<DbPool>` extractor
.app_data(web::Data::new(pool.clone())) .app_data(web::Data::new(pool.clone()))
// add request logger middleware
.wrap(middleware::Logger::default())
// add route handlers
.service(add_item) .service(add_item)
.service(get_item) .service(get_item)
.wrap(middleware::Logger::default())
}) })
.bind(("127.0.0.1", 8080))? .bind(("127.0.0.1", 8080))?
.run() .run()