parent
e5a675fe45
commit
19a3943012
File diff suppressed because it is too large
Load Diff
@ -1,47 +1,81 @@
|
|||||||
use chain::{
|
use chain::{
|
||||||
Chain,
|
Chain,
|
||||||
};
|
};
|
||||||
|
use warp::{
|
||||||
|
Filter,
|
||||||
|
Buf,
|
||||||
|
};
|
||||||
use std::{
|
use std::{
|
||||||
io::{
|
sync::Arc,
|
||||||
BufRead,
|
};
|
||||||
self,
|
use tokio::{
|
||||||
|
sync::{
|
||||||
|
RwLock,
|
||||||
},
|
},
|
||||||
|
stream::{Stream,StreamExt,},
|
||||||
|
prelude::*,
|
||||||
};
|
};
|
||||||
|
|
||||||
fn buffered_read_all_lines<T: BufRead+?Sized, F: FnMut(&str) -> io::Result<()>>(input: &mut T, mut then: F) -> io::Result<usize>
|
const MAX_CONTENT_LENGTH: u64 = 1024 * 16;
|
||||||
{
|
const MAX_GEN_SIZE: usize = 256;
|
||||||
let mut buffer = String::new();
|
|
||||||
let mut read;
|
async fn full_body(chain: &mut Chain<String>, mut body: impl Unpin + Stream<Item = Result<impl Buf, impl std::error::Error + 'static>>) -> Result<(), Box<dyn std::error::Error>> {
|
||||||
let mut total=0;
|
let mut buffer = Vec::new();
|
||||||
while {read = input.read_line(&mut buffer)?; read!=0} {
|
|
||||||
then(&buffer[..])?;
|
while let Some(buf) = body.next().await {
|
||||||
buffer.clear();
|
let mut body = buf?;
|
||||||
total += read;
|
while body.has_remaining() {
|
||||||
|
buffer.extend_from_slice(body.bytes());
|
||||||
|
let cnt = body.bytes().len();
|
||||||
|
body.advance(cnt);
|
||||||
}
|
}
|
||||||
Ok(total)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
let buffer = std::str::from_utf8(&buffer[..])?;
|
||||||
let stdin = io::stdin();
|
chain.feed_str(buffer);
|
||||||
let mut stdin = stdin.lock();
|
Ok(())
|
||||||
let mut chain = Chain::new();
|
}
|
||||||
|
|
||||||
buffered_read_all_lines(&mut stdin, |string| {
|
#[tokio::main]
|
||||||
chain.feed(&string.split_whitespace()
|
async fn main() {
|
||||||
.filter(|word| !word.is_empty())
|
pretty_env_logger::init();
|
||||||
.map(|s| s.to_owned()).collect::<Vec<_>>());
|
|
||||||
|
|
||||||
Ok(())
|
let chain = Arc::new(RwLock::new(Chain::new()));
|
||||||
}).expect("Failed to read from stdin");
|
let chain = warp::any().map(move || Arc::clone(&chain));
|
||||||
|
|
||||||
if !chain.is_empty() {
|
let push = warp::put()
|
||||||
if let Some(num) = std::env::args().skip(1).next() {
|
.and(chain.clone())
|
||||||
let sz: usize = num.parse().expect("Cannot parse number of tokens to generate");
|
.and(warp::path("put"))
|
||||||
for string in chain.str_iter_for(sz) {
|
.and(warp::body::content_length_limit(MAX_CONTENT_LENGTH))
|
||||||
println!("{}", string);
|
.and(warp::body::stream())
|
||||||
|
.and_then(|chain: Arc<RwLock<Chain<String>>>, buf| {
|
||||||
|
async move {
|
||||||
|
use std::ops::DerefMut;
|
||||||
|
let res = format!("{:?}", full_body(chain.write().await.deref_mut(), buf).await);
|
||||||
|
Ok::<String, std::convert::Infallible>(res)
|
||||||
}
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
let read = warp::get()
|
||||||
|
.and(chain.clone())
|
||||||
|
.and(warp::path("get"))
|
||||||
|
.and(warp::path::param().map(|opt: usize| Some(opt)).or(warp::any().map(|| Option::<usize>::None)).unify())
|
||||||
|
.and_then(|chain: Arc<RwLock<Chain<String>>>, num: Option<usize>| {
|
||||||
|
async move {
|
||||||
|
let chain = chain.read().await;
|
||||||
|
if chain.is_empty() {
|
||||||
|
Ok(String::default())
|
||||||
} else {
|
} else {
|
||||||
println!("{}", chain.generate_str());
|
match num {
|
||||||
|
Some(num) if num < MAX_GEN_SIZE => Ok(chain.str_iter_for(num).collect()),
|
||||||
|
_ => Ok::<String, std::convert::Infallible>(chain.generate_str()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
warp::serve(push
|
||||||
|
.or(read))
|
||||||
|
.bind_with_graceful_shutdown(([127,0,0,1], 8777), async { tokio::signal::ctrl_c().await.unwrap(); }).1
|
||||||
|
.await
|
||||||
|
}
|
||||||
|
Loading…
Reference in new issue