From 05dc4870992e8f8a6edbb4a438c73913ab15bb8c Mon Sep 17 00:00:00 2001 From: Vitor Hideyoshi Nakazone Batista Date: Sun, 25 Feb 2024 00:21:49 -0300 Subject: [PATCH] Implements Configuration via Env Variables --- .env | 4 + Cargo.lock | 186 +++++++++++++++++++++++++++--- Cargo.toml | 2 + src/config/config_auth.rs | 17 +++ src/config/config_server.rs | 22 ++++ src/config/mod.rs | 2 + src/handler/message.rs | 2 - src/main.rs | 11 +- src/middleware/auth_middleware.rs | 7 +- 9 files changed, 235 insertions(+), 18 deletions(-) create mode 100644 .env create mode 100644 src/config/config_auth.rs create mode 100644 src/config/config_server.rs create mode 100644 src/config/mod.rs diff --git a/.env b/.env new file mode 100644 index 0000000..fa1ef4c --- /dev/null +++ b/.env @@ -0,0 +1,4 @@ +HOST=localhost +PORT=8500 + +AUTH_URL=http://localhost:8070 \ No newline at end of file diff --git a/Cargo.lock b/Cargo.lock index c37725b..108f6b0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -17,6 +17,24 @@ version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe" +[[package]] +name = "ahash" +version = "0.8.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d713b3834d76b85304d4d525563c1276e2e30dc97cc67bfb4585a4a29fc2c89f" +dependencies = [ + "cfg-if", + "once_cell", + "version_check", + "zerocopy", +] + +[[package]] +name = "allocator-api2" +version = "0.2.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0942ffc6dcaadf03badf6e6a2d0228460359d5e34b57ccdc720b7382dfbd5ec5" + [[package]] name = "android-tzdata" version = "0.1.1" @@ -40,7 +58,7 @@ checksum = "c980ee35e870bd1a4d2c8294d4c04d0499e67bca1e4b5cefcc693c2fa00caea9" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 2.0.50", ] [[package]] @@ -158,6 +176,39 @@ version = "1.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a2bd12c1caf447e69cd4528f47f94d203fd2582878ecb9e9465484c4148a8223" +[[package]] +name = "cached" +version = "0.49.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f251fd1e72720ca07bf5d8e310f54a193fd053479a1f6342c6663ee4fa01cf96" +dependencies = [ + "ahash", + "cached_proc_macro", + "cached_proc_macro_types", + "hashbrown 0.14.3", + "instant", + "once_cell", + "thiserror", +] + +[[package]] +name = "cached_proc_macro" +version = "0.20.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ad9f16c0d84de31a2ab7fdf5f7783c14631f7075cf464eb3bb43119f61c9cb2a" +dependencies = [ + "darling 0.14.4", + "proc-macro2", + "quote", + "syn 1.0.109", +] + +[[package]] +name = "cached_proc_macro_types" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ade8366b8bd5ba243f0a58f036cc0ca8a2f069cff1a2351ef1cac6b083e16fc0" + [[package]] name = "cc" version = "1.0.86" @@ -220,14 +271,38 @@ dependencies = [ "typenum", ] +[[package]] +name = "darling" +version = "0.14.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7b750cb3417fd1b327431a470f388520309479ab0bf5e323505daf0290cd3850" +dependencies = [ + "darling_core 0.14.4", + "darling_macro 0.14.4", +] + [[package]] name = "darling" version = "0.20.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "54e36fcd13ed84ffdfda6f5be89b31287cbb80c439841fe69e04841435464391" dependencies = [ - "darling_core", - "darling_macro", + "darling_core 0.20.8", + "darling_macro 0.20.8", +] + +[[package]] +name = "darling_core" +version = "0.14.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "109c1ca6e6b7f82cc233a97004ea8ed7ca123a9af07a8230878fcfda9b158bf0" +dependencies = [ + "fnv", + "ident_case", + "proc-macro2", + "quote", + "strsim", + "syn 1.0.109", ] [[package]] @@ -241,7 +316,18 @@ dependencies = [ "proc-macro2", "quote", "strsim", - "syn", + "syn 2.0.50", +] + +[[package]] +name = "darling_macro" +version = "0.14.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a4aab4dbc9f7611d8b55048a3a16d2d010c2c8334e46304b40ac1cc14bf3b48e" +dependencies = [ + "darling_core 0.14.4", + "quote", + "syn 1.0.109", ] [[package]] @@ -250,9 +336,9 @@ version = "0.20.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a668eda54683121533a393014d8692171709ff57a7d61f187b6e782719f8933f" dependencies = [ - "darling_core", + "darling_core 0.20.8", "quote", - "syn", + "syn 2.0.50", ] [[package]] @@ -275,6 +361,12 @@ dependencies = [ "crypto-common", ] +[[package]] +name = "dotenv" +version = "0.15.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77c90badedccf4105eca100756a0b1289e191f6fcbdadd3cee1d2f614f97da8f" + [[package]] name = "encoding_rs" version = "0.8.33" @@ -440,6 +532,10 @@ name = "hashbrown" version = "0.14.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "290f1a1d9242c78d09ce40a5e87e7554ee637af1351968159f4952f028f75604" +dependencies = [ + "ahash", + "allocator-api2", +] [[package]] name = "headers" @@ -679,6 +775,15 @@ dependencies = [ "serde", ] +[[package]] +name = "instant" +version = "0.1.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7a5bbe824c507c5da5956355e86a746d82e0e1464f65d862cc5e71da70e94b2c" +dependencies = [ + "cfg-if", +] + [[package]] name = "ipnet" version = "2.9.0" @@ -741,7 +846,9 @@ name = "message-hideyoshi-com" version = "0.1.0" dependencies = [ "axum", + "cached", "chrono", + "dotenv", "headers", "http 1.0.0", "reqwest", @@ -859,7 +966,7 @@ checksum = "a948666b637a0f465e8564c73e89d4dde00d72d4d473cc972f390fc3dcee7d9c" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 2.0.50", ] [[package]] @@ -903,7 +1010,7 @@ checksum = "266c042b60c9c76b8d53061e52b2e0d1116abc57cefc8c5cd671619a56ac3690" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 2.0.50", ] [[package]] @@ -1077,7 +1184,7 @@ checksum = "7eb0b34b42edc17f6b7cac84a52a1c5f0e1bb2227e997ca9011ea3dd34e8610b" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 2.0.50", ] [[package]] @@ -1137,10 +1244,10 @@ version = "3.6.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "865f9743393e638991566a8b7a479043c2c8da94a33e0a31f18214c9cae0a64d" dependencies = [ - "darling", + "darling 0.20.8", "proc-macro2", "quote", - "syn", + "syn 2.0.50", ] [[package]] @@ -1185,6 +1292,17 @@ version = "0.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623" +[[package]] +name = "syn" +version = "1.0.109" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + [[package]] name = "syn" version = "2.0.50" @@ -1235,6 +1353,26 @@ dependencies = [ "windows-sys 0.52.0", ] +[[package]] +name = "thiserror" +version = "1.0.57" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e45bcbe8ed29775f228095caf2cd67af7a4ccf756ebff23a306bf3e8b47b24b" +dependencies = [ + "thiserror-impl", +] + +[[package]] +name = "thiserror-impl" +version = "1.0.57" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a953cb265bef375dae3de6663da4d3804eee9682ea80d8e2542529b73c531c81" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.50", +] + [[package]] name = "time" version = "0.3.34" @@ -1306,7 +1444,7 @@ checksum = "5b8a1e28f2deaa14e508979454cb3a223b10b938b45af148bc0986de36f1923b" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 2.0.50", ] [[package]] @@ -1479,7 +1617,7 @@ dependencies = [ "once_cell", "proc-macro2", "quote", - "syn", + "syn 2.0.50", "wasm-bindgen-shared", ] @@ -1513,7 +1651,7 @@ checksum = "642f325be6301eb8107a83d12a8ac6c1e1c54345a7ef1a9261962dfefda09e66" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 2.0.50", "wasm-bindgen-backend", "wasm-bindgen-shared", ] @@ -1684,3 +1822,23 @@ dependencies = [ "cfg-if", "windows-sys 0.48.0", ] + +[[package]] +name = "zerocopy" +version = "0.7.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "74d4d3961e53fa4c9a25a8637fc2bfaf2595b3d3ae34875568a5cf64787716be" +dependencies = [ + "zerocopy-derive", +] + +[[package]] +name = "zerocopy-derive" +version = "0.7.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9ce1b18ccd8e73a9321186f97e46f9f04b778851177567b1975109d26a08d2a6" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.50", +] diff --git a/Cargo.toml b/Cargo.toml index a814951..ecc3a2b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,3 +16,5 @@ headers = "0.4.0" http = "1.0.0" reqwest = "0.11.24" serde_json = "1.0.114" +cached = "0.49.2" +dotenv = "0.15.0" diff --git a/src/config/config_auth.rs b/src/config/config_auth.rs new file mode 100644 index 0000000..571858d --- /dev/null +++ b/src/config/config_auth.rs @@ -0,0 +1,17 @@ +use std::env; +use cached::proc_macro::cached; + +#[derive(Clone)] +pub struct ConfigAuth { + pub auth_url: String, +} + +#[cached] +pub fn get_config_auth() -> ConfigAuth { + let url = env::var("AUTH_URL") + .expect("AUTH_URL must be set"); + + ConfigAuth { + auth_url: url, + } +} \ No newline at end of file diff --git a/src/config/config_server.rs b/src/config/config_server.rs new file mode 100644 index 0000000..2daf1a2 --- /dev/null +++ b/src/config/config_server.rs @@ -0,0 +1,22 @@ +use std::env; +use cached::proc_macro::cached; + +#[derive(Clone)] +pub struct ConfigServer { + pub(crate) host: String, + pub(crate) port: u16, +} + +#[cached] +pub fn get_config_server() -> ConfigServer { + let h = option_env!("HOST") + .unwrap_or("localhost").to_string(); + + let p = option_env!("PORT").unwrap_or("8500") + .parse::().unwrap(); + + ConfigServer { + host: h, + port: p, + } +} \ No newline at end of file diff --git a/src/config/mod.rs b/src/config/mod.rs new file mode 100644 index 0000000..632fda3 --- /dev/null +++ b/src/config/mod.rs @@ -0,0 +1,2 @@ +pub mod config_server; +pub mod config_auth; \ No newline at end of file diff --git a/src/handler/message.rs b/src/handler/message.rs index acf68fa..93eaeef 100644 --- a/src/handler/message.rs +++ b/src/handler/message.rs @@ -5,9 +5,7 @@ use axum::extract::State; pub async fn send_message(Extension(auther): Extension, Json(payload): Json) -> impl IntoResponse { let mut response = payload.clone(); - response.author = Some(auther).clone(); - println!("Received message: {:?}", response); (StatusCode::OK, Json(response)) } diff --git a/src/main.rs b/src/main.rs index bcdf665..93d36d7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,12 +2,21 @@ mod handler; mod middleware; mod model; mod route; +mod config; + +use crate::config::config_server; + #[tokio::main] async fn main() { + dotenv::dotenv().ok(); + let server_config = config_server::get_config_server(); + let app = route::create_route(); println!("🚀 Server started successfully"); - let listener = tokio::net::TcpListener::bind("0.0.0.0:8500").await.unwrap(); + let listener = tokio::net::TcpListener::bind( + format!("{}:{}", server_config.host, server_config.port) + ).await.unwrap(); axum::serve(listener, app).await.unwrap(); } diff --git a/src/middleware/auth_middleware.rs b/src/middleware/auth_middleware.rs index 27505ee..2e8bf46 100644 --- a/src/middleware/auth_middleware.rs +++ b/src/middleware/auth_middleware.rs @@ -6,6 +6,7 @@ use axum::{ }; use reqwest::header::AUTHORIZATION; use crate::model::send_message::MessageAuthor; +use crate::config::config_auth; pub async fn auth_middleware(mut request: Request, next: Next) -> Result { let token = get_token(&request).ok_or(StatusCode::UNAUTHORIZED)?; @@ -30,8 +31,12 @@ fn get_token(req: &Request) -> Option { async fn validate_token(token: &str) -> Option { println!("Received token: {}", token); + let auth_config = config_auth::get_config_auth(); + + let validation_url = format!("{}/user/login/validate", auth_config.auth_url); + let client = reqwest::Client::new(); - let response = client.post("http://localhost:8070/user/login/validate") + let response = client.post(validation_url.as_str()) .header(AUTHORIZATION, format!("Bearer {}", token)) .send().await.unwrap();