Original Src:
https://gitlab.com/udemy-paris/rocket-app
$ mkdir app
$ cd app
$ cargo init . --bin
$ vi ./
$ cargo run
$ curl localhost:8000
$ curl localhost:8000/rustaceans
$ curl localhost:8000/rustaceans/1
$ curl localhost:8000/rustaceans -X POST -H 'Content-type: application/json'
$ curl localhost:8000/rustaceans/1 -X PUT -H 'Content-type: application/json'
$ curl localhost:8000/rustaceans/1 -X DELETE -I
$ curl localhost:8000/rustaceans -H 'Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ'
$ sudo apt install libsqlite3-dev
$ cargo install diesel_cli --no-default-features --features sqlite
$ diesel setup --database-url=database.sqlite
$ diesel migration generate create_rustaceans
$ diesel migration run --database-url=database.sqlite
$ diesel migration redo --database-url=database.sqlite
// CREATE
$ curl localhost:8000/rustaceans \
-H "Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ" \
-H "Content-type: application/json" \
-X POST -d '{"name" : "John Doe", "email" : "john.doe@gmail.com"}'
// GET ALL
$ curl localhost:8000/rustaceans \
-H "Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ" \
-H "Content-type: application/json" \
| jq
returns:
[
{
"id": 1,
"name": "John Doe",
"email": "john.doe@gmail.com",
"created_at": "2021-12-20 23:58:52"
}
]
// UPDATE
$ curl localhost:8000/rustaceans/1 \
-H "Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ" \
-H "Content-type: application/json" \
-X PUT -d '{"id" : 1, "name" : "Jane Doe", "email" : "john.doe@gmail.com"}'
// DELETE
$ curl localhost:8000/rustaceans/1 \
-H "Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ" \
-H "Content-type: application/json" \
-X DELETE
$ cargo build --release
$ ./target/release/rocket-app
$ ROCKET_DATABASES={sqlite_path={url./database.sqlite}} ./app
Add app as systemd service
$ sudo vi /etc/systemd/system/rocket-app.service
[Unit]
Description=My Rocket application
[Service]
User=www-data
Group=www-data
# The user www-data should own this directory
WorkingDirectory=/var/www/rocket-app
Environment="ROCKET_ENV=prod"
Environment="ROCKET_ADDRESS=127.0.0.1"
Environment="ROCKET_PORT=8000"
Environment="ROCKET_DATABASES={sqlite_path={url./database.sqlite}}"
ExecStart=/var/www/rocket-app/rocket-app
[Install]
WantedBy=multi-user.target
$ sudo systemctl enable rocket-app.service
$ sudo systemctl start rocket-app.service
$ sudo systemctl status rocket-app.service