From 292ff60720c1f370d300ce1dfb9d344c0c3d296b Mon Sep 17 00:00:00 2001 From: Miguel da Mota Date: Wed, 3 Jan 2024 00:10:33 +0100 Subject: [PATCH] [API-1] chore: little updates --- Cargo.lock | 132 +++++++++++++++++++++++++++++++--------- Cargo.toml | 2 + src/helpers/db.rs | 1 + src/helpers/jwt.rs | 2 +- src/routes/playlists.rs | 8 ++- src/routes/search.rs | 11 +++- src/services/spotify.rs | 2 - 7 files changed, 121 insertions(+), 37 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index d994919..79e1fbf 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -65,7 +65,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e01ed3140b2f8d422c68afa1ed2e85d996ea619c988ac834d255db32138655cb" dependencies = [ "quote", - "syn 2.0.44", + "syn 2.0.46", ] [[package]] @@ -179,7 +179,7 @@ dependencies = [ "actix-router", "proc-macro2", "quote", - "syn 2.0.44", + "syn 2.0.46", ] [[package]] @@ -251,13 +251,13 @@ dependencies = [ [[package]] name = "async-trait" -version = "0.1.76" +version = "0.1.77" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "531b97fb4cd3dfdce92c35dedbfdc1f0b9d8091c8ca943d6dae340ef5012d514" +checksum = "c980ee35e870bd1a4d2c8294d4c04d0499e67bca1e4b5cefcc693c2fa00caea9" dependencies = [ "proc-macro2", "quote", - "syn 2.0.44", + "syn 2.0.46", ] [[package]] @@ -527,7 +527,7 @@ dependencies = [ "diesel_table_macro_syntax", "proc-macro2", "quote", - "syn 2.0.44", + "syn 2.0.46", ] [[package]] @@ -547,7 +547,7 @@ version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fc5557efc453706fed5e4fa85006fe9817c224c3f480a34c7e5959fd700921c5" dependencies = [ - "syn 2.0.44", + "syn 2.0.46", ] [[package]] @@ -576,6 +576,19 @@ dependencies = [ "cfg-if", ] +[[package]] +name = "env_logger" +version = "0.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "95b3f3e67048839cb0d0781f445682a35113da7121f7c949db0e2be96a4fbece" +dependencies = [ + "humantime", + "is-terminal", + "log", + "regex", + "termcolor", +] + [[package]] name = "equivalent" version = "1.0.1" @@ -789,6 +802,12 @@ version = "1.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "df3b46402a9d5adb4c86a0cf463f42e19994e3ee891101b1841f30a545cb49a9" +[[package]] +name = "humantime" +version = "2.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4" + [[package]] name = "hyper" version = "0.14.28" @@ -884,6 +903,17 @@ version = "2.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8f518f335dce6725a761382244631d86cf0ccb2863413590b31338feb467f9c3" +[[package]] +name = "is-terminal" +version = "0.4.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0bad00257d07be169d870ab665980b06cdb366d792ad690bf2e76876dc503455" +dependencies = [ + "hermit-abi", + "rustix", + "windows-sys 0.52.0", +] + [[package]] name = "itoa" version = "1.0.10" @@ -1130,7 +1160,7 @@ checksum = "a948666b637a0f465e8564c73e89d4dde00d72d4d473cc972f390fc3dcee7d9c" dependencies = [ "proc-macro2", "quote", - "syn 2.0.44", + "syn 2.0.46", ] [[package]] @@ -1237,18 +1267,18 @@ dependencies = [ [[package]] name = "proc-macro2" -version = "1.0.73" +version = "1.0.74" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2dd5e8a1f1029c43224ad5898e50140c2aebb1705f19e67c918ebf5b9e797fe1" +checksum = "2de98502f212cfcea8d0bb305bd0f49d7ebdd75b64ba0a68f937d888f4e0d6db" dependencies = [ "unicode-ident", ] [[package]] name = "quote" -version = "1.0.34" +version = "1.0.35" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "22a37c9326af5ed140c86a46655b5278de879853be5573c01df185b6f49a580a" +checksum = "291ec9ab5efd934aaf503a6466c5d5251535d108ee747472c3977cc5acc868ef" dependencies = [ "proc-macro2", ] @@ -1473,29 +1503,29 @@ checksum = "836fa6a3e1e547f9a2c4040802ec865b5d85f4014efe00555d7090a3dcaa1090" [[package]] name = "serde" -version = "1.0.193" +version = "1.0.194" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "25dd9975e68d0cb5aa1120c288333fc98731bd1dd12f561e468ea4728c042b89" +checksum = "0b114498256798c94a0689e1a15fec6005dee8ac1f41de56404b67afc2a4b773" dependencies = [ "serde_derive", ] [[package]] name = "serde_derive" -version = "1.0.193" +version = "1.0.194" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "43576ca501357b9b071ac53cdc7da8ef0cbd9493d8df094cd821777ea6e894d3" +checksum = "a3385e45322e8f9931410f01b3031ec534c3947d0e94c18049af4d9f9907d4e0" dependencies = [ "proc-macro2", "quote", - "syn 2.0.44", + "syn 2.0.46", ] [[package]] name = "serde_json" -version = "1.0.109" +version = "1.0.110" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cb0652c533506ad7a2e353cce269330d6afd8bdfb6d75e0ace5b35aacbd7b9e9" +checksum = "6fbd975230bada99c8bb618e0c365c2eefa219158d5c6c29610fd09ff1833257" dependencies = [ "itoa", "ryu", @@ -1616,9 +1646,9 @@ dependencies = [ [[package]] name = "syn" -version = "2.0.44" +version = "2.0.46" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "92d27c2c202598d05175a6dd3af46824b7f747f8d8e9b14c623f19fa5069735d" +checksum = "89456b690ff72fddcecf231caedbe615c59480c93358a93dfae7fc29e3ebbf0e" dependencies = [ "proc-macro2", "quote", @@ -1660,23 +1690,32 @@ dependencies = [ ] [[package]] -name = "thiserror" -version = "1.0.53" +name = "termcolor" +version = "1.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b2cd5904763bad08ad5513ddbb12cf2ae273ca53fa9f68e843e236ec6dfccc09" +checksum = "ff1bc3d3f05aff0403e8ac0d92ced918ec05b666a43f83297ccef5bea8a3d449" +dependencies = [ + "winapi-util", +] + +[[package]] +name = "thiserror" +version = "1.0.56" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d54378c645627613241d077a3a79db965db602882668f9136ac42af9ecb730ad" dependencies = [ "thiserror-impl", ] [[package]] name = "thiserror-impl" -version = "1.0.53" +version = "1.0.56" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3dcf4a824cce0aeacd6f38ae6f24234c8e80d68632338ebaa1443b5df9e29e19" +checksum = "fa0faa943b50f3db30a20aa7e265dbc66076993efed8463e8de414e5d06d3471" dependencies = [ "proc-macro2", "quote", - "syn 2.0.44", + "syn 2.0.46", ] [[package]] @@ -1899,9 +1938,11 @@ dependencies = [ "diesel", "diesel_migrations", "dotenvy", + "env_logger", "hmac", "jsonwebtoken", "lazy_static", + "log", "reqwest", "serde", "serde_json", @@ -1944,7 +1985,7 @@ dependencies = [ "once_cell", "proc-macro2", "quote", - "syn 2.0.44", + "syn 2.0.46", "wasm-bindgen-shared", ] @@ -1978,7 +2019,7 @@ checksum = "f0eb82fcb7930ae6219a7ecfd55b217f5f0893484b7a13022ebb2b2bf20b5283" dependencies = [ "proc-macro2", "quote", - "syn 2.0.44", + "syn 2.0.46", "wasm-bindgen-backend", "wasm-bindgen-shared", ] @@ -1999,6 +2040,37 @@ dependencies = [ "wasm-bindgen", ] +[[package]] +name = "winapi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" +dependencies = [ + "winapi-i686-pc-windows-gnu", + "winapi-x86_64-pc-windows-gnu", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" + +[[package]] +name = "winapi-util" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f29e6f9198ba0d26b4c9f07dbe6f9ed633e1f3d5b8b414090084349e46a52596" +dependencies = [ + "winapi", +] + +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" + [[package]] name = "windows-core" version = "0.52.0" @@ -2176,7 +2248,7 @@ checksum = "9ce1b18ccd8e73a9321186f97e46f9f04b778851177567b1975109d26a08d2a6" dependencies = [ "proc-macro2", "quote", - "syn 2.0.44", + "syn 2.0.46", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index 6c1cd8e..bb3a36a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,7 +14,9 @@ chrono = { version = "0.4", features = ["serde"] } diesel = { version = "2", features = ["r2d2", "postgres", "chrono"] } diesel_migrations = "2" dotenvy = "*" +env_logger = "0.10" hmac = "0.12" +log = "0.4" reqwest = { version = "0.11", features = ["blocking", "json"] } sha2 = "0.10" lazy_static = "1" diff --git a/src/helpers/db.rs b/src/helpers/db.rs index c6f224e..9d03a1a 100644 --- a/src/helpers/db.rs +++ b/src/helpers/db.rs @@ -16,6 +16,7 @@ lazy_static! { } pub fn init() { + log::info!("Initializing db pool."); lazy_static::initialize(&POOL); let _conn = connection().expect("Failed to get db connection."); } diff --git a/src/helpers/jwt.rs b/src/helpers/jwt.rs index 96ac6d6..b50e2cf 100644 --- a/src/helpers/jwt.rs +++ b/src/helpers/jwt.rs @@ -5,7 +5,7 @@ use jsonwebtoken::{decode, DecodingKey, EncodingKey, TokenData, Validation}; const JWT_SECRET: &str = "secret"; pub fn get_token(token: &str) -> Result, Error> { - let token = decode::(&token, &get_decoding_key(), &Validation::default())?; + let token = decode::(token, &get_decoding_key(), &Validation::default())?; Ok(token) } diff --git a/src/routes/playlists.rs b/src/routes/playlists.rs index 5a12319..d49aed9 100644 --- a/src/routes/playlists.rs +++ b/src/routes/playlists.rs @@ -1,9 +1,13 @@ use crate::middlewares::error::ErrorResponse; use crate::models::playlists::{PlaylistCreator, Playlists}; use crate::models::tracks::TracksWithArtists; -use actix_web::{get, web, HttpResponse}; +use actix_web::{get, web, HttpResponse, Scope}; use serde::{Deserialize, Serialize}; +pub fn routes() -> Scope { + web::scope("/playlists").service(get_playlist) +} + #[derive(Deserialize, Serialize)] struct GetPlaylistResponse { pub id: String, @@ -15,7 +19,7 @@ struct GetPlaylistResponse { } #[get("/{playlist_id}")] -pub async fn get_playlist(path: web::Path) -> Result { +async fn get_playlist(path: web::Path) -> Result { let playlist_id = path.into_inner(); let playlist = Playlists::find(playlist_id.as_str())?; diff --git a/src/routes/search.rs b/src/routes/search.rs index f4f3b7c..718a3a4 100644 --- a/src/routes/search.rs +++ b/src/routes/search.rs @@ -1,6 +1,6 @@ +use crate::middlewares::error::ErrorResponse; use crate::models::spotify; use crate::services::spotify as Spotify; -use crate::{middlewares::error::ErrorResponse, models::tracks::Tracks}; use actix_web::{get, web, HttpResponse, Result, Scope}; use serde::{Deserialize, Serialize}; @@ -21,11 +21,18 @@ struct SearchResponse { #[derive(Serialize)] struct SearchTracks { pub id: String, + pub title: String, + // pub artists: String, + pub duration_ms: i32, } impl From for SearchTracks { fn from(value: spotify::Track) -> SearchTracks { - SearchTracks { id: value.id } + SearchTracks { + id: value.id, + title: value.name, + duration_ms: value.duration_ms, + } } } diff --git a/src/services/spotify.rs b/src/services/spotify.rs index 251737b..2443d4f 100644 --- a/src/services/spotify.rs +++ b/src/services/spotify.rs @@ -106,8 +106,6 @@ impl Service for Spotify { .send() .await; - println!("Hello"); - match response { Ok(res) => { let data = res.json::().await;