~jojo/jucast

252c0095d032bc29f8712ba716916dc40f4cf348 — JoJo 3 months ago efa77d7 master
Basic play/stop controls via html form + hyper
4 files changed, 153 insertions(+), 15 deletions(-)

M Cargo.lock
M Cargo.toml
M src/main.rs
A test.html
M Cargo.lock => Cargo.lock +79 -0
@@ 37,6 37,16 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1"

[[package]]
name = "form_urlencoded"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ece68d15c92e84fa4f19d3780f1294e5ca82a78a6d515f1efaabcc144688be00"
dependencies = [
 "matches",
 "percent-encoding",
]

[[package]]
name = "futures"
version = "0.3.8"
source = "registry+https://github.com/rust-lang/crates.io-index"


@@ 224,6 234,17 @@ dependencies = [
]

[[package]]
name = "idna"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "02e2673c30ee86b5b96a9cb52ad15718aa1f966f5ab9ad54a8b95d5ca33120a9"
dependencies = [
 "matches",
 "unicode-bidi",
 "unicode-normalization",
]

[[package]]
name = "indexmap"
version = "1.6.1"
source = "registry+https://github.com/rust-lang/crates.io-index"


@@ 246,6 267,7 @@ dependencies = [
 "futures",
 "hyper",
 "tokio",
 "url",
]

[[package]]


@@ 270,6 292,12 @@ dependencies = [
]

[[package]]
name = "matches"
version = "0.1.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7ffc5c5338469d4d3ea17d269fa8ea3512ad247247c30bd2df69e68309ed0a08"

[[package]]
name = "memchr"
version = "2.3.4"
source = "registry+https://github.com/rust-lang/crates.io-index"


@@ 324,6 352,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "13bd41f508810a131401606d54ac32a467c97172d74ba7662562ebba5ad07fa0"

[[package]]
name = "percent-encoding"
version = "2.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d4fd5641d01c8f18a23da7b6fe29298ff4b55afcccdf78973b24cf3175fee32e"

[[package]]
name = "pin-project"
version = "0.4.27"
source = "registry+https://github.com/rust-lang/crates.io-index"


@@ 443,6 477,21 @@ dependencies = [
]

[[package]]
name = "tinyvec"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ccf8dbc19eb42fba10e8feaaec282fb50e2c14b2726d6301dbfeed0f73306a6f"
dependencies = [
 "tinyvec_macros",
]

[[package]]
name = "tinyvec_macros"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cda74da7e1a664f795bb1f8a87ec406fb89a02522cf6e50620d016add6dbbf5c"

[[package]]
name = "tokio"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"


@@ 541,12 590,42 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "59547bce71d9c38b83d9c0e92b6066c4253371f15005def0c30d9657f50c7642"

[[package]]
name = "unicode-bidi"
version = "0.3.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "49f2bd0c6468a8230e1db229cff8029217cf623c767ea5d60bfbd42729ea54d5"
dependencies = [
 "matches",
]

[[package]]
name = "unicode-normalization"
version = "0.1.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a13e63ab62dbe32aeee58d1c5408d35c36c392bba5d9d3142287219721afe606"
dependencies = [
 "tinyvec",
]

[[package]]
name = "unicode-xid"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f7fe0bb3479651439c9112f72b6c505038574c9fbb575ed1bf3b797fa39dd564"

[[package]]
name = "url"
version = "2.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5909f2b0817350449ed73e8bcd81c8c3c8d9a7a5d8acba4b27db277f1868976e"
dependencies = [
 "form_urlencoded",
 "idna",
 "matches",
 "percent-encoding",
]

[[package]]
name = "want"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"

M Cargo.toml => Cargo.toml +2 -1
@@ 9,4 9,5 @@ edition = "2018"
[dependencies]
tokio = { version = "1.0", features = ["rt-multi-thread", "process", "time", "macros"] }
futures = "0.3"
hyper = { version = "0.14", features = ["full"] }
\ No newline at end of file
hyper = { version = "0.14", features = ["full"] }
url = "2.2"
\ No newline at end of file

M src/main.rs => src/main.rs +53 -14
@@ 1,7 1,11 @@
#![feature(async_closure)]

use futures::{channel::mpsc, future::Either::*, pin_mut, prelude::*};
use std::{process::Stdio, time::Duration};
use hyper::{
    service::{make_service_fn, service_fn},
    Body, Response, Server, StatusCode,
};
use std::{collections::HashMap, convert::Infallible, process::Stdio};
use tokio::process::Command;

enum Msg {


@@ 10,8 14,8 @@ enum Msg {
}

#[tokio::main]
async fn main() {
    let (mut tx, mut rx) = mpsc::unbounded();
async fn main() -> Result<(), hyper::Error> {
    let (tx, mut rx) = mpsc::unbounded();

    // Player thread
    tokio::spawn(async move {


@@ 37,15 41,50 @@ async fn main() {
            }
        }
    });
    let vs = [
        "https://www.youtube.com/watch?v=a8c5wmeOL9o",
        "/home/jojo/Music/Lion/Transformers The Movie/Transformers Theme.mp3",
        "https://www.youtube.com/watch?v=8jZUETfIfNo",
    ];
    for v in &vs {
        tx.send(Msg::Play(v.to_string())).await.unwrap();
        std::thread::sleep(Duration::from_secs(4));
        tx.send(Msg::Stop).await.unwrap();
        std::thread::sleep(Duration::from_secs(4));
    }

    let make_service = make_service_fn(|_conn| {
        let tx = tx.clone();
        async {
            Ok::<_, Infallible>(service_fn(move |req| {
                let mut tx = tx.clone();
                async move {
                    let b = hyper::body::to_bytes(req).await.unwrap();
                    let params = url::form_urlencoded::parse(b.as_ref())
                        .into_owned()
                        .collect::<HashMap<String, String>>();
                    println!("params: {:?}", params);
                    match params.get("action").map(|s| s.as_str()) {
                        Some("stop") => tx.send(Msg::Stop).await.unwrap(),
                        Some("play") => {
                            if let Some(src) = params.get("src") {
                                tx.send(Msg::Play(src.clone())).await.unwrap()
                            }
                        }
                        Some(a) => println!("invalid action:\n{}", a),
                        None => (),
                    }
                    Response::builder()
                        .status(StatusCode::OK)
                        .body(Body::from("ok"))
                }
            }))
        }
    });

    let addr = ([0, 0, 0, 0], 8121).into();
    let server = Server::bind(&addr).serve(make_service);
    println!("Listening on {}", addr);
    server.await

    // let vs = [
    //     "https://www.youtube.com/watch?v=a8c5wmeOL9o",
    //     "/home/jojo/Music/Lion/Transformers The Movie/Transformers Theme.mp3",
    //     "https://www.youtube.com/watch?v=8jZUETfIfNo",
    // ];
    // for v in &vs {
    //     tx.send(Msg::Play(v.to_string())).await.unwrap();
    //     std::thread::sleep(Duration::from_secs(4));
    //     tx.send(Msg::Stop).await.unwrap();
    //     std::thread::sleep(Duration::from_secs(4));
    // }
}

A test.html => test.html +19 -0
@@ 0,0 1,19 @@
<!DOCTYPE html>
<html>
  <body>
    <!-- <input type="file" id="mediaupload" name="mediaupload" accept="audio/*, video/*, image/*"> -->

    <form method="POST" enctype="application/x-www-form-urlencoded" action="http://localhost:8121">
      <input type="hidden" name="action" value="play">
      <input type="string" name="src" placeholder="Youtube URL or such">
      <input type="submit" value="Play">
    </form>

    <hr/>

    <form method="POST" enctype="application/x-www-form-urlencoded" action="http://localhost:8121">
      <input type="hidden" name="action" value="stop">
      <input type="submit" value="Stop">
    </form>
  </body>
</html>