Integrate with Warp
This commit is contained in:
parent
4bcf78e361
commit
4032ffcecd
907
Cargo.lock
generated
907
Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
@ -11,4 +11,4 @@ license = "AGPL-3.0-or-later"
|
|||||||
legion = "0.2.1"
|
legion = "0.2.1"
|
||||||
rand = "0.7.3"
|
rand = "0.7.3"
|
||||||
tokio = { version = "0.2", features = ["full"] }
|
tokio = { version = "0.2", features = ["full"] }
|
||||||
|
warp = "0.2"
|
||||||
|
34
src/main.rs
34
src/main.rs
@ -1,19 +1,39 @@
|
|||||||
mod simulation;
|
mod simulation;
|
||||||
mod state;
|
mod state;
|
||||||
|
|
||||||
|
use std::convert::Infallible;
|
||||||
|
|
||||||
|
use tokio::sync::watch;
|
||||||
|
use warp::Filter;
|
||||||
|
|
||||||
use simulation::Simulation;
|
use simulation::Simulation;
|
||||||
|
use state::State;
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() {
|
async fn main() {
|
||||||
let mut simulation = Simulation::new();
|
let mut simulation = Simulation::new();
|
||||||
let mut state_channel = simulation.state();
|
let state_channel = simulation.state();
|
||||||
|
|
||||||
tokio::spawn(async move {
|
let sim = tokio::spawn(async move {
|
||||||
simulation.run().await;
|
simulation.run().await;
|
||||||
});
|
});
|
||||||
|
|
||||||
loop {
|
let hello = warp::path!("hello" / String)
|
||||||
let state = state_channel.recv().await.unwrap();
|
.map(move |name| { (name, state_channel.clone()) })
|
||||||
println!("Iteration {}", state.iteration)
|
.and_then(|(name, channel)| async move { hello(name, channel).await });
|
||||||
}
|
|
||||||
|
let server = tokio::spawn(async move { warp::serve(hello).run(([127, 0, 0, 1], 3030)).await });
|
||||||
|
|
||||||
|
tokio::try_join!(sim, server).unwrap();
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn hello(
|
||||||
|
name: String,
|
||||||
|
mut state_channel: watch::Receiver<State>,
|
||||||
|
) -> Result<impl warp::Reply, Infallible> {
|
||||||
|
Ok(format!(
|
||||||
|
"Hello, {}! We're at iteration {}",
|
||||||
|
name,
|
||||||
|
state_channel.recv().await.unwrap().iteration
|
||||||
|
))
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user