From f571f062fa8bce2d14cddf0aa423ef5593d5afeb Mon Sep 17 00:00:00 2001 From: trivernis Date: Thu, 10 Sep 2020 12:28:38 +0200 Subject: [PATCH] Add redis connection to all models Signed-off-by: trivernis --- .env | 3 +- .idea/workspace.xml | 34 +-- Cargo.lock | 429 +++++++++++++++++++++++++++++++ Cargo.toml | 3 +- src/database/mod.rs | 81 ++++-- src/database/permissions.rs | 29 ++- src/database/role_permissions.rs | 27 +- src/database/roles.rs | 30 ++- src/database/user_roles.rs | 27 +- src/database/users.rs | 30 ++- 10 files changed, 615 insertions(+), 78 deletions(-) diff --git a/.env b/.env index d305050..b47f53e 100644 --- a/.env +++ b/.env @@ -1 +1,2 @@ -POSTGRES_CONNECTION_URL = "postgres://flotte_userdb:flottedev@localhost/flotte_usersdb" \ No newline at end of file +POSTGRES_CONNECTION_URL = "postgres://flotte_userdb:flottedev@localhost/flotte_usersdb" +REDIS_CONNECTION_URL = "redis://127.0.0.1/" \ No newline at end of file diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 917d5e7..d6c29b2 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -13,14 +13,16 @@ - - - - + + + + + + + - @@ -103,22 +105,22 @@ - + - - + + - - + + - - + + - + @@ -131,9 +133,9 @@ - + - + \ No newline at end of file diff --git a/Cargo.lock b/Cargo.lock index dee7c87..8daa560 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,5 +1,91 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. +[[package]] +name = "async-channel" +version = "1.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "21279cfaa4f47df10b1816007e738ca3747ef2ee53ffc51cdbf57a8bb266fee3" +dependencies = [ + "concurrent-queue", + "event-listener", + "futures-core", +] + +[[package]] +name = "async-executor" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "90f47c78ea98277cb1f5e6f60ba4fc762f5eafe9f6511bc2f7dfd8b75c225650" +dependencies = [ + "async-io", + "futures-lite", + "multitask", + "parking 1.0.6", + "scoped-tls", + "waker-fn", +] + +[[package]] +name = "async-io" +version = "0.1.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3ae22a338d28c75b53702b66f77979062cb29675db376d99e451af4fa79dedb3" +dependencies = [ + "cfg-if", + "concurrent-queue", + "futures-lite", + "libc", + "once_cell", + "parking 2.0.0", + "polling", + "socket2", + "vec-arena", + "wepoll-sys-stjepang", + "winapi 0.3.9", +] + +[[package]] +name = "async-mutex" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "66941c2577c4fa351e4ce5fdde8f86c69b88d623f3b955be1bc7362a23434632" +dependencies = [ + "event-listener", +] + +[[package]] +name = "async-std" +version = "1.6.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "46c8da367da62b8ff2313c406c9ac091c1b31d67a165becdd2de380d846260f7" +dependencies = [ + "async-executor", + "async-io", + "async-mutex", + "async-task", + "blocking", + "crossbeam-utils", + "futures-channel", + "futures-core", + "futures-io", + "futures-lite", + "kv-log-macro", + "log", + "memchr", + "num_cpus", + "once_cell", + "pin-project-lite", + "pin-utils", + "slab", + "wasm-bindgen-futures", +] + +[[package]] +name = "async-task" +version = "3.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c17772156ef2829aadc587461c7753af20b7e8db1529bc66855add962a3b35d3" + [[package]] name = "async-trait" version = "0.1.40" @@ -11,6 +97,12 @@ dependencies = [ "syn", ] +[[package]] +name = "atomic-waker" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "065374052e7df7ee4047b1160cca5e1467a12351a40b3da123c870ba0b8eda2a" + [[package]] name = "autocfg" version = "1.0.1" @@ -38,12 +130,31 @@ dependencies = [ "generic-array", ] +[[package]] +name = "blocking" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ea5800d29218fea137b0880387e5948694a23c93fcdde157006966693a865c7c" +dependencies = [ + "async-channel", + "atomic-waker", + "futures-lite", + "once_cell", + "waker-fn", +] + [[package]] name = "build_const" version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "39092a32794787acd8525ee150305ff051b0aa6cc2abaf193924f5ab05425f39" +[[package]] +name = "bumpalo" +version = "3.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2e8c087f005730276d1096a652e92a8bacee2e2472bcc9715a74d2bec38b5820" + [[package]] name = "byteorder" version = "1.3.4" @@ -56,6 +167,18 @@ version = "0.5.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0e4cec68f03f32e44924783795810fa50a7035d8c8ebe78580ad7e6c703fba38" +[[package]] +name = "cache-padded" +version = "1.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "631ae5198c9be5e753e5cc215e1bd73c2b466a3565173db433f52bb9d3e66dba" + +[[package]] +name = "cc" +version = "1.0.59" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "66120af515773fb005778dc07c261bd201ec8ce50bd6e7144c927753fe013381" + [[package]] name = "cfg-if" version = "0.1.10" @@ -71,6 +194,28 @@ dependencies = [ "bitflags", ] +[[package]] +name = "combine" +version = "4.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2809f67365382d65fd2b6d9c22577231b954ed27400efeafbe687bda75abcc0b" +dependencies = [ + "bytes", + "futures-util", + "memchr", + "pin-project-lite", + "tokio", +] + +[[package]] +name = "concurrent-queue" +version = "1.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "30ed07550be01594c6026cff2a1d7fe9c8f683caa798e12b68694ac9e88286a3" +dependencies = [ + "cache-padded", +] + [[package]] name = "cpuid-bool" version = "0.1.2" @@ -122,12 +267,30 @@ version = "0.15.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "77c90badedccf4105eca100756a0b1289e191f6fcbdadd3cee1d2f614f97da8f" +[[package]] +name = "dtoa" +version = "0.4.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "134951f4028bdadb9b84baf4232681efbf277da25144b9b0ad65df75946c422b" + +[[package]] +name = "event-listener" +version = "2.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e1cd41440ae7e4734bbd42302f63eaba892afc93a3912dad84006247f0dedb0e" + [[package]] name = "fallible-iterator" version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4443176a9f2c162692bd3d352d745ef9413eec5782a80d8fd6f8a1ac692a07f7" +[[package]] +name = "fastrand" +version = "1.3.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c85295147490b8fcf2ea3d104080a105a8b2c63f9c319e82c02d8e952388919" + [[package]] name = "flotte-user-management" version = "0.1.0" @@ -135,8 +298,15 @@ dependencies = [ "dotenv", "msgrpc", "postgres", + "redis", ] +[[package]] +name = "fnv" +version = "1.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" + [[package]] name = "fuchsia-zircon" version = "0.3.3" @@ -201,6 +371,21 @@ version = "0.3.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "de27142b013a8e869c14957e6d2edeef89e97c289e69d042ee3a49acd8b51789" +[[package]] +name = "futures-lite" +version = "0.1.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "97999970129b808f0ccba93211201d431fcc12d7e1ffae03a61b5cedd1a7ced2" +dependencies = [ + "fastrand", + "futures-core", + "futures-io", + "memchr", + "parking 2.0.0", + "pin-project-lite", + "waker-fn", +] + [[package]] name = "futures-macro" version = "0.3.5" @@ -269,6 +454,15 @@ dependencies = [ "wasi", ] +[[package]] +name = "hermit-abi" +version = "0.1.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3deed196b6e7f9e44a2ae8d94225d80302d81208b1bb673fd21fe634645c85a9" +dependencies = [ + "libc", +] + [[package]] name = "hmac" version = "0.8.1" @@ -279,6 +473,17 @@ dependencies = [ "digest", ] +[[package]] +name = "idna" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "02e2673c30ee86b5b96a9cb52ad15718aa1f966f5ab9ad54a8b95d5ca33120a9" +dependencies = [ + "matches", + "unicode-bidi", + "unicode-normalization", +] + [[package]] name = "instant" version = "0.1.6" @@ -294,6 +499,21 @@ dependencies = [ "libc", ] +[[package]] +name = "itoa" +version = "0.4.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc6f3ad7b9d11a0c00842ff8de1b60ee58661048eb8049ed33c73594f359d7e6" + +[[package]] +name = "js-sys" +version = "0.3.45" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ca059e81d9486668f12d455a4ea6daa600bd408134cd17e3d3fb5a32d1f016f8" +dependencies = [ + "wasm-bindgen", +] + [[package]] name = "kernel32-sys" version = "0.2.2" @@ -304,6 +524,15 @@ dependencies = [ "winapi-build", ] +[[package]] +name = "kv-log-macro" +version = "1.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0de8b303297635ad57c9f5059fd9cee7a47f8e8daa09df0fcd07dd39fb22977f" +dependencies = [ + "log", +] + [[package]] name = "lazy_static" version = "1.4.0" @@ -407,6 +636,17 @@ dependencies = [ "serde", ] +[[package]] +name = "multitask" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c09c35271e7dcdb5f709779111f2c8e8ab8e06c1b587c1c6a9e179d865aaa5b4" +dependencies = [ + "async-task", + "concurrent-queue", + "fastrand", +] + [[package]] name = "net2" version = "0.2.35" @@ -427,6 +667,16 @@ dependencies = [ "autocfg", ] +[[package]] +name = "num_cpus" +version = "1.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "05499f3756671c15885fee9034446956fff3f243d6077b91e5767df161f766b3" +dependencies = [ + "hermit-abi", + "libc", +] + [[package]] name = "once_cell" version = "1.4.1" @@ -439,6 +689,18 @@ version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "624a8340c38c1b80fd549087862da4ba43e08858af025b236e509b6649fc13d5" +[[package]] +name = "parking" +version = "1.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6cb300f271742d4a2a66c01b6b2fa0c83dfebd2e0bf11addb879a3547b4ed87c" + +[[package]] +name = "parking" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "427c3892f9e783d91cc128285287e70a59e206ca452770ece88a76f7a3eddd72" + [[package]] name = "parking_lot" version = "0.11.0" @@ -521,6 +783,19 @@ version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" +[[package]] +name = "polling" +version = "0.1.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8fffa183f6bd5f1a8a3e1f60ce2f8d5621e350eed84a62d6daaa5b9d1aaf6fbd" +dependencies = [ + "cfg-if", + "libc", + "log", + "wepoll-sys-stjepang", + "winapi 0.3.9", +] + [[package]] name = "postgres" version = "0.17.5" @@ -641,6 +916,27 @@ dependencies = [ "rand_core", ] +[[package]] +name = "redis" +version = "0.17.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "95357caf2640abc54651b93c98a8df4fe1ccbf44b8e601ccdf43d5c1451f29ac" +dependencies = [ + "async-std", + "async-trait", + "bytes", + "combine", + "dtoa", + "futures-util", + "itoa", + "percent-encoding", + "pin-project-lite", + "sha1", + "tokio", + "tokio-util", + "url", +] + [[package]] name = "redox_syscall" version = "0.1.57" @@ -668,6 +964,12 @@ dependencies = [ "serde", ] +[[package]] +name = "scoped-tls" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ea6a9290e3c9cf0f18145ef7ffa62d68ee0bf5fcd651017e586dc7fd5da448c2" + [[package]] name = "scopeguard" version = "1.1.0" @@ -680,6 +982,12 @@ version = "1.0.115" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e54c9a88f2da7238af84b5101443f0c0d0a3bbdc455e34a5c9497b1903ed55d5" +[[package]] +name = "sha1" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2579985fda508104f7587689507983eadd6a6e84dd35d6d115361f530916fa0d" + [[package]] name = "sha2" version = "0.9.1" @@ -711,6 +1019,18 @@ version = "1.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fbee7696b84bbf3d89a1c2eccff0850e3047ed46bfcd2e92c29a2d074d57e252" +[[package]] +name = "socket2" +version = "0.3.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b1fa70dc5c8104ec096f4fe7ede7a221d35ae13dcd19ba1ad9a81d2cab9a1c44" +dependencies = [ + "cfg-if", + "libc", + "redox_syscall", + "winapi 0.3.9", +] + [[package]] name = "stringprep" version = "0.1.2" @@ -751,6 +1071,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5d34ca54d84bf2b5b4d7d31e901a8464f7b60ac145a284fba25ceb801f2ddccd" dependencies = [ "bytes", + "fnv", "futures-core", "iovec", "lazy_static", @@ -828,18 +1149,126 @@ version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f7fe0bb3479651439c9112f72b6c505038574c9fbb575ed1bf3b797fa39dd564" +[[package]] +name = "url" +version = "2.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "829d4a8476c35c9bf0bbce5a3b23f4106f79728039b726d292bb93bc106787cb" +dependencies = [ + "idna", + "matches", + "percent-encoding", +] + +[[package]] +name = "vec-arena" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8cb18268690309760d59ee1a9b21132c126ba384f374c59a94db4bc03adeb561" + [[package]] name = "version_check" version = "0.9.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b5a972e5669d67ba988ce3dc826706fb0a8b01471c088cb0b6110b805cc36aed" +[[package]] +name = "waker-fn" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9d5b2c62b4012a3e1eca5a7e077d13b3bf498c4073e33ccd58626607748ceeca" + [[package]] name = "wasi" version = "0.9.0+wasi-snapshot-preview1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cccddf32554fecc6acb585f82a32a72e28b48f8c4c1883ddfeeeaa96f7d8e519" +[[package]] +name = "wasm-bindgen" +version = "0.2.68" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1ac64ead5ea5f05873d7c12b545865ca2b8d28adfc50a49b84770a3a97265d42" +dependencies = [ + "cfg-if", + "wasm-bindgen-macro", +] + +[[package]] +name = "wasm-bindgen-backend" +version = "0.2.68" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f22b422e2a757c35a73774860af8e112bff612ce6cb604224e8e47641a9e4f68" +dependencies = [ + "bumpalo", + "lazy_static", + "log", + "proc-macro2", + "quote", + "syn", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-futures" +version = "0.4.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b7866cab0aa01de1edf8b5d7936938a7e397ee50ce24119aef3e1eaa3b6171da" +dependencies = [ + "cfg-if", + "js-sys", + "wasm-bindgen", + "web-sys", +] + +[[package]] +name = "wasm-bindgen-macro" +version = "0.2.68" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6b13312a745c08c469f0b292dd2fcd6411dba5f7160f593da6ef69b64e407038" +dependencies = [ + "quote", + "wasm-bindgen-macro-support", +] + +[[package]] +name = "wasm-bindgen-macro-support" +version = "0.2.68" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f249f06ef7ee334cc3b8ff031bfc11ec99d00f34d86da7498396dc1e3b1498fe" +dependencies = [ + "proc-macro2", + "quote", + "syn", + "wasm-bindgen-backend", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-shared" +version = "0.2.68" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1d649a3145108d7d3fbcde896a468d1bd636791823c9921135218ad89be08307" + +[[package]] +name = "web-sys" +version = "0.3.45" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4bf6ef87ad7ae8008e15a355ce696bed26012b7caa21605188cfd8214ab51e2d" +dependencies = [ + "js-sys", + "wasm-bindgen", +] + +[[package]] +name = "wepoll-sys-stjepang" +version = "1.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6fd319e971980166b53e17b1026812ad66c6b54063be879eb182342b55284694" +dependencies = [ + "cc", +] + [[package]] name = "winapi" version = "0.2.8" diff --git a/Cargo.toml b/Cargo.toml index c4d9499..127f6b5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,4 +9,5 @@ edition = "2018" [dependencies] msgrpc = {path = "./msg-rpc"} postgres = "0.17.5" -dotenv = "0.15.0" \ No newline at end of file +dotenv = "0.15.0" +redis = "0.17.0" \ No newline at end of file diff --git a/src/database/mod.rs b/src/database/mod.rs index 3ad7b0e..cf8e835 100644 --- a/src/database/mod.rs +++ b/src/database/mod.rs @@ -4,7 +4,8 @@ use crate::database::roles::Roles; use crate::database::user_roles::UserRoles; use crate::database::users::Users; use dotenv; -use postgres::{Client, Error, NoTls}; +use postgres::{Client, NoTls}; +use redis::{RedisError, RedisResult}; use std::sync::{Arc, Mutex}; pub mod permissions; @@ -14,16 +15,36 @@ pub mod user_roles; pub mod users; const DB_CONNECTION_URL: &str = "POSTGRES_CONNECTION_URL"; -const DEFAULT_CONNECTION: &str = "postgres://postgres:postgres@localhost/postgrees"; +const DEFAULT_CONNECTION: &str = "postgres://postgres:postgres@localhost/postgres"; +const REDIS_CONNECTION_URL: &str = "REDIS_CONNECTION_URL"; +const DEFAULT_REDIS_CONNECTION: &str = "redis:://127.0.0.1/"; + +pub type DatabaseClient = postgres::Client; +pub type RedisClient = redis::Client; +pub type RedisConnection = redis::Connection; +pub type PostgresError = postgres::Error; pub trait Model { - fn new(connection: Arc>) -> Self; - fn init(&self) -> Result<(), Error>; + fn new( + database_connection: Arc>, + redis_connection: Arc>, + ) -> Self; + fn init(&self) -> DatabaseResult<()>; +} + +#[derive(Debug)] +pub enum Error { + Redis(RedisError), + Postgres(PostgresError), } +pub type DatabaseError = Error; +pub type DatabaseResult = Result; + #[derive(Clone)] pub struct Database { - connection: Arc>, + database_connection: Arc>, + redis_connection: Arc>, pub users: Users, pub roles: Roles, pub permissions: Permissions, @@ -31,23 +52,42 @@ pub struct Database { user_roles: UserRoles, } -type PostgresResult = Result; - impl Database { - pub fn new() -> PostgresResult { - let connection = Arc::new(Mutex::new(get_connection()?)); + pub fn new() -> DatabaseResult { + let database_connection = Arc::new(Mutex::new( + get_database_connection().map_err(|e| Error::Postgres(e))?, + )); + let redis_connection = Arc::new(Mutex::new( + get_redis_connection().map_err(|e| Error::Redis(e))?, + )); Ok(Self { - users: Users::new(Arc::clone(&connection)), - roles: Roles::new(Arc::clone(&connection)), - permissions: Permissions::new(Arc::clone(&connection)), - user_roles: UserRoles::new(Arc::clone(&connection)), - role_permission: RolePermissions::new(Arc::clone(&connection)), - connection, + users: Users::new( + Arc::clone(&database_connection), + Arc::clone(&redis_connection), + ), + roles: Roles::new( + Arc::clone(&database_connection), + Arc::clone(&redis_connection), + ), + permissions: Permissions::new( + Arc::clone(&database_connection), + Arc::clone(&redis_connection), + ), + user_roles: UserRoles::new( + Arc::clone(&database_connection), + Arc::clone(&redis_connection), + ), + role_permission: RolePermissions::new( + Arc::clone(&database_connection), + Arc::clone(&redis_connection), + ), + database_connection, + redis_connection, }) } /// Inits all database models - pub fn init(&self) -> PostgresResult<()> { + pub fn init(&self) -> DatabaseResult<()> { self.users.init()?; self.roles.init()?; self.permissions.init()?; @@ -58,7 +98,14 @@ impl Database { } } /// Returns a database connection -fn get_connection() -> Result { +fn get_database_connection() -> Result { let conn_url = dotenv::var(DB_CONNECTION_URL).unwrap_or(DEFAULT_CONNECTION.to_string()); Client::connect(conn_url.as_str(), NoTls) } + +fn get_redis_connection() -> RedisResult { + let conn_url = + dotenv::var(REDIS_CONNECTION_URL).unwrap_or(DEFAULT_REDIS_CONNECTION.to_string()); + let client = RedisClient::open(conn_url)?; + client.get_connection() +} diff --git a/src/database/permissions.rs b/src/database/permissions.rs index dd223e1..43f35d5 100644 --- a/src/database/permissions.rs +++ b/src/database/permissions.rs @@ -1,24 +1,35 @@ -use crate::database::Model; -use postgres::{Client, Error}; +use crate::database::{DatabaseClient, DatabaseError, DatabaseResult, Model, RedisConnection}; +use postgres::Client; use std::sync::{Arc, Mutex}; #[derive(Clone)] pub struct Permissions { - connection: Arc>, + database_connection: Arc>, + redis_connection: Arc>, } impl Model for Permissions { - fn new(connection: Arc>) -> Self { - Self { connection } + fn new( + database_connection: Arc>, + redis_connection: Arc>, + ) -> Self { + Self { + database_connection, + redis_connection, + } } - fn init(&self) -> Result<(), Error> { - self.connection.lock().unwrap().batch_execute( - "CREATE TABLE IF NOT EXISTS permissions ( + fn init(&self) -> DatabaseResult<()> { + self.database_connection + .lock() + .unwrap() + .batch_execute( + "CREATE TABLE IF NOT EXISTS permissions ( id SERIAL PRIMARY KEY, name VARCHAR(128) UNIQUE NOT NULL, description VARCHAR(512) );", - ) + ) + .map_err(|e| DatabaseError::Postgres(e)) } } diff --git a/src/database/role_permissions.rs b/src/database/role_permissions.rs index 853b11f..8ba6aec 100644 --- a/src/database/role_permissions.rs +++ b/src/database/role_permissions.rs @@ -1,25 +1,36 @@ -use crate::database::Model; +use crate::database::{DatabaseClient, DatabaseError, DatabaseResult, Model, RedisConnection}; use postgres::{Client, Error}; use std::sync::{Arc, Mutex}; #[derive(Clone)] pub struct RolePermissions { - connection: Arc>, + database_connection: Arc>, + redis_connection: Arc>, } impl Model for RolePermissions { - fn new(connection: Arc>) -> Self { - Self { connection } + fn new( + database_connection: Arc>, + redis_connection: Arc>, + ) -> Self { + Self { + database_connection, + redis_connection, + } } - fn init(&self) -> Result<(), Error> { - self.connection.lock().unwrap().batch_execute( - " + fn init(&self) -> DatabaseResult<()> { + self.database_connection + .lock() + .unwrap() + .batch_execute( + " CREATE TABLE IF NOT EXISTS role_permissions ( role_id INT NOT NULL REFERENCES roles(id) ON DELETE CASCADE, permission_id INT NOT NULL REFERENCES permissions(id) ON DELETE CASCADE, PRIMARY KEY (role_id, permission_id) );", - ) + ) + .map_err(|e| DatabaseError::Postgres(e)) } } diff --git a/src/database/roles.rs b/src/database/roles.rs index c8a61f1..bb0d8ac 100644 --- a/src/database/roles.rs +++ b/src/database/roles.rs @@ -1,30 +1,42 @@ use crate::database::role_permissions::RolePermissions; -use crate::database::Model; +use crate::database::{DatabaseError, DatabaseResult, Model, RedisConnection}; use postgres::{Client, Error}; use std::sync::{Arc, Mutex}; #[derive(Clone)] pub struct Roles { - connection: Arc>, + database_connection: Arc>, + redis_connection: Arc>, role_permission: RolePermissions, } impl Model for Roles { - fn new(connection: Arc>) -> Self { + fn new( + database_connection: Arc>, + redis_connection: Arc>, + ) -> Self { Self { - role_permission: RolePermissions::new(Arc::clone(&connection)), - connection, + role_permission: RolePermissions::new( + Arc::clone(&database_connection), + Arc::clone(&redis_connection), + ), + database_connection, + redis_connection, } } - fn init(&self) -> Result<(), Error> { - self.connection.lock().unwrap().batch_execute( - " + fn init(&self) -> DatabaseResult<()> { + self.database_connection + .lock() + .unwrap() + .batch_execute( + " CREATE TABLE IF NOT EXISTS roles ( id SERIAL PRIMARY KEY, name VARCHAR(128) UNIQUE NOT NULL, description VARCHAR(512) );", - ) + ) + .map_err(|e| DatabaseError::Postgres(e)) } } diff --git a/src/database/user_roles.rs b/src/database/user_roles.rs index 6cca12c..e36cd27 100644 --- a/src/database/user_roles.rs +++ b/src/database/user_roles.rs @@ -1,25 +1,36 @@ -use crate::database::Model; +use crate::database::{DatabaseError, DatabaseResult, Model, RedisConnection}; use postgres::{Client, Error}; use std::sync::{Arc, Mutex}; #[derive(Clone)] pub struct UserRoles { - connection: Arc>, + database_connection: Arc>, + redis_connection: Arc>, } impl Model for UserRoles { - fn new(connection: Arc>) -> Self { - Self { connection } + fn new( + database_connection: Arc>, + redis_connection: Arc>, + ) -> Self { + Self { + database_connection, + redis_connection, + } } - fn init(&self) -> Result<(), Error> { - self.connection.lock().unwrap().batch_execute( - " + fn init(&self) -> DatabaseResult<()> { + self.database_connection + .lock() + .unwrap() + .batch_execute( + " CREATE TABLE IF NOT EXISTS user_roles ( user_id INT NOT NULL REFERENCES users(id) ON DELETE CASCADE, role_id INT NOT NULL REFERENCES roles(id) ON DELETE CASCADE, PRIMARY KEY (user_id, role_id) );", - ) + ) + .map_err(|e| DatabaseError::Postgres(e)) } } diff --git a/src/database/users.rs b/src/database/users.rs index 999b27c..4b13f04 100644 --- a/src/database/users.rs +++ b/src/database/users.rs @@ -1,31 +1,43 @@ use crate::database::user_roles::UserRoles; -use crate::database::Model; +use crate::database::{DatabaseError, DatabaseResult, Model, RedisConnection}; use postgres::{Client, Error}; use std::sync::{Arc, Mutex}; #[derive(Clone)] pub struct Users { - connection: Arc>, + database_connection: Arc>, + redis_connection: Arc>, user_roles: UserRoles, } impl Model for Users { - fn new(connection: Arc>) -> Self { + fn new( + database_connection: Arc>, + redis_connection: Arc>, + ) -> Self { Self { - user_roles: UserRoles::new(Arc::clone(&connection)), - connection, + user_roles: UserRoles::new( + Arc::clone(&database_connection), + Arc::clone(&redis_connection), + ), + database_connection, + redis_connection, } } - fn init(&self) -> Result<(), Error> { - self.connection.lock().unwrap().batch_execute( - "CREATE TABLE IF NOT EXISTS users ( + fn init(&self) -> DatabaseResult<()> { + self.database_connection + .lock() + .unwrap() + .batch_execute( + "CREATE TABLE IF NOT EXISTS users ( id SERIAL PRIMARY KEY, name VARCHAR(255) NOT NULL, email VARCHAR(255) UNIQUE NOT NULL, password_hash VARCHAR(32) NOT NULL, salt VARCHAR(16) NOT NULL );", - ) + ) + .map_err(|e| DatabaseError::Postgres(e)) } }