summaryrefslogtreecommitdiff
path: root/src/requests/start.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/requests/start.rs')
-rw-r--r--src/requests/start.rs73
1 files changed, 60 insertions, 13 deletions
diff --git a/src/requests/start.rs b/src/requests/start.rs
index 30f65b9..d0c4411 100644
--- a/src/requests/start.rs
+++ b/src/requests/start.rs
@@ -1,37 +1,57 @@
1use std::time::Duration;
2
3use futures_util::{StreamExt, SinkExt};
4use indicatif::{ProgressBar, ProgressStyle};
1use reqwest::StatusCode; 5use reqwest::StatusCode;
2use serde::Deserialize; 6use serde::Deserialize;
7use tokio_tungstenite::{connect_async, tungstenite::Message};
3 8
4use crate::{config::SETTINGS, error::CliError, default_headers, ErrorResponse}; 9use crate::{error::CliError, default_headers, ErrorResponse, format_url, Protocols};
5 10
6pub fn start(id: String) -> Result<(), CliError> { 11pub async fn start(id: String, ping: bool) -> Result<(), CliError> {
7 let res = reqwest::blocking::Client::new() 12
8 .post( 13 let send_start = ProgressBar::new(1);
9 format!( 14
10 "{}/start", 15 // TODO: calculate average start-time on server
11 SETTINGS.get_string("server").map_err(CliError::Config)? 16 send_start.set_style(
12 ) 17 ProgressStyle::with_template("{spinner:.green} ({elapsed}) {wide_msg}")
13 ) 18 .unwrap()
19 .tick_chars("|/-\\\\")
20 );
21
22 let url = format_url("start", Protocols::Http)?;
23
24 send_start.set_message(format!("connect to {}", url));
25 send_start.enable_steady_tick(Duration::from_millis(125));
26
27 let res = reqwest::Client::new()
28 .post(url)
14 .headers(default_headers()?) 29 .headers(default_headers()?)
15 .body( 30 .body(
16 format!(r#"{{"id": "{}"}}"#, id) 31 format!(r#"{{"id": "{}", "ping": {}}}"#, id, ping)
17 ) 32 )
18 .send() 33 .send()
34 .await
19 .map_err(CliError::Reqwest)?; 35 .map_err(CliError::Reqwest)?;
20 36
21 match res.status() { 37 match res.status() {
22 StatusCode::OK => { 38 StatusCode::OK => {
23 let body = serde_json::from_str::<StartResponse>( 39 let body = serde_json::from_str::<StartResponse>(
24 &res.text().map_err(CliError::Reqwest)? 40 &res.text().await.map_err(CliError::Reqwest)?
25 ) 41 )
26 .map_err(CliError::Serde)?; 42 .map_err(CliError::Serde)?;
27 43
28 if body.boot { 44 if body.boot {
29 println!("successfully started {}", body.id); 45 send_start.println("connected, sent start packet");
46 }
47
48 if ping {
49 send_start.println(status_socket(body.uuid, &send_start).await?.to_string());
30 } 50 }
31 }, 51 },
32 _ => { 52 _ => {
33 let body = serde_json::from_str::<ErrorResponse>( 53 let body = serde_json::from_str::<ErrorResponse>(
34 &res.text().map_err(CliError::Reqwest)? 54 &res.text().await.map_err(CliError::Reqwest)?
35 ) 55 )
36 .map_err(CliError::Serde)?; 56 .map_err(CliError::Serde)?;
37 57
@@ -42,8 +62,35 @@ pub fn start(id: String) -> Result<(), CliError> {
42 Ok(()) 62 Ok(())
43} 63}
44 64
65async fn status_socket(uuid: String, pb: &ProgressBar) -> Result<bool, CliError> {
66 pb.set_message("setup websocket");
67
68 let (mut ws_stream, _response) = connect_async(format_url("status", Protocols::Websocket)?)
69 .await
70 .expect("Failed to connect");
71 pb.println("connected to websocket");
72
73 pb.set_message("send uuid message");
74 ws_stream.send(Message::Text(uuid)).await.unwrap();
75 pb.println("sent uuid message");
76
77 pb.set_message("wait for message");
78 let msg = ws_stream.next().await.unwrap();
79
80 pb.println(format!("msg: {:?}", msg));
81
82 ws_stream.close(None).await.unwrap();
83 pb.println("connection closed");
84 // TODO: Check for correct UUID and timeout
85 pb.set_message("verifying message");
86 if msg.is_ok() { return Ok(true) }
87
88 Ok(false)
89}
90
45#[derive(Debug, Deserialize)] 91#[derive(Debug, Deserialize)]
46struct StartResponse { 92struct StartResponse {
47 boot: bool, 93 boot: bool,
48 id: String, 94 id: String,
95 uuid: String,
49} 96}