1
0
mirror of https://github.com/actix/examples synced 2024-11-30 09:12:54 +01:00
examples/diesel
yjh 89d2e4e3f7
fix typo and update some deps (#390)
* chore: fix typo for `run-in-thread`

* chore: update some deps for examples
2020-12-14 11:30:06 +00:00
..
migrations/20170124012402_create_users actix examples in actix release version 2018-04-13 09:18:42 +08:00
src v3 examples (#364) 2020-09-12 16:49:45 +01:00
.env Read SQLite path from .env and fix DATABASE_URL 2019-06-12 23:03:20 +02:00
.gitignore improve diesel example (#241) 2020-01-27 21:20:04 +09:00
Cargo.toml fix typo and update some deps (#390) 2020-12-14 11:30:06 +00:00
README.md improve diesel example (#241) 2020-01-27 21:20:04 +09:00

diesel

Basic integration of Diesel using SQLite for Actix web.

Usage

Install SQLite

# on OpenSUSE
sudo zypper install sqlite3-devel libsqlite3-0 sqlite3

# on Ubuntu
sudo apt-get install libsqlite3-dev sqlite3

# on Fedora
sudo dnf install libsqlite3x-devel sqlite3x

# on macOS (using homebrew)
brew install sqlite3

Initialize SQLite Database

cd examples/diesel
cargo install diesel_cli --no-default-features --features sqlite

echo "DATABASE_URL=test.db" > .env
diesel migration run

There will now be a database file at ./test.db.

Running Server

cd examples/diesel
cargo run (or ``cargo watch -x run``)

# Started http server: 127.0.0.1:8080

Available Routes

POST /user

Inserts a new user into the SQLite DB.

Provide a JSON payload with a name. Eg:

{ "name": "bill" }

On success, a response like the following is returned:

{
    "id": "9e46baba-a001-4bb3-b4cf-4b3e5bab5e97",
    "name": "bill"
}
Client Examples

Using HTTPie:

http POST localhost:8080/user name=bill

Using cURL:

curl -S -X POST --header "Content-Type: application/json" --data '{"name":"bill"}' http://localhost:8080/user

GET /user/{user_uid}

Gets a user from the DB using its UID (returned from the insert request or taken from the DB directly). Returns a 404 when no user exists with that UID.

Client Examples

Using HTTPie:

http localhost:8080/user/9e46baba-a001-4bb3-b4cf-4b3e5bab5e97

Using cURL:

curl -S http://localhost:8080/user/9e46baba-a001-4bb3-b4cf-4b3e5bab5e97

Explore The SQLite DB

sqlite3 test.db
sqlite> .tables
sqlite> SELECT * FROM users;

Using Other Databases

You can find a complete example of Diesel + PostgreSQL at: https://github.com/TechEmpower/FrameworkBenchmarks/tree/master/frameworks/Rust/actix