|
|
|
@ -2,13 +2,11 @@ use warp::{ |
|
|
|
|
Filter, |
|
|
|
|
Rejection, |
|
|
|
|
Reply, |
|
|
|
|
body, |
|
|
|
|
http::StatusCode |
|
|
|
|
body |
|
|
|
|
}; |
|
|
|
|
use std::{ |
|
|
|
|
collections::HashMap, |
|
|
|
|
sync::Arc, |
|
|
|
|
marker::Send |
|
|
|
|
sync::Arc |
|
|
|
|
}; |
|
|
|
|
use env_logger; |
|
|
|
|
use tokio::sync::Mutex; |
|
|
|
@ -20,34 +18,28 @@ use u_lib::{ |
|
|
|
|
contracts::* |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
use serde::Deserialize; |
|
|
|
|
|
|
|
|
|
type SharedStorage = Arc<Mutex<CliStorage>>; |
|
|
|
|
|
|
|
|
|
fn get_content() |
|
|
|
|
-> impl Filter<Extract = (Message<ClientInfo>,), Error = Rejection> + Clone { |
|
|
|
|
fn get_content() -> impl Filter< |
|
|
|
|
Extract = (Message<'static, ClientInfo>,), |
|
|
|
|
Error = Rejection |
|
|
|
|
> + Clone { |
|
|
|
|
body::content_length_limit(1024*64).and(body::json::<Message<ClientInfo>>()) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
async fn add_client(msg: Message<ClientInfo>, db: SharedStorage) -> |
|
|
|
|
async fn add_client(msg: Message<'_, ClientInfo>, db: SharedStorage) -> |
|
|
|
|
Result<impl Reply, Rejection> { |
|
|
|
|
let new_cli = msg.item; |
|
|
|
|
let mut clients = db.lock().await; |
|
|
|
|
if clients.contains_key(&new_cli.id) { |
|
|
|
|
Ok(warp::reply::json( |
|
|
|
|
&Message::new( |
|
|
|
|
&new_cli.id, |
|
|
|
|
&RawMsg("Already exist".to_string()) |
|
|
|
|
) |
|
|
|
|
&RawMsg("Already exist".to_string()).into_message() |
|
|
|
|
)) |
|
|
|
|
} else { |
|
|
|
|
let id = new_cli.id.clone(); |
|
|
|
|
clients.insert(new_cli.id.clone(), new_cli); |
|
|
|
|
clients.insert(new_cli.id.clone(), new_cli.into_owned()); |
|
|
|
|
Ok(warp::reply::json( |
|
|
|
|
&Message::new( |
|
|
|
|
&id, |
|
|
|
|
&RawMsg("Added".to_string()) |
|
|
|
|
)) |
|
|
|
|
&RawMsg("Added".to_string()).into_message() |
|
|
|
|
) |
|
|
|
|
) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
@ -55,8 +47,12 @@ Result<impl Reply, Rejection> { |
|
|
|
|
async fn listing(db: SharedStorage) -> |
|
|
|
|
Result<impl Reply, Rejection> { |
|
|
|
|
let clients = db.lock().await; |
|
|
|
|
let mut result: Vec<ClientInfo> = Vec::with_capacity(clients.len()); |
|
|
|
|
for cli in clients.values() { |
|
|
|
|
result.push(cli.clone()); |
|
|
|
|
} |
|
|
|
|
Ok(warp::reply::json( |
|
|
|
|
&Message::new(&Uuid::nil(), clients) |
|
|
|
|
&Message::new_owned(result) |
|
|
|
|
)) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -69,9 +65,6 @@ async fn main() { |
|
|
|
|
); |
|
|
|
|
let db = warp::any().map(move || Arc::clone(&base_db)); |
|
|
|
|
|
|
|
|
|
let hello = warp::get().and(warp::path::param()) |
|
|
|
|
.map(|p: String| format!("Hello, {}", p)); |
|
|
|
|
|
|
|
|
|
let new_client = warp::post() |
|
|
|
|
.and(warp::path("new")) |
|
|
|
|
.and(get_content()) |
|
|
|
@ -83,10 +76,9 @@ async fn main() { |
|
|
|
|
.and(db.clone()) |
|
|
|
|
.and_then(listing); |
|
|
|
|
|
|
|
|
|
let routes = hello |
|
|
|
|
.or(new_client) |
|
|
|
|
let routes = new_client |
|
|
|
|
.or(ls) |
|
|
|
|
.with(warp::log("warp")); |
|
|
|
|
warp::serve(routes) |
|
|
|
|
.run((MASTER_SERVER.octets(), MASTER_PORT)).await; |
|
|
|
|
.run(([0,0,0,0], MASTER_PORT)).await; |
|
|
|
|
} |
|
|
|
|