Add log-init-function and websocket retry
This commit is contained in:
		
							parent
							
								
									6536ff0792
								
							
						
					
					
						commit
						50bcac2a1b
					
				
					 12 changed files with 73 additions and 69 deletions
				
			
		| 
						 | 
				
			
			@ -14,13 +14,14 @@ serde = "1.0"
 | 
			
		|||
serde_json = "1.0"
 | 
			
		||||
serde_derive = "1.0"
 | 
			
		||||
 | 
			
		||||
simple_logger = "4.2"
 | 
			
		||||
log = "0.4"
 | 
			
		||||
 | 
			
		||||
config = "0.13"
 | 
			
		||||
 | 
			
		||||
chrono = { version = "0.4", features = ["serde"] }
 | 
			
		||||
 | 
			
		||||
sqlx = { version = "0.7", features = ["sqlite", "runtime-tokio", "macros", "chrono"] }
 | 
			
		||||
libsqlite3-sys = { version = "*", features = ["bundled"] }
 | 
			
		||||
 | 
			
		||||
log = "0.4"
 | 
			
		||||
uuid = "1.6"
 | 
			
		||||
futures = "0.3"
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -3,3 +3,5 @@ use std::time::Duration;
 | 
			
		|||
pub const DEFAULT_PORT: u16 = 4419;
 | 
			
		||||
pub const HEARTBEAT_INTERVAL: Duration = Duration::from_secs(5);
 | 
			
		||||
pub const HEARTBEAT_TIMEOUT: Duration = Duration::from_secs(15);
 | 
			
		||||
 | 
			
		||||
pub const WEBSOCKET_RETRY_TIMEOUT: Duration = Duration::from_secs(5);
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -9,18 +9,12 @@ use crate::models::{convert_db_list, Controller};
 | 
			
		|||
use crate::types::ConnectedControllersType;
 | 
			
		||||
 | 
			
		||||
#[get("/api/v1/controllers")]
 | 
			
		||||
pub async fn index(
 | 
			
		||||
	pool: web::Data<Pool<Sqlite>>,
 | 
			
		||||
	connected_controllers: web::Data<ConnectedControllersType>,
 | 
			
		||||
) -> Result<HttpResponse, ApiError> {
 | 
			
		||||
pub async fn index(pool: web::Data<Pool<Sqlite>>) -> Result<HttpResponse, ApiError> {
 | 
			
		||||
	let mut pool_conn = pool.acquire().await?;
 | 
			
		||||
 | 
			
		||||
	let db_controllers = DbController::get_all(&mut pool_conn).await?;
 | 
			
		||||
 | 
			
		||||
	let controllers: Vec<Controller> = convert_db_list(&mut pool_conn, db_controllers)?;
 | 
			
		||||
 | 
			
		||||
	let data = connected_controllers.lock().unwrap();
 | 
			
		||||
	println!("{:?}", *data);
 | 
			
		||||
 | 
			
		||||
	Ok(HttpResponse::Ok().json(controllers))
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -26,6 +26,5 @@ pub async fn ws_controllers(
 | 
			
		|||
		stream,
 | 
			
		||||
	)
 | 
			
		||||
	.map_err(|_| ApiError::InternalError(String::from("error starting websocket")));
 | 
			
		||||
	println!("{:?}", resp);
 | 
			
		||||
	resp
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -17,14 +17,14 @@ pub trait FromDbModel {
 | 
			
		|||
		Self: Sized;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[derive(Serialize, Deserialize, Debug)]
 | 
			
		||||
#[derive(Serialize, Deserialize, Debug, Clone)]
 | 
			
		||||
pub struct Schedule {
 | 
			
		||||
	#[serde(flatten)]
 | 
			
		||||
	pub s: DbSchedule,
 | 
			
		||||
	pub tags: Vec<String>,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[derive(Serialize, Deserialize, Debug)]
 | 
			
		||||
#[derive(Serialize, Deserialize, Debug, Clone)]
 | 
			
		||||
pub struct Relay {
 | 
			
		||||
	#[serde(flatten)]
 | 
			
		||||
	pub r: DbRelay,
 | 
			
		||||
| 
						 | 
				
			
			@ -33,7 +33,7 @@ pub struct Relay {
 | 
			
		|||
	pub tags: Vec<String>,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[derive(Serialize, Deserialize, Debug)]
 | 
			
		||||
#[derive(Serialize, Deserialize, Debug, Clone)]
 | 
			
		||||
pub struct Controller {
 | 
			
		||||
	#[serde(flatten)]
 | 
			
		||||
	pub c: DbController,
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,3 +1,7 @@
 | 
			
		|||
use log::LevelFilter;
 | 
			
		||||
use simple_logger::SimpleLogger;
 | 
			
		||||
use std::str::FromStr;
 | 
			
		||||
 | 
			
		||||
pub fn load_settings<T>(config_name: &str, env_prefix: &str) -> T
 | 
			
		||||
where
 | 
			
		||||
	for<'de> T: serde::Deserialize<'de>,
 | 
			
		||||
| 
						 | 
				
			
			@ -16,3 +20,13 @@ where
 | 
			
		|||
		.try_deserialize::<T>()
 | 
			
		||||
		.expect("Error reading settings")
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
pub fn init_logging(level: &str) {
 | 
			
		||||
	let log_level: LevelFilter = LevelFilter::from_str(level).expect("Error parsing log level.");
 | 
			
		||||
	log::trace!("Log level set to {:?}", log_level);
 | 
			
		||||
 | 
			
		||||
	SimpleLogger::new()
 | 
			
		||||
		.with_level(log_level)
 | 
			
		||||
		.init()
 | 
			
		||||
		.expect("Error initializing logger.");
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue