From 506ce117d313251b494daf333f5272353bb78f13 Mon Sep 17 00:00:00 2001 From: Joakim Hulthe Date: Fri, 16 Feb 2024 13:43:24 +0100 Subject: [PATCH] refactor? --- Cargo.lock | 22 +++++++++---------- Cargo.toml | 2 +- snitch/Cargo.toml | 9 +------- {snitchlib => snitch}/examples/log.rs | 2 +- {snitchlib => snitch}/src/lib.rs | 14 ++++++++++-- {snitchlib => snitch}/src/message.rs | 0 snitch_srv/Cargo.toml | 17 ++++++++++++++ {snitch => snitch_srv}/build.rs | 0 .../migrations/2022-11-11T22:56.down.sql | 0 .../migrations/2022-11-11T22:56.up.sql | 0 {snitch => snitch_srv}/src/api.rs | 2 +- {snitch => snitch_srv}/src/dashboard.rs | 0 {snitch => snitch_srv}/src/database.rs | 0 {snitch => snitch_srv}/src/main.rs | 0 {snitch => snitch_srv}/templates/index.hbs | 0 snitchlib/Cargo.toml | 10 --------- 16 files changed, 44 insertions(+), 34 deletions(-) rename {snitchlib => snitch}/examples/log.rs (96%) rename {snitchlib => snitch}/src/lib.rs (79%) rename {snitchlib => snitch}/src/message.rs (100%) create mode 100644 snitch_srv/Cargo.toml rename {snitch => snitch_srv}/build.rs (100%) rename {snitch => snitch_srv}/migrations/2022-11-11T22:56.down.sql (100%) rename {snitch => snitch_srv}/migrations/2022-11-11T22:56.up.sql (100%) rename {snitch => snitch_srv}/src/api.rs (97%) rename {snitch => snitch_srv}/src/dashboard.rs (100%) rename {snitch => snitch_srv}/src/database.rs (100%) rename {snitch => snitch_srv}/src/main.rs (100%) rename {snitch => snitch_srv}/templates/index.hbs (100%) delete mode 100644 snitchlib/Cargo.toml diff --git a/Cargo.lock b/Cargo.lock index 26c3926..1dff386 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2041,6 +2041,16 @@ checksum = "a507befe795404456341dfab10cef66ead4c041f62b8b11bbb92bffe5d0953e0" [[package]] name = "snitch" version = "1.0.0" +dependencies = [ + "chrono", + "log", + "reqwest", + "serde", +] + +[[package]] +name = "snitch_srv" +version = "1.0.0" dependencies = [ "anyhow", "chrono", @@ -2050,20 +2060,10 @@ dependencies = [ "rocket_db_pools", "rocket_dyn_templates", "serde", - "snitchlib", + "snitch", "sqlx", ] -[[package]] -name = "snitchlib" -version = "1.0.0" -dependencies = [ - "chrono", - "log", - "reqwest", - "serde", -] - [[package]] name = "socket2" version = "0.4.7" diff --git a/Cargo.toml b/Cargo.toml index 738dced..de55865 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,2 +1,2 @@ [workspace] -members = ["snitch", "snitchlib"] +members = ["snitch", "snitch_srv"] diff --git a/snitch/Cargo.toml b/snitch/Cargo.toml index 2d27376..3aab622 100644 --- a/snitch/Cargo.toml +++ b/snitch/Cargo.toml @@ -4,14 +4,7 @@ version = "1.0.0" edition = "2021" [dependencies] -snitchlib = { path = "../snitchlib" } - -anyhow = "*" log = "0.4" serde = { version = "1", features = ["derive"] } chrono = { version = "0.4", features = ["serde"] } -sqlx = { version = "0.5", features = ["runtime-tokio-rustls", "postgres", "sqlite", "migrate", "macros", "chrono"] } -rocket_dyn_templates = { version = "0.1.0-rc.2", features = ["handlebars"] } -rocket_db_pools = { version = "0.1.0-rc.2", features = ["sqlx_postgres"] } -rocket = { version = "0.5.0-rc.2", features = ["json"] } -clap = { version = "4.0.23", features = ["derive", "env"] } +reqwest = { version = "0.11.12", default-features = false, features = ["rustls-tls", "serde_json", "blocking", "json"] } diff --git a/snitchlib/examples/log.rs b/snitch/examples/log.rs similarity index 96% rename from snitchlib/examples/log.rs rename to snitch/examples/log.rs index 2c75a65..7137240 100644 --- a/snitchlib/examples/log.rs +++ b/snitch/examples/log.rs @@ -1,5 +1,5 @@ use log::{Level, LevelFilter, Log, Metadata, Record}; -use snitchlib::SnitchLogger; +use snitch::SnitchLogger; struct SimpleLogger; diff --git a/snitchlib/src/lib.rs b/snitch/src/lib.rs similarity index 79% rename from snitchlib/src/lib.rs rename to snitch/src/lib.rs index 17a09b9..0d65ccc 100644 --- a/snitchlib/src/lib.rs +++ b/snitch/src/lib.rs @@ -1,3 +1,5 @@ +use std::time::Duration; + use log::{Level, Log, Metadata, Record}; use reqwest::blocking; @@ -22,10 +24,13 @@ impl SnitchLogger { impl Log for SnitchLogger { fn enabled(&self, metadata: &Metadata) -> bool { - self.wrapped.enabled(metadata) || metadata.level() <= Level::Warn + dbg!(self.wrapped.enabled(metadata) || metadata.level() <= Level::Warn) } fn log(&self, record: &Record) { + eprintln!("log called"); + eprintln!("log called"); + eprintln!("log called"); eprintln!("log called"); self.wrapped.log(record); @@ -43,7 +48,12 @@ impl Log for SnitchLogger { }; let client = blocking::Client::new(); - if let Err(e) = client.post(&self.url).json(&record).send() { + if let Err(e) = client + .post(&self.url) + .json(&record) + .timeout(Duration::from_secs(2)) + .send() + { // TODO: log error (without sending it) eprintln!("failed to send log record: {e:?}"); } diff --git a/snitchlib/src/message.rs b/snitch/src/message.rs similarity index 100% rename from snitchlib/src/message.rs rename to snitch/src/message.rs diff --git a/snitch_srv/Cargo.toml b/snitch_srv/Cargo.toml new file mode 100644 index 0000000..da49775 --- /dev/null +++ b/snitch_srv/Cargo.toml @@ -0,0 +1,17 @@ +[package] +name = "snitch_srv" +version = "1.0.0" +edition = "2021" + +[dependencies] +snitch = { path = "../snitch" } + +anyhow = "*" +log = "0.4" +serde = { version = "1", features = ["derive"] } +chrono = { version = "0.4", features = ["serde"] } +sqlx = { version = "0.5", features = ["runtime-tokio-rustls", "postgres", "sqlite", "migrate", "macros", "chrono"] } +rocket_dyn_templates = { version = "0.1.0-rc.2", features = ["handlebars"] } +rocket_db_pools = { version = "0.1.0-rc.2", features = ["sqlx_postgres"] } +rocket = { version = "0.5.0-rc.2", features = ["json"] } +clap = { version = "4.0.23", features = ["derive", "env"] } diff --git a/snitch/build.rs b/snitch_srv/build.rs similarity index 100% rename from snitch/build.rs rename to snitch_srv/build.rs diff --git a/snitch/migrations/2022-11-11T22:56.down.sql b/snitch_srv/migrations/2022-11-11T22:56.down.sql similarity index 100% rename from snitch/migrations/2022-11-11T22:56.down.sql rename to snitch_srv/migrations/2022-11-11T22:56.down.sql diff --git a/snitch/migrations/2022-11-11T22:56.up.sql b/snitch_srv/migrations/2022-11-11T22:56.up.sql similarity index 100% rename from snitch/migrations/2022-11-11T22:56.up.sql rename to snitch_srv/migrations/2022-11-11T22:56.up.sql diff --git a/snitch/src/api.rs b/snitch_srv/src/api.rs similarity index 97% rename from snitch/src/api.rs rename to snitch_srv/src/api.rs index 45df247..a84e189 100644 --- a/snitch/src/api.rs +++ b/snitch_srv/src/api.rs @@ -1,5 +1,5 @@ use rocket::{http::Status, serde::json::Json, State}; -use snitchlib::LogMsg; +use snitch::LogMsg; use sqlx::query; use crate::database::Database; diff --git a/snitch/src/dashboard.rs b/snitch_srv/src/dashboard.rs similarity index 100% rename from snitch/src/dashboard.rs rename to snitch_srv/src/dashboard.rs diff --git a/snitch/src/database.rs b/snitch_srv/src/database.rs similarity index 100% rename from snitch/src/database.rs rename to snitch_srv/src/database.rs diff --git a/snitch/src/main.rs b/snitch_srv/src/main.rs similarity index 100% rename from snitch/src/main.rs rename to snitch_srv/src/main.rs diff --git a/snitch/templates/index.hbs b/snitch_srv/templates/index.hbs similarity index 100% rename from snitch/templates/index.hbs rename to snitch_srv/templates/index.hbs diff --git a/snitchlib/Cargo.toml b/snitchlib/Cargo.toml deleted file mode 100644 index b44277e..0000000 --- a/snitchlib/Cargo.toml +++ /dev/null @@ -1,10 +0,0 @@ -[package] -name = "snitchlib" -version = "1.0.0" -edition = "2021" - -[dependencies] -log = "0.4" -serde = { version = "1", features = ["derive"] } -chrono = { version = "0.4", features = ["serde"] } -reqwest = { version = "0.11.12", default-features = false, features = ["rustls-tls", "serde_json", "blocking", "json"] }