presentation-rust-webapps/rust_web.org

7.5 KiB

Rust, Rocket and Diesel

Rust

What is Rust?

#+LaTeX:∈cludegraphics[width = 0.65\textwidth]{img/Bruine_roest_op_tarwe_(Puccinia_recondita_f.sp._tritici_on_Triticum_aestivum).jpg}

Rust, the language

  • About 10 years young (2010)
  • Memory safe without gcing, optional ref counting
  • Ownership, lifetimes, traits
  • Variables are immutable by default and can be shadowed
  • Performance of idiomatic Rust is comparable to the performance of idiomatic cpp

Where to get further information?

Docs…

What are we going to use?

Definitely

Maybe

  • anyhow
  • lettre
  • sha3
  • serde
  • chrono

How to install

Install Rust

$ curl --proto '=https' --tlsv1.2 -sSf \
  https://sh.rustup.rs | sh

Install nightly Rust (needed for Rocket)

$ rustup toolchain install nightly

Install rustfmt (code formatter)

$ rustup component add rustfmt

Install clippy (linter and static code analysis)

$ rustup component add clippy

Rocket

What is Rocket?

Rocket is a web framework for Rust that makes it simple to write fast, secure web applications without sacrificing flexibility, usability, or type safety.

Features

Col left
  • Type Safe
  • Extensible
  • Templating
  • Cookies
Col right
  • Streams
  • Testing (Unit tests)
  • Typed URIs

Where to get further information?

Diesel

What is an ORM?

Object-relational-mapping is a programming technique for converting data between incompatible type systems using the paradigms of your programming language.

Pros

  • Models are DRY (don't repeat yourself)
  • Cleaner separation
  • Prepared and sanitised queries
  • Changes are versioned

Cons

  • Debugging can get more complicated (performance)
  • No need to learn SQL

Prerequisites diesel_cli

Setup Diesel for default DBMS

$ sudo apt install libpq-dev libsqlite3-dev \
  default-libmysqlclient-dev
$ cargo install diesel_cli

Setup Diesel for SQLite only

$ sudo apt install libsqlite3-dev
$ cargo install diesel_cli -no-default-features \
  --features sqlite

How does that look in Rust

Define a struct

#[derive(Queryable)]
pub struct NewComment<'a> {
    name: &'a str,
    email: &'a str,
    comment: &'a str,
}

Insert something

let user = NewUser {
    name: "foo",
    email: "foo@bar.baz",
    comment: "A comment",
};

insert_into(comments)
    .values(&user)
    .execute(conn);

Where to get further information?

Lets code something

Lets create a small web app

A mini text board where everybody can post and reply.

Caution

HTML is not disabled!

Let us start

#+LaTeX:∈cludegraphics[width = 0.7\textwidth]{img/challenge-accepted-lets-code-it.jpg}

Conclusion

What did we do?

  • Setup Rust and Diesel
  • Create a project with Rocket and Diesel prerequisites
  • Handle a SQLite DB with Diesel
  • Create Tera templates

What did you hopefully learn!

Two small projects

ldap0r

  • LDAP password reset tool
  • ($\sim$ 300 SloC)
  • First release
  • Tests

filerly

  • File sharing a la NextCloud
  • Not yet ready, work in progress…

The End

#+LaTeX:∈cludegraphics[width = 0.3\textwidth]{img/goodbye.jpg}

\Huge Thanks for your attention!!1!

This is the end..