mod daemon; mod listenbrainz; mod models; use navidrome::{ client::{builder::NavidromeBuilder, library::Library}, models::navidrome::NavidromeTrack, }; use crate::daemon::{update_playlists_daemon, update_tracks_daemon}; use crate::listenbrainz::StatsRange; use axum::extract::State; use axum::routing::get; use axum::Router; use sqlx::postgres::PgPool; use std::collections::HashMap; use std::env; use std::net::SocketAddr; use std::sync::Arc; use tokio::sync::RwLock; #[derive(Clone)] struct TrackState { tracks: Arc>>, } #[tokio::main] async fn main() -> Result<(), Box> { let database = PgPool::connect(&env::var("DATABASE_URL")?).await?; sqlx::migrate!().run(&database).await?; let media_client = NavidromeBuilder::new() .base(env::var("NAVIDROME_BASE")?) .token(env::var("NAVIDROME_TOKEN")?) .build()?; let mut tracks = Arc::new(RwLock::new(HashMap::new())); let state = TrackState { tracks: tracks.clone(), }; tokio::spawn(async move { update_playlists_daemon().await.unwrap(); }); tokio::spawn(async move { update_tracks_daemon(&media_client, &mut tracks) .await .unwrap(); }); let app = Router::new().route("/", get(index)).with_state(database); let addr = SocketAddr::from(([127, 0, 0, 1], 3000)); axum::Server::bind(&addr) .serve(app.into_make_service()) .await?; Ok(()) } async fn index(State(pool): State) -> String { let response = listenbrainz::recordings("jellywx", StatsRange::Week) .await .unwrap(); response .recordings .iter() .map(|a| a.track_name.clone()) .collect::>() .join(", ") }