diff --git a/Cargo.lock b/Cargo.lock index 03f4127..8babb13 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -8,6 +8,8 @@ version = "0.1.0" dependencies = [ "askama", "axum", + "serde", + "serde_json", "tokio", "tower-http", "tower-livereload", @@ -460,6 +462,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9a8e94ea7f378bd32cbbd37198a4a91436180c5bb472411e48b5ec2e2124ae9e" dependencies = [ "serde_core", + "serde_derive", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index 80c8556..1f6d99e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,6 +6,8 @@ edition = "2024" [dependencies] askama = { version = "0.15.4", features = ["std", "derive"] } axum = "0.8.8" +serde = { version = "1.0.228", features = ["derive"] } +serde_json = "1.0.149" tokio = { version = "1.50.0", features = ["full"] } tower-http = { version = "0.6.8", features = ["fs"] } tower-livereload = "0.10.3" diff --git a/content/mp-tracks.json b/content/mp-tracks.json new file mode 100644 index 0000000..3242421 --- /dev/null +++ b/content/mp-tracks.json @@ -0,0 +1,38 @@ +[ + { + "id": 1, + "title": "Good old times", + "artist": "Agahnim", + "src": "/static/music/times.mp3" + }, + { + "id": 2, + "title": "Identification", + "artist": "Infinity Frequencies", + "src": "/static/music/infinity.mp3" + }, + { + "id": 3, + "title": "Flip the Switch", + "artist": "Stevia Sphere", + "src": "/static/music/switch.mp3" + }, + { + "id": 4, + "title": "Machines Vs Water", + "artist": "Stevia Sphere", + "src": "/static/music/machines.mp3" + }, + { + "id": 5, + "title": "Elevator 1", + "artist": "Stevia Sphere", + "src": "/static/music/elevator.mp3" + }, + { + "id": 6, + "title": "Somewhere Dark", + "artist": "Monodrone", + "src": "/static/music/dark.mp3" + } +] \ No newline at end of file diff --git a/content/p-tracks.json b/content/p-tracks.json new file mode 100644 index 0000000..f9d8f46 --- /dev/null +++ b/content/p-tracks.json @@ -0,0 +1,72 @@ +[ + { + "id": 1, + "title": "I'm getting tired of farewells", + "artist": "Agahnim", + "src": "/static/music/farewells.mp3", + "album": "LIFE" + }, + { + "id": 2, + "title": "DAWN", + "artist": "Agahnim", + "src": "/static/music/DAWN.mp3", + "album": "DAWN" + }, + { + "id": 3, + "title": "Move on", + "artist": "Agahnim", + "src": "/static/music/move_on.mp3", + "album": "DAWN" + }, + { + "id": 4, + "title": "Carcer", + "artist": "Agahnim", + "src": "/static/music/carcer.mp3", + "album": "LIFE" + }, + { + "id": 5, + "title": "Under listening", + "artist": "Agahnim", + "src": "/static/music/listening.mp3", + "album": "Justice" + }, + { + "id": 6, + "title": "Fading away", + "artist": "Agahnim", + "src": "/static/music/fading.mp3", + "album": "LIFE" + }, + { + "id": 7, + "title": "Hindsight", + "artist": "Agahnim", + "src": "/static/music/hindsight.mp3", + "album": "The End" + }, + { + "id": 8, + "title": "With or without", + "artist": "Agahnim", + "src": "/static/music/without.mp3", + "album": "Extinction" + }, + { + "id": 9, + "title": "Perfect Light", + "artist": "Agahnim", + "src": "/static/music/perfect_light.mp3", + "album": "Rebirth" + }, + { + "id": 10, + "title": "Good old times", + "artist": "Agahnim", + "src": "/static/music/times.mp3", + "album": "Extinction" + } +] \ No newline at end of file diff --git a/src/domain.rs b/src/domain.rs new file mode 100644 index 0000000..f9ff12d --- /dev/null +++ b/src/domain.rs @@ -0,0 +1,30 @@ +use serde::Deserialize; + +pub struct AppState { + pub mp_tracks: Vec, + pub p_tracks: Vec, +} + +impl AppState { + pub async fn try_new() -> Self { + Self { + mp_tracks: serde_json::from_str( + &std::fs::read_to_string("content/mp-tracks.json") + .expect("mp-tracks.json non trouvé"), + ) + .expect("JSON invalide"), + p_tracks: serde_json::from_str( + &std::fs::read_to_string("content/p-tracks.json") + .expect("p-tracks.json non trouvé"), + ) + .expect("JSON invalide"), + } + } +} + +#[derive(Deserialize, Clone)] +pub struct Track { + title: String, + artist: String, + src: String, +} diff --git a/src/lib.rs b/src/lib.rs index b9a1684..479173e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1 +1,2 @@ +pub mod domain; pub mod templates; diff --git a/src/main.rs b/src/main.rs index a6121ed..5ac7672 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,9 @@ -use agahnim_web_v2::templates::{index::home, notfound::notfound}; +use std::sync::Arc; + +use agahnim_web_v2::{ + domain::AppState, + templates::{index::home, miniplayer::miniplayer, notfound::notfound}, +}; use axum::{Router, routing::get}; use tower_http::services::ServeDir; #[cfg(debug_assertions)] @@ -6,10 +11,14 @@ use tower_livereload::LiveReloadLayer; #[tokio::main] async fn main() { + let state = Arc::new(AppState::try_new().await); + let app = Router::new() .route("/", get(home)) + .route("/miniplayer", get(miniplayer)) .nest_service("/static", ServeDir::new("static")) - .fallback(notfound); + .fallback(notfound) + .with_state(state); // We need to include this flag so that the live reload layer isn't included when the server is built #[cfg(debug_assertions)] diff --git a/src/state.rs b/src/state.rs new file mode 100644 index 0000000..b22820d --- /dev/null +++ b/src/state.rs @@ -0,0 +1,21 @@ +pub struct AppState { + mp_tracks: Vec, + p_tracks: Vec, +} + +impl AppState { + async fn try_new() -> Self { + Self { + mp_tracks: serde_json::from_str( + &std::fs::read_to_string("content/mp-tracks.json") + .expect("mp-tracks.json non trouvé"), + ) + .expect("JSON invalide"), + p_tracks: serde_json::from_str( + &std::fs::read_to_string("content/p-tracks.json") + .expect("p-tracks.json non trouvé"), + ) + .expect("JSON invalide"), + } + } +} diff --git a/src/templates/miniplayer.rs b/src/templates/miniplayer.rs new file mode 100644 index 0000000..9b72140 --- /dev/null +++ b/src/templates/miniplayer.rs @@ -0,0 +1,24 @@ +use std::sync::Arc; + +use askama::Template; +use axum::{ + extract::State, + response::{Html, IntoResponse}, +}; + +use crate::domain::{AppState, Track}; + +#[derive(Template)] +#[template(path = "partials/miniplayer.html")] +struct MiniPlayerTemplate { + tracks: Vec, +} +pub async fn miniplayer(State(state): State>) -> impl IntoResponse { + Html( + MiniPlayerTemplate { + tracks: state.mp_tracks.clone(), + } + .render() + .unwrap(), + ) +} diff --git a/src/templates/mod.rs b/src/templates/mod.rs index 6685146..b2b9394 100644 --- a/src/templates/mod.rs +++ b/src/templates/mod.rs @@ -1,2 +1,3 @@ pub mod index; +pub mod miniplayer; pub mod notfound; diff --git a/static/style.css b/static/style.css index 5308bb4..a9597b4 100644 --- a/static/style.css +++ b/static/style.css @@ -100,4 +100,14 @@ katcenkat { transform: translateY(500px); } } +} + + + +/* Mini player */ +miniplayer { + position: fixed; + top: 4rem; + left: 1rem; + z-index: 100; } \ No newline at end of file diff --git a/templates/base.html b/templates/base.html index 8f934fd..ee7ea75 100644 --- a/templates/base.html +++ b/templates/base.html @@ -12,7 +12,7 @@ {% include "partials/header.html" %} - +
{% block content %}{% endblock %}
diff --git a/templates/partials/miniplayer.html b/templates/partials/miniplayer.html new file mode 100644 index 0000000..5fb461a --- /dev/null +++ b/templates/partials/miniplayer.html @@ -0,0 +1,3 @@ + +

Salut

+
\ No newline at end of file