mini-player: wip

This commit is contained in:
Agahnim 2026-03-19 19:17:53 +01:00
parent db3c7657a3
commit 4611980c23
Signed by: Agahnim
SSH key fingerprint: SHA256:Zj65PJnE0dRYye8Ltk/qDglynyXUxJngQ9qqx/VI+b4
13 changed files with 217 additions and 3 deletions

30
src/domain.rs Normal file
View file

@ -0,0 +1,30 @@
use serde::Deserialize;
pub struct AppState {
pub mp_tracks: Vec<Track>,
pub p_tracks: Vec<Track>,
}
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,
}

View file

@ -1 +1,2 @@
pub mod domain;
pub mod templates;

View file

@ -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)]

21
src/state.rs Normal file
View file

@ -0,0 +1,21 @@
pub struct AppState {
mp_tracks: Vec<Track>,
p_tracks: Vec<Track>,
}
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"),
}
}
}

View file

@ -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<Track>,
}
pub async fn miniplayer(State(state): State<Arc<AppState>>) -> impl IntoResponse {
Html(
MiniPlayerTemplate {
tracks: state.mp_tracks.clone(),
}
.render()
.unwrap(),
)
}

View file

@ -1,2 +1,3 @@
pub mod index;
pub mod miniplayer;
pub mod notfound;