|
| 1 | +# diesel |
| 2 | + |
| 3 | +Basic integration of [Diesel-async](https://github.com/weiznich/diesel_async) using PostgreSQL for Actix Web. |
| 4 | + |
| 5 | +## Usage |
| 6 | + |
| 7 | +### Install PostgreSQL |
| 8 | + |
| 9 | +```sh |
| 10 | +# on any OS |
| 11 | +docker run -d --restart unless-stopped --name postgresql -e POSTGRES_USER=test-user -e POSTGRES_PASSWORD=password -p 5432:5432 -v postgres_data:/var/lib/postgresql/data postgres:alpine |
| 12 | +``` |
| 13 | +make sure it has successfully started up and is running |
| 14 | +```sh |
| 15 | +# on any OS |
| 16 | +docker ps --format "table {{.ID}}\t{{.Names}}\t{{.Status}}\t{{.Ports}}" |
| 17 | +``` |
| 18 | + |
| 19 | +### Initialize PostgreSQL Database |
| 20 | + |
| 21 | +```sh |
| 22 | +cd databases/diesel-async |
| 23 | +cargo install diesel_cli --no-default-features --features postgres |
| 24 | + |
| 25 | +echo DATABASE_URL=postgres://test-user:password@localhost:5432/test_db > .env |
| 26 | +diesel setup |
| 27 | +diesel migration run |
| 28 | +``` |
| 29 | + |
| 30 | +The database will now be created in your PostgreSQL instance. |
| 31 | +```sh |
| 32 | +docker exec -i postgresql psql -U test-user -c "\l" |
| 33 | +``` |
| 34 | + |
| 35 | +### Running Server |
| 36 | + |
| 37 | +```sh |
| 38 | +cd databases/diesel-async |
| 39 | +cargo run |
| 40 | + |
| 41 | +# Started http server: 127.0.0.1:8080 |
| 42 | +``` |
| 43 | + |
| 44 | +### Available Routes |
| 45 | + |
| 46 | +#### `POST /items` |
| 47 | + |
| 48 | +Inserts a new item into the PostgreSQL DB. |
| 49 | + |
| 50 | +Provide a JSON payload with a name. Eg: |
| 51 | + |
| 52 | +```json |
| 53 | +{ "name": "thingamajig" } |
| 54 | +``` |
| 55 | + |
| 56 | +On success, a response like the following is returned: |
| 57 | + |
| 58 | +```json |
| 59 | +{ |
| 60 | + "id": "01948982-67d0-7a55-b4b1-8b8b962d8c6b", |
| 61 | + "name": "thingamajig" |
| 62 | +} |
| 63 | +``` |
| 64 | + |
| 65 | +<details> |
| 66 | + <summary>Client Examples</summary> |
| 67 | + |
| 68 | +Using [HTTPie]: |
| 69 | + |
| 70 | +```sh |
| 71 | +http POST localhost:8080/items name=thingamajig |
| 72 | +``` |
| 73 | + |
| 74 | +Using cURL: |
| 75 | + |
| 76 | +```sh |
| 77 | +curl -S -X POST --header "Content-Type: application/json" --data '{"name":"thingamajig"}' http://localhost:8080/items |
| 78 | +``` |
| 79 | + |
| 80 | +</details> |
| 81 | + |
| 82 | +#### `GET /items/{item_uid}` |
| 83 | + |
| 84 | +Gets an item from the DB using its UID (returned from the insert request or taken from the DB directly). Returns a 404 when no item exists with that UID. |
| 85 | + |
| 86 | +<details> |
| 87 | + <summary>Client Examples</summary> |
| 88 | + |
| 89 | +Using [HTTPie]: |
| 90 | + |
| 91 | +```sh |
| 92 | +http localhost:8080/items/9e46baba-a001-4bb3-b4cf-4b3e5bab5e97 |
| 93 | +``` |
| 94 | + |
| 95 | +Using cURL: |
| 96 | + |
| 97 | +```sh |
| 98 | +curl -S http://localhost:8080/items/9e46baba-a001-4bb3-b4cf-4b3e5bab5e97 |
| 99 | +``` |
| 100 | + |
| 101 | +</details> |
| 102 | + |
| 103 | +### Explore The PostgreSQL DB |
| 104 | + |
| 105 | +```sh |
| 106 | +docker exec -i postgresql psql -U test-user -d test_db -c "select * from public.items" |
| 107 | +``` |
| 108 | + |
| 109 | +## Using Other Databases |
| 110 | + |
| 111 | +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) |
| 112 | + |
| 113 | +[httpie]: https://httpie.io/cli |
0 commit comments