[API-1] chore: little updates

This commit is contained in:
Miguel da Mota 2024-01-03 00:10:33 +01:00
parent 96a091f068
commit 292ff60720
7 changed files with 121 additions and 37 deletions

132
Cargo.lock generated
View file

@ -65,7 +65,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e01ed3140b2f8d422c68afa1ed2e85d996ea619c988ac834d255db32138655cb" checksum = "e01ed3140b2f8d422c68afa1ed2e85d996ea619c988ac834d255db32138655cb"
dependencies = [ dependencies = [
"quote", "quote",
"syn 2.0.44", "syn 2.0.46",
] ]
[[package]] [[package]]
@ -179,7 +179,7 @@ dependencies = [
"actix-router", "actix-router",
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn 2.0.44", "syn 2.0.46",
] ]
[[package]] [[package]]
@ -251,13 +251,13 @@ dependencies = [
[[package]] [[package]]
name = "async-trait" name = "async-trait"
version = "0.1.76" version = "0.1.77"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "531b97fb4cd3dfdce92c35dedbfdc1f0b9d8091c8ca943d6dae340ef5012d514" checksum = "c980ee35e870bd1a4d2c8294d4c04d0499e67bca1e4b5cefcc693c2fa00caea9"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn 2.0.44", "syn 2.0.46",
] ]
[[package]] [[package]]
@ -527,7 +527,7 @@ dependencies = [
"diesel_table_macro_syntax", "diesel_table_macro_syntax",
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn 2.0.44", "syn 2.0.46",
] ]
[[package]] [[package]]
@ -547,7 +547,7 @@ version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fc5557efc453706fed5e4fa85006fe9817c224c3f480a34c7e5959fd700921c5" checksum = "fc5557efc453706fed5e4fa85006fe9817c224c3f480a34c7e5959fd700921c5"
dependencies = [ dependencies = [
"syn 2.0.44", "syn 2.0.46",
] ]
[[package]] [[package]]
@ -576,6 +576,19 @@ dependencies = [
"cfg-if", "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]] [[package]]
name = "equivalent" name = "equivalent"
version = "1.0.1" version = "1.0.1"
@ -789,6 +802,12 @@ version = "1.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "df3b46402a9d5adb4c86a0cf463f42e19994e3ee891101b1841f30a545cb49a9" checksum = "df3b46402a9d5adb4c86a0cf463f42e19994e3ee891101b1841f30a545cb49a9"
[[package]]
name = "humantime"
version = "2.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4"
[[package]] [[package]]
name = "hyper" name = "hyper"
version = "0.14.28" version = "0.14.28"
@ -884,6 +903,17 @@ version = "2.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8f518f335dce6725a761382244631d86cf0ccb2863413590b31338feb467f9c3" 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]] [[package]]
name = "itoa" name = "itoa"
version = "1.0.10" version = "1.0.10"
@ -1130,7 +1160,7 @@ checksum = "a948666b637a0f465e8564c73e89d4dde00d72d4d473cc972f390fc3dcee7d9c"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn 2.0.44", "syn 2.0.46",
] ]
[[package]] [[package]]
@ -1237,18 +1267,18 @@ dependencies = [
[[package]] [[package]]
name = "proc-macro2" name = "proc-macro2"
version = "1.0.73" version = "1.0.74"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2dd5e8a1f1029c43224ad5898e50140c2aebb1705f19e67c918ebf5b9e797fe1" checksum = "2de98502f212cfcea8d0bb305bd0f49d7ebdd75b64ba0a68f937d888f4e0d6db"
dependencies = [ dependencies = [
"unicode-ident", "unicode-ident",
] ]
[[package]] [[package]]
name = "quote" name = "quote"
version = "1.0.34" version = "1.0.35"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "22a37c9326af5ed140c86a46655b5278de879853be5573c01df185b6f49a580a" checksum = "291ec9ab5efd934aaf503a6466c5d5251535d108ee747472c3977cc5acc868ef"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
] ]
@ -1473,29 +1503,29 @@ checksum = "836fa6a3e1e547f9a2c4040802ec865b5d85f4014efe00555d7090a3dcaa1090"
[[package]] [[package]]
name = "serde" name = "serde"
version = "1.0.193" version = "1.0.194"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "25dd9975e68d0cb5aa1120c288333fc98731bd1dd12f561e468ea4728c042b89" checksum = "0b114498256798c94a0689e1a15fec6005dee8ac1f41de56404b67afc2a4b773"
dependencies = [ dependencies = [
"serde_derive", "serde_derive",
] ]
[[package]] [[package]]
name = "serde_derive" name = "serde_derive"
version = "1.0.193" version = "1.0.194"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "43576ca501357b9b071ac53cdc7da8ef0cbd9493d8df094cd821777ea6e894d3" checksum = "a3385e45322e8f9931410f01b3031ec534c3947d0e94c18049af4d9f9907d4e0"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn 2.0.44", "syn 2.0.46",
] ]
[[package]] [[package]]
name = "serde_json" name = "serde_json"
version = "1.0.109" version = "1.0.110"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cb0652c533506ad7a2e353cce269330d6afd8bdfb6d75e0ace5b35aacbd7b9e9" checksum = "6fbd975230bada99c8bb618e0c365c2eefa219158d5c6c29610fd09ff1833257"
dependencies = [ dependencies = [
"itoa", "itoa",
"ryu", "ryu",
@ -1616,9 +1646,9 @@ dependencies = [
[[package]] [[package]]
name = "syn" name = "syn"
version = "2.0.44" version = "2.0.46"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "92d27c2c202598d05175a6dd3af46824b7f747f8d8e9b14c623f19fa5069735d" checksum = "89456b690ff72fddcecf231caedbe615c59480c93358a93dfae7fc29e3ebbf0e"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
@ -1660,23 +1690,32 @@ dependencies = [
] ]
[[package]] [[package]]
name = "thiserror" name = "termcolor"
version = "1.0.53" version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index" 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 = [ dependencies = [
"thiserror-impl", "thiserror-impl",
] ]
[[package]] [[package]]
name = "thiserror-impl" name = "thiserror-impl"
version = "1.0.53" version = "1.0.56"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3dcf4a824cce0aeacd6f38ae6f24234c8e80d68632338ebaa1443b5df9e29e19" checksum = "fa0faa943b50f3db30a20aa7e265dbc66076993efed8463e8de414e5d06d3471"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn 2.0.44", "syn 2.0.46",
] ]
[[package]] [[package]]
@ -1899,9 +1938,11 @@ dependencies = [
"diesel", "diesel",
"diesel_migrations", "diesel_migrations",
"dotenvy", "dotenvy",
"env_logger",
"hmac", "hmac",
"jsonwebtoken", "jsonwebtoken",
"lazy_static", "lazy_static",
"log",
"reqwest", "reqwest",
"serde", "serde",
"serde_json", "serde_json",
@ -1944,7 +1985,7 @@ dependencies = [
"once_cell", "once_cell",
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn 2.0.44", "syn 2.0.46",
"wasm-bindgen-shared", "wasm-bindgen-shared",
] ]
@ -1978,7 +2019,7 @@ checksum = "f0eb82fcb7930ae6219a7ecfd55b217f5f0893484b7a13022ebb2b2bf20b5283"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn 2.0.44", "syn 2.0.46",
"wasm-bindgen-backend", "wasm-bindgen-backend",
"wasm-bindgen-shared", "wasm-bindgen-shared",
] ]
@ -1999,6 +2040,37 @@ dependencies = [
"wasm-bindgen", "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]] [[package]]
name = "windows-core" name = "windows-core"
version = "0.52.0" version = "0.52.0"
@ -2176,7 +2248,7 @@ checksum = "9ce1b18ccd8e73a9321186f97e46f9f04b778851177567b1975109d26a08d2a6"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn 2.0.44", "syn 2.0.46",
] ]
[[package]] [[package]]

View file

@ -14,7 +14,9 @@ chrono = { version = "0.4", features = ["serde"] }
diesel = { version = "2", features = ["r2d2", "postgres", "chrono"] } diesel = { version = "2", features = ["r2d2", "postgres", "chrono"] }
diesel_migrations = "2" diesel_migrations = "2"
dotenvy = "*" dotenvy = "*"
env_logger = "0.10"
hmac = "0.12" hmac = "0.12"
log = "0.4"
reqwest = { version = "0.11", features = ["blocking", "json"] } reqwest = { version = "0.11", features = ["blocking", "json"] }
sha2 = "0.10" sha2 = "0.10"
lazy_static = "1" lazy_static = "1"

View file

@ -16,6 +16,7 @@ lazy_static! {
} }
pub fn init() { pub fn init() {
log::info!("Initializing db pool.");
lazy_static::initialize(&POOL); lazy_static::initialize(&POOL);
let _conn = connection().expect("Failed to get db connection."); let _conn = connection().expect("Failed to get db connection.");
} }

View file

@ -5,7 +5,7 @@ use jsonwebtoken::{decode, DecodingKey, EncodingKey, TokenData, Validation};
const JWT_SECRET: &str = "secret"; const JWT_SECRET: &str = "secret";
pub fn get_token(token: &str) -> Result<TokenData<JWTClaims>, Error> { pub fn get_token(token: &str) -> Result<TokenData<JWTClaims>, Error> {
let token = decode::<JWTClaims>(&token, &get_decoding_key(), &Validation::default())?; let token = decode::<JWTClaims>(token, &get_decoding_key(), &Validation::default())?;
Ok(token) Ok(token)
} }

View file

@ -1,9 +1,13 @@
use crate::middlewares::error::ErrorResponse; use crate::middlewares::error::ErrorResponse;
use crate::models::playlists::{PlaylistCreator, Playlists}; use crate::models::playlists::{PlaylistCreator, Playlists};
use crate::models::tracks::TracksWithArtists; use crate::models::tracks::TracksWithArtists;
use actix_web::{get, web, HttpResponse}; use actix_web::{get, web, HttpResponse, Scope};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
pub fn routes() -> Scope {
web::scope("/playlists").service(get_playlist)
}
#[derive(Deserialize, Serialize)] #[derive(Deserialize, Serialize)]
struct GetPlaylistResponse { struct GetPlaylistResponse {
pub id: String, pub id: String,
@ -15,7 +19,7 @@ struct GetPlaylistResponse {
} }
#[get("/{playlist_id}")] #[get("/{playlist_id}")]
pub async fn get_playlist(path: web::Path<String>) -> Result<HttpResponse, ErrorResponse> { async fn get_playlist(path: web::Path<String>) -> Result<HttpResponse, ErrorResponse> {
let playlist_id = path.into_inner(); let playlist_id = path.into_inner();
let playlist = Playlists::find(playlist_id.as_str())?; let playlist = Playlists::find(playlist_id.as_str())?;

View file

@ -1,6 +1,6 @@
use crate::middlewares::error::ErrorResponse;
use crate::models::spotify; use crate::models::spotify;
use crate::services::spotify as Spotify; use crate::services::spotify as Spotify;
use crate::{middlewares::error::ErrorResponse, models::tracks::Tracks};
use actix_web::{get, web, HttpResponse, Result, Scope}; use actix_web::{get, web, HttpResponse, Result, Scope};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
@ -21,11 +21,18 @@ struct SearchResponse {
#[derive(Serialize)] #[derive(Serialize)]
struct SearchTracks { struct SearchTracks {
pub id: String, pub id: String,
pub title: String,
// pub artists: String,
pub duration_ms: i32,
} }
impl From<spotify::Track> for SearchTracks { impl From<spotify::Track> for SearchTracks {
fn from(value: spotify::Track) -> SearchTracks { fn from(value: spotify::Track) -> SearchTracks {
SearchTracks { id: value.id } SearchTracks {
id: value.id,
title: value.name,
duration_ms: value.duration_ms,
}
} }
} }

View file

@ -106,8 +106,6 @@ impl Service for Spotify {
.send() .send()
.await; .await;
println!("Hello");
match response { match response {
Ok(res) => { Ok(res) => {
let data = res.json::<SpotifyResponse>().await; let data = res.json::<SpotifyResponse>().await;