From bbb2e2c8eaf48df9431f76f15f04ec8cd35608d3 Mon Sep 17 00:00:00 2001 From: kale Date: Mon, 3 Jun 2024 20:00:06 +0200 Subject: [PATCH] feat: Make a builder function for the stream request route --- server/src/main.rs | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/server/src/main.rs b/server/src/main.rs index e4a18ed..793c02f 100644 --- a/server/src/main.rs +++ b/server/src/main.rs @@ -2,7 +2,7 @@ pub mod error; use error::{handle_rejection, InternalServerError, NotFoundError}; use hyper::{body, Body}; -use std::{path::PathBuf, time::Duration}; +use std::path::PathBuf; use bytes::Bytes; use clap::Parser as _; @@ -16,7 +16,7 @@ use warp::{ Filter, }; -#[derive(clap::Parser)] +#[derive(clap::Parser, Clone)] pub struct Args { music_dir: PathBuf, } @@ -29,17 +29,24 @@ async fn main() { let route = warp::get() .and(warp::path("track")) - .and(any().map(move || config)) - .and(warp::header::headers_cloned()) - .and(param()) - .and_then(serve_local_tracks); + .and(serve_local_tracks(config)); warp::serve(route.recover(handle_rejection)) .run(([127, 0, 0, 1], 8080)) .await; } -pub async fn serve_local_tracks( +pub fn serve_local_tracks( + config: &'static Args, +) -> impl Filter + Clone { + any() + .map(move || config) + .and(warp::header::headers_cloned()) + .and(param()) + .and_then(handle_file_request) +} + +pub async fn handle_file_request( config: &Args, _headers: HeaderMap, // TODO: request files based off of their MusicBrainz Identifier instead