mini-player: wip
This commit is contained in:
parent
c4c49dee6b
commit
8b0de4bf93
13 changed files with 217 additions and 3 deletions
30
src/domain.rs
Normal file
30
src/domain.rs
Normal 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,
|
||||
}
|
||||
|
|
@ -1 +1,2 @@
|
|||
pub mod domain;
|
||||
pub mod templates;
|
||||
|
|
|
|||
13
src/main.rs
13
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)]
|
||||
|
|
|
|||
21
src/state.rs
Normal file
21
src/state.rs
Normal 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"),
|
||||
}
|
||||
}
|
||||
}
|
||||
24
src/templates/miniplayer.rs
Normal file
24
src/templates/miniplayer.rs
Normal 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(),
|
||||
)
|
||||
}
|
||||
|
|
@ -1,2 +1,3 @@
|
|||
pub mod index;
|
||||
pub mod miniplayer;
|
||||
pub mod notfound;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue