Integrate with Warp

This commit is contained in:
Gender Shrapnel 2020-06-01 03:28:29 +02:00
parent 4bcf78e361
commit 4032ffcecd
3 changed files with 922 additions and 21 deletions

907
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -11,4 +11,4 @@ license = "AGPL-3.0-or-later"
legion = "0.2.1"
rand = "0.7.3"
tokio = { version = "0.2", features = ["full"] }
warp = "0.2"

View File

@ -1,19 +1,39 @@
mod simulation;
mod state;
use std::convert::Infallible;
use tokio::sync::watch;
use warp::Filter;
use simulation::Simulation;
use state::State;
#[tokio::main]
async fn main() {
let mut simulation = Simulation::new();
let mut state_channel = simulation.state();
tokio::spawn(async move {
let state_channel = simulation.state();
let sim = tokio::spawn(async move {
simulation.run().await;
});
loop {
let state = state_channel.recv().await.unwrap();
println!("Iteration {}", state.iteration)
}
let hello = warp::path!("hello" / String)
.map(move |name| { (name, state_channel.clone()) })
.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
))
}