~lthms/ogmios

70d12849d18ca50f9c1707f9de76bb9cf56fc9dd — Thomas Letan 1 year, 2 months ago 4b60516
refactor: Use the rocket_contrib newly introduced support for da

With rocket_contrib-0.4.0-rc.1, a new feature has been introduced in
order to easily share a database connection among route handler. It
was already easy before using managed state, but as it turned out, the
author decided to provide a standard implementation.

Once again, I really feel like it is a good call, and this allows for
removing code in ogmios which is great.
5 files changed, 26 insertions(+), 53 deletions(-)

M Cargo.lock
M Cargo.toml
M Rocket.toml
M src/db.rs
M src/main.rs
M Cargo.lock => Cargo.lock +15 -0
@@ 896,16 896,30 @@ name = "rocket_contrib"
version = "0.4.0-rc.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
 "diesel 1.3.3 (registry+https://github.com/rust-lang/crates.io-index)",
 "glob 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)",
 "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
 "notify 4.0.6 (registry+https://github.com/rust-lang/crates.io-index)",
 "r2d2 0.8.2 (registry+https://github.com/rust-lang/crates.io-index)",
 "rocket 0.4.0-rc.1 (registry+https://github.com/rust-lang/crates.io-index)",
 "rocket_contrib_codegen 0.4.0-rc.1 (registry+https://github.com/rust-lang/crates.io-index)",
 "serde 1.0.80 (registry+https://github.com/rust-lang/crates.io-index)",
 "serde_json 1.0.32 (registry+https://github.com/rust-lang/crates.io-index)",
 "tera 0.11.19 (registry+https://github.com/rust-lang/crates.io-index)",
]

[[package]]
name = "rocket_contrib_codegen"
version = "0.4.0-rc.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
 "devise 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
 "quote 0.6.9 (registry+https://github.com/rust-lang/crates.io-index)",
 "version_check 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)",
 "yansi 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
]

[[package]]
name = "rocket_http"
version = "0.4.0-rc.1"
source = "registry+https://github.com/rust-lang/crates.io-index"


@@ 1487,6 1501,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
"checksum rocket 0.4.0-rc.1 (registry+https://github.com/rust-lang/crates.io-index)" = "71f9970125ea965ae6e468a5bb878f47762aff210596e4cfc2d4ee9af2365b0f"
"checksum rocket_codegen 0.4.0-rc.1 (registry+https://github.com/rust-lang/crates.io-index)" = "9a9adbb359e17dabd05d97aa05a328817a20db5ba6b694cd51bba2900b262691"
"checksum rocket_contrib 0.4.0-rc.1 (registry+https://github.com/rust-lang/crates.io-index)" = "38cce3e4f0de0b950604c2a5cda4b3d74a7f17c62937cee54f4996b36c2d31a5"
"checksum rocket_contrib_codegen 0.4.0-rc.1 (registry+https://github.com/rust-lang/crates.io-index)" = "2f6c15b94784d695d6624aab9dc6ca19512c0f853cf1bc473b81aee05e6b899a"
"checksum rocket_http 0.4.0-rc.1 (registry+https://github.com/rust-lang/crates.io-index)" = "2201c854709fd6f8cc7dca9bd7ebbddb10f27e1cdda191f932546c3dcd98d4a6"
"checksum rustc-demangle 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)" = "bcfe5b13211b4d78e5c2cadfebd7769197d95c639c35a50057eb4c05de811395"
"checksum rustc_version 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "138e3e0acb6c9fb258b19b67cb8abd63c00679d2851805ea151465464fe9030a"

M Cargo.toml => Cargo.toml +2 -2
@@ 15,11 15,11 @@ base64            = "0.9"

[dependencies.diesel]
version  = "1.3"
features = ["postgres", "r2d2"]
features = ["postgres"]

[dependencies.rocket_contrib]
version  = "0.4.0-rc.1"
features = ["tera_templates", "json"]
features = ["tera_templates", "json", "diesel_postgres_pool"]

[dependencies.ogmarkup]
features = ["html"]

M Rocket.toml => Rocket.toml +3 -0
@@ 1,2 1,5 @@
[global.databases]
pg_url = { url = "postgres://ogmios@localhost/ogmios" }

[development]
limits = { forms = 524288 }
\ No newline at end of file

M src/db.rs => src/db.rs +4 -47
@@ 1,57 1,14 @@
use diesel::Connection;
use diesel::pg::PgConnection;
use diesel::r2d2::{PooledConnection, ConnectionManager, Pool};
use diesel::result::DatabaseErrorKind;
use diesel::result::Error as DieselError;
use ::errors::Error;

use rocket::request::{FromRequest, Request};
use rocket::State;
use rocket::Outcome;
use rocket::http::Status;

use ::errors::{Error, ResultExt};

pub type PgPool = Pool<ConnectionManager<PgConnection>>;
pub struct PgConn(PooledConnection<ConnectionManager<PgConnection>>);

pub fn establish_connection() -> Result<PgPool, Error> {
    let manager = ConnectionManager::new("postgres://ogmios@localhost/ogmios");

    Pool::new(manager)
        .chain_err(|| "Unable to connect to the database")
}

pub fn uniqueness_violation(err: DieselError) -> bool {
    match err {
        DieselError::DatabaseError(DatabaseErrorKind::UniqueViolation, _) => {
            true
        },
        _ => {
            false
        }
    }
}

impl<'a, 'r> FromRequest<'a, 'r> for PgConn {
    type Error = ();

    fn from_request(request: &'a Request<'r>) -> Outcome<PgConn, (Status, ()), ()> {
        let pool = request.guard::<State<PgPool>>()?;

        match pool.get() {
            Ok(conn) => Outcome::Success(PgConn(conn)),
            Err(_) => Outcome::Failure((Status::ServiceUnavailable, ()))
        }
    }
}
#[database("pg_url")]
pub struct PgConn(diesel::PgConnection);

impl PgConn {
    pub fn get(&self) -> &PgConnection {
    pub fn get(&self) -> &diesel::PgConnection {
        &self.0
    }
}

impl PgConn {
    pub fn transaction<A, F>(
        &self,
        f: F

M src/main.rs => src/main.rs +2 -4
@@ 37,7 37,7 @@ pub mod models;
pub mod schema;
pub mod routes;

use db::{PgConn, establish_connection};
use db::PgConn;

use ::schema::users;
use ::errors::{Error, ResultExt};


@@ 104,8 104,6 @@ fn static_files(file: PathBuf) -> Option<NamedFile> {
}

fn run() -> Result <(), Error> {
    let conn = establish_connection()?;

    rocket::ignite()
        .mount("/", routes![
            index_auth,


@@ 125,7 123,7 @@ fn run() -> Result <(), Error> {
            routes::characters::edit_future_sheet_post,
            static_files,
        ])
        .manage(conn)
        .attach(PgConn::fairing())
        .attach(Template::fairing())
        .launch();