/Building-web-APIs-with-Rust

[Udemy] Building web APIs with Rust (beginners) [ENG, 2020]

Primary LanguageRust

[Udemy] Building web APIs with Rust (beginners) [ENG, 2020]


Original Src:
https://gitlab.com/udemy-paris/rocket-app


02. Routing


01. Hello world!

$ mkdir app 
$ cd app
$ cargo init . --bin
$ vi ./

$ cargo run

$ curl localhost:8000

02. JSON


03. CRUD routes


$ 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

05. Error catchers


03. Auth


01. Basic auth intro


02. Implementing a basic auth guard


$ curl localhost:8000/rustaceans -H 'Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ'

04. Database


01. Diesel CLI

$ 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

02. Diesel and rocket - Dependencies


03. Diesel and rocket - Model & first query


04. Diesel and rocket - New model & create endpoint


// 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"
  }
]

06. Diesel and rocket - Full CRUD


// 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 

07. Repositories


09. Error handling


10. Embedding migrations


05. Deploying


01. Deploying and systemd

$ 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

02. Reverse proxying with nginx and ssl