Skip to content

Commit

Permalink
Add Rustfmt and Clippy as tooling for the project
Browse files Browse the repository at this point in the history
- Rustfmt will make it easy for the project to follow thr standard Rust sytnax (with a coupel of our changes)
- Clippy is an extra linter that can help make suggestions to improve code quality
  • Loading branch information
mirdaki committed Dec 19, 2018
1 parent 127d267 commit c2b19d6
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 153 deletions.
134 changes: 0 additions & 134 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ authors = ["Hunter Jarrell <[email protected]>"]
rocket = "0.4.0"
diesel = { version = "1.3", features = ["postgres", "r2d2"] }
diesel_migrations = "1.3"
dotenv = "0.13.0"

[dependencies.rocket_contrib]
version = "0.4.0"
Expand Down
4 changes: 4 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ RUN cargo install diesel_cli --no-default-features --features postgres

RUN cargo install cargo-watch

RUN rustup component add rustfmt

RUN rustup component add clippy

WORKDIR /club-backend

EXPOSE 3001
Expand Down
14 changes: 11 additions & 3 deletions docs/extra-docker-info.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

## Background

With some insight from this [Rust Web Starter project](https://github.com/ghotiphud/rust-web-starter) the Dockerfile and docker-compose were edited to connect to the database properly, to not re-install cargo packages each time docker-compose is run, to automatically re-compile code changes, and to use the diesel CLI.
With some insight from this [Rust Web Starter project](https://github.com/ghotiphud/rust-web-starter) the Dockerfile and docker-compose were edited to connect to the database properly, to not re-install cargo packages each time docker-compose is run, to automatically re-compile code changes, and to use the Diesel CLI.

## Docker Compose

Expand All @@ -18,9 +18,15 @@ Sometimes docker-compose leaves behind ports, volumes, etc. This can interfere w
docker-compose down -v
```

## Rust Tools

Rust has a code formatting tool called Rustfmt, which allows us to define a style and have it easily enforced. All you need to do is run `cargo fmt` (as described below) and all the source code in this project will be updated to match our style guidelines!

Clippy is a smart linter for Rust that can be run as an extra check on the code in the project. By default it checks all the dependencies and the source code, before only checking changes. It can be run by using `cargo clippy` (as described below).

## Additional Commands

The [diesel CLI](http://diesel.rs/) is designed to help create databases, change the databases properties (called migrations), and undo migrations if there is an issue (all while keeping the data safe).
The [Diesel CLI](http://diesel.rs/) is designed to help create databases, change the databases properties (called migrations), and undo migrations if there is an issue (all while keeping the data safe).

So to run these commands first start the instance with

Expand All @@ -34,8 +40,10 @@ and then in a new terminal jump into that containers shell by
docker-compose exec backend bash
```

From there all normal diesel or rust maintenance commands can be run. Please go to the official documentation for details, a list of commonly used commands are below.
From there all normal Diesel or Rust maintenance commands can be run. Please go to the official documentation for details, a list of commonly used commands are below.
- `cargo upgrade`
- `cargo fmt`
- `cargo clippy`
- `diesel setup`
- `diesel migration generate {name}`
- `diesel migration run`
Expand Down
25 changes: 10 additions & 15 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,35 +1,30 @@
#![feature(proc_macro_hygiene, decl_macro)]

#[macro_use] extern crate rocket;
#[macro_use] extern crate rocket_contrib;
// #[macro_use] extern crate diesel;
#[macro_use] extern crate dotenv;
#[macro_use]
extern crate rocket;
#[macro_use]
extern crate rocket_contrib;

use rocket_contrib::databases::diesel;
use diesel::prelude::*;
use diesel::pg::PgConnection;
use dotenv::dotenv;
use diesel::prelude::*;
use rocket_contrib::databases::diesel;
use std::env;

#[database("club_data")]
struct ClubDbConn(diesel::PgConnection);

pub fn establish_connection() -> PgConnection {
dotenv().ok();

let database_url = env::var("DATABASE_URL")
.expect("DATABASE_URL must be set");
PgConnection::establish(&database_url)
.expect(&format!("Error connecting to {}", database_url))
let database_url = env::var("DATABASE_URL").expect("DATABASE_URL must be set");
PgConnection::establish(&database_url).expect(&format!("Error connecting to {}", database_url))
}

#[get("/")]
fn index() -> &'static str {
"Hello, world!"
"Hello, world!"
}

fn main() {
rocket::ignite()
rocket::ignite()
.attach(ClubDbConn::fairing())
.mount("/", routes![index])
.launch();
Expand Down

0 comments on commit c2b19d6

Please sign in to comment.