From 2a7c13d612e71010e0249c135754ca357b2779a9 Mon Sep 17 00:00:00 2001 From: Samuel Lorch Date: Sun, 22 Oct 2023 22:16:35 +0200 Subject: [PATCH] Move router to seperate file, add traceing --- Cargo.lock | 81 +++++++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 2 ++ src/main.rs | 38 +++++++++--------------- src/router.rs | 23 +++++++++++++++ 4 files changed, 120 insertions(+), 24 deletions(-) create mode 100644 src/router.rs diff --git a/Cargo.lock b/Cargo.lock index d8b627e..d5a52a4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -397,9 +397,21 @@ dependencies = [ "serde_json", "tokio", "tower-http", + "tracing", + "tracing-subscriber", "validator", ] +[[package]] +name = "nu-ansi-term" +version = "0.46.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77a8165726e8236064dbb45459242600304b42a5ea24ee2948e18e023bf7ba84" +dependencies = [ + "overload", + "winapi", +] + [[package]] name = "num_cpus" version = "1.16.0" @@ -425,6 +437,12 @@ version = "1.18.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dd8b5dd2ae5ed71462c540258bedcb51965123ad7e7ccf4b9a8cafaa4a63576d" +[[package]] +name = "overload" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b15813163c1d831bf4a13c3610c05c0d03b39feb07f7e09fa234dac9b15aaf39" + [[package]] name = "parking_lot" version = "0.12.1" @@ -643,6 +661,15 @@ dependencies = [ "serde", ] +[[package]] +name = "sharded-slab" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f40ca3c46823713e0d4209592e8d6e826aa57e928f09752619fc696c499637f6" +dependencies = [ + "lazy_static", +] + [[package]] name = "signal-hook-registry" version = "1.4.1" @@ -706,6 +733,16 @@ version = "0.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2047c6ded9c721764247e62cd3b03c09ffc529b2ba5b10ec482ae507a4a70160" +[[package]] +name = "thread_local" +version = "1.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3fdd6f064ccff2d6567adcb3873ca630700f00b5ad3f060c25b5dcfd9a4ce152" +dependencies = [ + "cfg-if", + "once_cell", +] + [[package]] name = "tinyvec" version = "1.6.0" @@ -805,9 +842,21 @@ checksum = "c3523ab5a71916ccf420eebdf5521fcef02141234bbc0b8a49f2fdc4544364ef" dependencies = [ "log", "pin-project-lite", + "tracing-attributes", "tracing-core", ] +[[package]] +name = "tracing-attributes" +version = "0.1.27" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34704c8d6ebcbc939824180af020566b01a7c01f80641264eba0999f6c2b6be7" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.38", +] + [[package]] name = "tracing-core" version = "0.1.32" @@ -815,6 +864,32 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c06d3da6113f116aaee68e4d601191614c9053067f9ab7f6edbcb161237daa54" dependencies = [ "once_cell", + "valuable", +] + +[[package]] +name = "tracing-log" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "78ddad33d2d10b1ed7eb9d1f518a5674713876e97e5bb9b7345a7984fbb4f922" +dependencies = [ + "lazy_static", + "log", + "tracing-core", +] + +[[package]] +name = "tracing-subscriber" +version = "0.3.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "30a651bc37f915e81f087d86e62a18eec5f79550c7faff886f7090b4ea757c77" +dependencies = [ + "nu-ansi-term", + "sharded-slab", + "smallvec", + "thread_local", + "tracing-core", + "tracing-log", ] [[package]] @@ -897,6 +972,12 @@ dependencies = [ "syn 1.0.109", ] +[[package]] +name = "valuable" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "830b7e5d4d90034032940e4ace0d9a9a057e7a45cd94e6c007832e39edb82f6d" + [[package]] name = "version_check" version = "0.9.4" diff --git a/Cargo.toml b/Cargo.toml index 7c8607b..d4d2d94 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,4 +13,6 @@ serde = { version = "1.0.189", features = ["derive"] } serde_json = "1.0.107" tokio = { version = "1.33.0", features = ["full"] } tower-http = "0.4.4" +tracing = "0.1.40" +tracing-subscriber = "0.3.17" validator = { version = "0.15", features = ["derive"] } diff --git a/src/main.rs b/src/main.rs index f697123..0bc8863 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,39 +1,28 @@ #![allow(dead_code)] +use tracing::info; +use tracing_subscriber; + mod config_manager; mod definitions; - -use axum::{ - response::IntoResponse, - routing::{get, post}, - Json, Router, -}; - -pub async fn health_checker_handler() -> impl IntoResponse { - const MESSAGE: &str = "Hello there"; - - let json_response = serde_json::json!({ - "status": "success", - "message": MESSAGE - }); - - Json(json_response) -} +mod router; #[tokio::main] async fn main() { - println!("Starting..."); + tracing_subscriber::fmt::init(); + info!("Starting..."); - let mut config_manager = config_manager::new_config_manager().unwrap(); + // let mut config_manager = config_manager::new_config_manager().unwrap(); - let app = Router::new().route("/health", get(health_checker_handler)); + let app = router::get_router(); - println!("Server started successfully"); - axum::Server::bind(&"0.0.0.0:8000".parse().unwrap()) - .serve(app.into_make_service()) + info!("Server started successfully"); + axum::Server::bind(&"[::]:8080".parse().unwrap()) + .serve(app.await.into_make_service()) .await .unwrap(); + /* let mut tx = config_manager.start_transaction().unwrap(); tx.changes @@ -97,5 +86,6 @@ async fn main() { config_manager.apply_pending_changes().unwrap(); let applied_config = config_manager.get_current_config(); - println!("applied_config = {:#?}", applied_config); + info!("applied_config = {:#?}", applied_config); + */ } diff --git a/src/router.rs b/src/router.rs new file mode 100644 index 0000000..f13fcf2 --- /dev/null +++ b/src/router.rs @@ -0,0 +1,23 @@ +use axum::{ + response::IntoResponse, + routing::{get, post}, + Json, Router, +}; + +use tracing::info; + +pub async fn get_router() -> Router { + Router::new().route("/health", get(health_checker_handler)) +} + +pub async fn health_checker_handler() -> impl IntoResponse { + info!("health hit"); + const MESSAGE: &str = "Hello there"; + + let json_response = serde_json::json!({ + "status": "success", + "message": MESSAGE + }); + + Json(json_response) +}