Giter Site home page Giter Site logo

lwp001 / rust-webserver-example-with-iron-diesel-r2d2-serde Goto Github PK

View Code? Open in Web Editor NEW

This project forked from davidbm/rust-webserver-example-with-iron-diesel-r2d2-serde

0.0 1.0 0.0 33 KB

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

License: MIT License

Rust 99.86% RenderScript 0.14%

rust-webserver-example-with-iron-diesel-r2d2-serde's Introduction

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

rust-webserver-example-with-iron-diesel-r2d2-serde's People

Contributors

davidbm 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.