Code Monkey home page Code Monkey logo

building-web-apis-with-rust's Introduction

[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" : "[email protected]"}' 

// GET ALL
$ curl localhost:8000/rustaceans \
  -H "Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ" \
  -H "Content-type: application/json" \
  | jq

returns:

[
  {
    "id": 1,
    "name": "John Doe",
    "email": "[email protected]",
    "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" : "[email protected]"}'

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

building-web-apis-with-rust's People

Contributors

webmakaka avatar wildmakaka avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.