monorepo/music-player/server/src/bin/server.rs

159 lines
5.0 KiB
Rust
Raw Normal View History

use flow::Flow;
2023-02-23 16:03:30 +00:00
use std::{
io::stdin,
net::{IpAddr, Ipv4Addr, SocketAddr},
path::PathBuf,
sync::Arc,
thread,
time::Duration,
};
use warp::{Filter, Reply};
use music_player::{core::Core, database::MemoryIndex};
/*
fn tracks() -> Vec<Track> {
vec![
Track {
track_number: Some(1),
name: Some("Underground".to_owned()),
album: Some("Artemis".to_owned()),
artist: Some("Lindsey Stirling".to_owned()),
path: PathBuf::from("/mnt/music/Lindsey Stirling/Artemis/01 - Underground.ogg"),
},
Track {
track_number: Some(2),
name: Some("Artemis".to_owned()),
album: Some("Artemis".to_owned()),
artist: Some("Lindsey Stirling".to_owned()),
path: PathBuf::from("/mnt/music/Lindsey Stirling/Artemis/02 - Artemis.ogg"),
},
Track {
track_number: Some(3),
name: Some("Til the Light Goes Out".to_owned()),
album: Some("Artemis".to_owned()),
artist: Some("Lindsey Stirling".to_owned()),
path: PathBuf::from(
"/mnt/music/Lindsey Stirling/Artemis/03 - Til the Light Goes Out.ogg",
),
},
Track {
track_number: Some(4),
name: Some("Between Twilight".to_owned()),
album: Some("Artemis".to_owned()),
artist: Some("Lindsey Stirling".to_owned()),
path: PathBuf::from("/mnt/music/Lindsey Stirling/Artemis/04 - Between Twilight.ogg"),
},
Track {
track_number: Some(5),
name: Some("Foreverglow".to_owned()),
album: Some("Artemis".to_owned()),
artist: Some("Lindsey Stirling".to_owned()),
path: PathBuf::from("/mnt/music/Lindsey Stirling/Artemis/05 - Foreverglow.ogg"),
},
]
}
*/
2023-02-23 16:03:30 +00:00
enum Bundle {
Index,
App,
Styles,
}
impl Bundle {
fn read(self, root: PathBuf) -> String {
let mut path = root;
match self {
Bundle::Index => path.push(PathBuf::from("index.html")),
Bundle::App => path.push(PathBuf::from("bundle.js")),
Bundle::Styles => path.push(PathBuf::from("styles.css")),
};
println!("path: {:?}", path);
std::fs::read_to_string(path).expect("to find the file")
}
}
#[tokio::main]
pub async fn main() {
2023-02-23 16:03:30 +00:00
let dev = std::env::var("DEV")
.ok()
.and_then(|v| v.parse::<bool>().ok())
.unwrap_or(false);
let bundle_root = std::env::var("BUNDLE_ROOT")
.map(|b| PathBuf::from(b))
.unwrap();
2023-02-23 16:03:30 +00:00
println!("config: {:?} {:?}", dev, bundle_root);
2023-02-23 16:03:30 +00:00
let index = warp::path!().and(warp::get()).map({
let bundle_root = bundle_root.clone();
move || {
warp::http::Response::builder()
.header("content-type", "text/html")
.body(Bundle::Index.read(bundle_root.clone()))
}
});
let app = warp::path!("bundle.js").and(warp::get()).map({
let bundle_root = bundle_root.clone();
move || {
warp::http::Response::builder()
.header("content-type", "text/javascript")
.body(Bundle::App.read(bundle_root.clone()))
}
});
let styles = warp::path!("styles.css").and(warp::get()).map({
let bundle_root = bundle_root.clone();
move || {
warp::http::Response::builder()
.header("content-type", "text/css")
.body(Bundle::Styles.read(bundle_root.clone()))
}
});
/*
let devices = warp::path!("api" / "v1" / "devices")
.and(warp::get())
.map(|| {
let conn = Connection::new_session().expect("to connect to dbus");
warp::reply::json(&list_devices(conn))
});
let track_list = warp::path!("api" / "v1" / "tracks")
.and(warp::get())
.map(|| warp::reply::json(&tracks()));
let tracks_for_artist = warp::path!("api" / "v1" / "artist" / String)
.and(warp::get())
.map(|_artist: String| warp::reply::json(&tracks()));
let tracks_for_album = warp::path!("api" / "v1" / "album" / String)
.and(warp::get())
.map(|_album: String| warp::reply::json(&tracks()));
let queue = warp::path!("api" / "v1" / "queue")
.and(warp::get())
.map(|| {
let conn = Connection::new_session().expect("to connect to dbus");
warp::reply::json(&list_tracks(conn))
});
let playing_status = warp::path!("api" / "v1" / "play-pause")
.and(warp::get())
.map(|| warp::reply::json(&PlayPause::Paused));
let routes = devices
.or(track_list)
.or(tracks_for_album)
.or(tracks_for_artist)
.or(queue)
.or(playing_status);
2023-02-23 16:03:30 +00:00
*/
let routes = index.or(app).or(styles);
let server = warp::serve(routes);
server
.run(SocketAddr::new(
IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)),
8002,
))
.await;
}