diff --git a/.vscode/settings.json b/.vscode/settings.json index 68b20b9..c51e4b3 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -19,8 +19,7 @@ "target": true, // 📝 readmes - "**/**/README.md": true, - "LICENSE": true, + // "**/**/README.md": true, // 🗑️ "check_size.py": true, diff --git a/Cargo.lock b/Cargo.lock index 96ae286..5d6e975 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1095,6 +1095,7 @@ version = "0.1.0" dependencies = [ "entities", "eyre", + "log", "prost", "tonic", "tonic-build", @@ -2249,6 +2250,7 @@ dependencies = [ "entities", "eyre", "grpc", + "log", "tokio", ] diff --git a/Cargo.toml b/Cargo.toml index e22c3b5..1e570be 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -24,12 +24,19 @@ path = "app/main.rs" [workspace] members = [".", "migration", "entities", "grpc"] +[workspace.dependencies] +log = { version = "0.4.21" } +eyre = { version = "0.6.12" } + [dependencies] # internal entities = { path = "entities" } grpc = { path = "grpc" } +# workspace +log = { workspace = true } +eyre = { workspace = true, default-features = false, features = ["auto-install"] } + # external -eyre = { version = "0.6.12", default-features = false, features = ["auto-install"] } tokio = { version = "1.36.0", features = ["macros", "rt-multi-thread"] } dotenvy = "0.15.7" diff --git a/README.md b/README.md index f4abea1..022e55e 100644 --- a/README.md +++ b/README.md @@ -27,6 +27,9 @@ ## Commands ```bash -# run migrations -task db:migrate +# add a dependency to a project +cargo add {dependency} -p {project} + +# example +cargo add tokio-tungstenite -p gateway ``` \ No newline at end of file diff --git a/app/main.rs b/app/main.rs index 320c72f..6497c6c 100644 --- a/app/main.rs +++ b/app/main.rs @@ -1,9 +1,4 @@ -use { - dotenvy::dotenv, - entities::sea_orm::{ConnectionTrait, Database, DbBackend, Statement}, - eyre::Result, - std::sync::Arc, -}; +use {dotenvy::dotenv, eyre::Result, log::info}; // TODO: here we will trigger the start of both the grpc server and the websocket gateway // look at: https://github.com/hyperium/tonic/discussions/740 @@ -11,19 +6,9 @@ use { #[tokio::main] async fn main() -> Result<()> { dotenv()?; + let conn = entities::db::get_connection().await?; - let db_conn_str = std::env::var("DATABASE_URL")?; - // let conn = Arc::new(); - let conn = Arc::new(Database::connect(&db_conn_str).await?); - - conn.query_one(Statement::from_string( - DbBackend::Sqlite, - r#" - PRAGMA journal_mode = WAL; - PRAGMA synchronous = NORMAL; - "#, - )) - .await?; + info!("Starting gRPC server..."); grpc::server::start(conn.clone()).await?; diff --git a/entities/Cargo.toml b/entities/Cargo.toml index be27294..e040df0 100644 --- a/entities/Cargo.toml +++ b/entities/Cargo.toml @@ -9,5 +9,12 @@ name = "entities" path = "src/lib.rs" [dependencies] -sea-orm = { version = "0.12.15", features = ["runtime-tokio-native-tls", "sqlx-sqlite", "macros"] } -eyre = { version = "0.6.12", default-features = false } +# common +eyre = { workspace = true, default-features = false } + +# external +sea-orm = { version = "0.12.15", features = [ + "runtime-tokio-native-tls", + "sqlx-sqlite", + "macros", +] } diff --git a/entities/src/lib.rs b/entities/src/lib.rs index 9bbd106..60e1fe4 100644 --- a/entities/src/lib.rs +++ b/entities/src/lib.rs @@ -2,3 +2,27 @@ pub mod market; pub mod ticker; pub use sea_orm; + +pub mod db { + use { + eyre::Result, + sea_orm::{ConnectionTrait, Database, DatabaseConnection, DbBackend, Statement}, + std::sync::Arc, + }; + + pub async fn get_connection() -> Result> { + let db_conn_str = std::env::var("DATABASE_URL")?; + let conn = Arc::new(Database::connect(&db_conn_str).await?); + + conn.query_one(Statement::from_string( + DbBackend::Sqlite, + r#" + PRAGMA journal_mode = WAL; + PRAGMA synchronous = NORMAL; + "#, + )) + .await?; + + Ok(conn) + } +} diff --git a/gateway/Cargo.toml b/gateway/Cargo.toml index 3c47534..04e8d20 100644 --- a/gateway/Cargo.toml +++ b/gateway/Cargo.toml @@ -10,5 +10,5 @@ path = "src/lib.rs" [dependencies] entities = { path = "../entities" } -eyre = { version = "0.6.12", default-features = false } +eyre = { workspace = true, default-features = false } diff --git a/grpc/Cargo.toml b/grpc/Cargo.toml index 3fa608e..1dfca8b 100644 --- a/grpc/Cargo.toml +++ b/grpc/Cargo.toml @@ -9,8 +9,14 @@ name = "grpc" path = "src/lib.rs" [dependencies] +# common +log = { workspace = true } +eyre = { workspace = true, default-features = false } + +# internal entities = { path = "../entities" } -eyre = { version = "0.6.12", default-features = false } + +# external prost = "0.12.3" tonic = "0.11.0"