use std::convert::Infallible; use std::convert::TryInto; use std::env; use warp::Filter; mod structs; mod utils; use reqwest::Client; use tokio::runtime; extern crate warp; async fn handle_album(id: String, client: Client) -> Result { let (status_code, reply) = utils::generate_html(utils::get_album(client.clone(), &id).await); Ok(warp::reply::with_status( warp::reply::with_header(reply, "Content-Type", "text/html"), status_code.try_into().unwrap(), )) } async fn handle_media(id: String, client: Client) -> Result { let (status_code, reply) = utils::generate_html(utils::get_media(client.clone(), &id).await); Ok(warp::reply::with_status( warp::reply::with_header(reply, "Content-Type", "text/html"), status_code.try_into().unwrap(), )) } async fn async_main(port: u16) { let client = Client::new(); let client = warp::any().map(move || client.clone()); let album_path = warp::path!("a" / String) .and(client.clone()) .and_then(handle_album); let media_path = warp::path!(String).and(client).and_then(handle_media); let not_found_handler = warp::any().map(|| warp::reply::with_status( warp::reply::html("
404: Not Found
"), 404.try_into().unwrap(), )); let routes = warp::filters::method::get().and(album_path.or(media_path).or(not_found_handler)); eprintln!("Serving on 0.0.0.0:{}", port); warp::serve(routes).run(([0u8, 0, 0, 0], port)).await; } fn main() { let port = match env::var("PORT") { Ok(port) => port.parse().unwrap(), Err(env::VarError::NotPresent) => 8080, Err(env::VarError::NotUnicode(_)) => panic!("Environment variable PORT isn't unicode"), }; runtime::Builder::new_multi_thread() .enable_all() .build() .unwrap() .block_on(async_main(port)); }