/rust-webserver-example-with-iron-diesel-r2d2-serde

Rust Web server example using Iron, R2d2 Diesel and Serde json

Primary LanguageRustMIT LicenseMIT

This is quite old and Iron framework is deprecated. You may be better using something like https://rocket.rs/ or https://github.com/http-rs/tide. For the DB, diesel is still the way to go (it is not async yet, but very feature complete.)

Rust webserver example backend

I evolved a bit the ideas here, maybe you want to watch this project instead of this one: https://github.com/DavidBM/templic-backend

Example of how to build a http json backend in rust. The code is split in controllers/models. For more complexity new abastraction shluld be created.

Main components:

  • slog: log system
  • r2d2: database connection pool
  • diesel: ORM
  • iron: http framework
  • serde: json en/decoder

Other components/characteristics

  • Database password hash
  • CORS support
  • Login with JSON Webtokens
  • Middlewares

Structure

Routes are declared in src/http_adaptor/endpoints. Some macros are used for automate the code there.

The Diesel (PostgreSQL) ORM connections are pooled with r2d2.

The output is a JSON version of the database model using Serde json.

This code use macros in several places, use cargo expand (cargo install cargo-expand) for see the final code.

Dependencies

  • rust and cargo (Install using https://www.rustup.rs/)
  • diesel-cli - Install: cargo install diesel_cli
  • PostgreSQL (accesible from the server. Can be local or remote)

Compiled using Rust rustc 1.16.0 (30cf806ef 2017-03-10) stable version.

Before run

  • Enter in the git folder
  • Update the database connection data in the file .env
  • diesel migration run

Running (develop)

  • cargo run

Deploy

  • Copy the executable compiled with cargo build --release in the server (Sign the executable and verify).
  • Copy the .env of the repository next to the executable.
  • Fill the data of .env.
  • Execute the executable.

Creating new database migrations

  • diesel migration generate <migration name>
  • fill the files up.sql and down.sql
  • check with diesel migration run
  • check again with diesel migration redo
  • Execute cargo build for checking types in the code. (Types depend of the actual types in the database, and the compiler use that information for checking compatibility with code types)

If there isn't any errors, you finish! :)

Running migrations

Adding new endpoints

  • Add the function to the controller or create a new controller in src/controllers.
    • If a new controller is created, don't forget to add it in src/controllers/mod.rs.
  • Add the function to src/http_adaptor/endpoints.rs.
    • Maybe you need to add use controllers::<controller file name>; at the beginning of the file.
  • Fill the controller function
  • Test it

Recommended packages for working in Rust & sublime text editor

  • Sublime packages
    • rust enhanced
    • anaconda_rust
    • sublimeLinter-contrib-rustc
  • cargo install cargo-expand
  • cargo install cargo-watch

TODO

  • Create get /post/:id
  • Create delete /post/:id
  • Create put /post/:id
  • Tokens MUST change every login
  • Add caducity to tokens
  • Add token_version to user table for cancelling tokens for users
  • Improve return macros for showing the internal error (like response_bad_request)
  • Add permissions for delete & update user (only the same user can do it)
  • Delete user must delete all the posts
  • Logs in queries should identify the query action

Blocked

  • Update jsonwebtoken and remove rustc-serialize (it breaks cargo expand)
  • Update to better security argon2rs (using at least 10 passes and all possible lanes)
  • Help in iron-cors and reactivate the crate in the code
  • base64. Maintain update.
  • Debug the SQL of the update methods