Coder Social home page Coder Social logo

svenstaro / rust-web-boilerplate Goto Github PK

View Code? Open in Web Editor NEW
303.0 12.0 53.0 152 KB

Rust web template for modern web backend applications

License: MIT License

Rust 95.65% Shell 1.00% PLpgSQL 3.35%
best-practices boilerplate web diesel rocket

rust-web-boilerplate's Introduction

Rust Web Boilerplate

Build Status codecov lines of code license

About

This is a boilerplate project made using best practices for getting started quickly in a new project. I made this for myself but maybe it will help someone else. Pull requests and discussions on best practices welcome!

Development setup

Install a few external dependencies and make sure ~/.cargo/bin is in your $PATH:

cargo install diesel_cli
cargo install cargo-watch

Optionally if you want line coverage from your tests, install cargo-tarpaulin:

cargo-tarpaulin

Copy .env.example to .env and update your application environment in this file.

Make sure you have a working local postgres setup. Your current user should be admin in your development postgres installation and it should use the "peer" or "trust" auth methods (see pg_hba.conf).

Now you can launch the watch.sh script which helps you quickly iterate. It will remove and recreate the DB and run the migrations and then the tests on all code changes.

./watch.sh

To get line coverage, do

cargo tarpaulin --ignore-tests

rust-web-boilerplate's People

Contributors

andantonyan avatar dependabot-preview[bot] avatar dependabot-support avatar dependabot[bot] avatar hatzel avatar nukesor avatar revgum avatar shirshak55 avatar svenstaro avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

rust-web-boilerplate's Issues

QUESTION: 500 error, but rocket is saying everything is just fine when I try and insert a row

Here is my post request code, which was just modified from the boilerplate

#[post("/new-article", data = "<body>", format = "application/json")]
pub fn new_article(db: DbConn, body: Json<NewArticle>) -> Result<APIResponse, APIResponse>{
    let new_article = NewArticle {
        title: body.title.to_string(),
        description: body.description.to_string(),
        tag: body.tag.to_string()
    };
    println!("{}", &new_article);
    let insert_result = diesel::insert_into(articles::table)
        .values(&new_article)
        .get_result::<ArticleModel>(&*db);

    if let Err(diesel::result::Error::DatabaseError(
                   diesel::result::DatabaseErrorKind::UniqueViolation,
                   _,
               )) = insert_result
    {
        return Err(conflict().message("Article already exists."));
    }

    let user = insert_result?;
    Ok(created().data(json!(&user)))
}

The SQL table

CREATE TABLE articles
(
    "id"          SERIAL  NOT NULL PRIMARY KEY,
    "title"       TEXT    NOT NULL,
    "tag"         TEXT    NOT NULL,
    "description" TEXT    NOT NULL,
    "last_edited" TIMESTAMP DEFAULT current_timestamp NOT NULL,
    "edit_count"  INTEGER NOT NULL DEFAULT 0,
    "status"      BOOLEAN NOT NULL DEFAULT true,
    "publicized"  BOOLEAN NOT NULL DEFAULT false,
    "featured"    BOOLEAN NOT NULL DEFAULT false,
);
-- CreateIndex
CREATE UNIQUE INDEX articles.id_unique ON articles(id);

And my Models

#[derive(Debug, Serialize, Deserialize, Queryable, Identifiable, AsChangeset)]
#[table_name="articles"]
pub struct ArticleModel {
    pub id: i32,
    pub title: String,
    pub tag: String,
    pub description: String,
    pub last_edited: NaiveDateTime,
    pub edit_count: i32,
    pub status: bool,
    pub publicized: bool,
    pub featured: bool,
}

#[derive(Insertable, Debug, Deserialize)]
#[table_name="articles"]
pub struct NewArticle {
    pub title: String,
    pub description: String,
    pub tag: String,
}

Any help? I dont know where else to go

Update to rocket v5

I am having to use some async features for some other parts of my code, things that Rocket only supports in version 5.0. The version is currently not released but It would be nice to have a branch of this that started adapting version 5.0 since eventually it will be the release. A lot has changed so far.

edit: im not sure how active this is but im just putting this here if it still is

Implement cors_allow_origin header

I was wondering how to implement cors origins rules. Changing the string values in config.rs doesn't seem to do the trick. It's still blocking fetch requests from local addresses. A small example would do. Thanks!

Don't use a 'hard coded' salt for password hash

I have looked through the code and found that it is using a hard coded salt for the password hash loginsalt.
https://github.com/svenstaro/rust-web-boilerplate/blob/master/src/models/user.rs#L42
https://github.com/svenstaro/rust-web-boilerplate/blob/master/src/models/user.rs#L47

You should consider to use a random value here and also save it alongside your password hash inside the database. Much in the same way the bcrypt-rs crate is doing it https://github.com/Keats/rust-bcrypt/blob/master/src/lib.rs#L115

either by concatenate both and splitting them .. or just save them in separate columns inside the database.

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.