Add controller to database
This commit is contained in:
		
							parent
							
								
									9f64075f5a
								
							
						
					
					
						commit
						d193000aec
					
				
					 34 changed files with 1055 additions and 195 deletions
				
			
		| 
						 | 
				
			
			@ -2,8 +2,10 @@ use std::str;
 | 
			
		|||
 | 
			
		||||
use futures::{future, pin_mut, StreamExt};
 | 
			
		||||
use futures::channel::mpsc;
 | 
			
		||||
use tokio::io::{AsyncReadExt, AsyncWriteExt};
 | 
			
		||||
use tokio::io::AsyncReadExt;
 | 
			
		||||
use tokio_tungstenite::{connect_async, tungstenite::protocol::Message};
 | 
			
		||||
use tokio_tungstenite::tungstenite::Error;
 | 
			
		||||
use emgauwa_lib::db;
 | 
			
		||||
 | 
			
		||||
mod settings;
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -11,6 +13,8 @@ mod settings;
 | 
			
		|||
async fn main() {
 | 
			
		||||
	let settings = settings::init();
 | 
			
		||||
 | 
			
		||||
	let _pool = db::init(&settings.database).await;
 | 
			
		||||
 | 
			
		||||
	let url = format!(
 | 
			
		||||
		"ws://{}:{}/api/v1/ws/controllers",
 | 
			
		||||
		settings.core.host,
 | 
			
		||||
| 
						 | 
				
			
			@ -21,17 +25,11 @@ async fn main() {
 | 
			
		|||
	tokio::spawn(read_stdin(stdin_tx));
 | 
			
		||||
 | 
			
		||||
	let (ws_stream, _) = connect_async(url).await.expect("Failed to connect");
 | 
			
		||||
	println!("WebSocket handshake has been successfully completed");
 | 
			
		||||
 | 
			
		||||
	let (write, read) = ws_stream.split();
 | 
			
		||||
 | 
			
		||||
	let stdin_to_ws = stdin_rx.map(Ok).forward(write);
 | 
			
		||||
	let ws_to_stdout = {
 | 
			
		||||
		read.for_each(|message| async {
 | 
			
		||||
			let data = message.unwrap().into_text().unwrap();
 | 
			
		||||
			println!("{}", data);
 | 
			
		||||
		})
 | 
			
		||||
	};
 | 
			
		||||
	let ws_to_stdout = read.for_each(handle_message);
 | 
			
		||||
 | 
			
		||||
	pin_mut!(stdin_to_ws, ws_to_stdout);
 | 
			
		||||
	future::select(stdin_to_ws, ws_to_stdout).await;
 | 
			
		||||
| 
						 | 
				
			
			@ -50,4 +48,11 @@ async fn read_stdin(tx: mpsc::UnboundedSender<Message>) {
 | 
			
		|||
		buf.truncate(n);
 | 
			
		||||
		tx.unbounded_send(Message::text(str::from_utf8(&buf).unwrap())).unwrap();
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
pub async fn handle_message(message_result: Result<Message, Error>) {
 | 
			
		||||
	match message_result {
 | 
			
		||||
		Ok(message) => println!("{}", message.into_text().unwrap()),
 | 
			
		||||
		Err(err) => println!("Error: {}", err)
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue