server: add basic config file
This commit is contained in:
@@ -19,6 +19,35 @@ async fn main() -> anyhow::Result<()> {
|
||||
.with(EnvFilter::from_default_env())
|
||||
.init();
|
||||
|
||||
let config = toml::from_str::<roowho2_lib::server::config::Config>(
|
||||
&std::fs::read_to_string("/etc/roowho2/roowho2.toml")
|
||||
.context("Failed to read configuration file /etc/roowho2/roowho2.toml")?,
|
||||
)?;
|
||||
|
||||
let mut join_set = tokio::task::JoinSet::new();
|
||||
|
||||
if config.rwhod.enable {
|
||||
tracing::info!("Starting RWHOD server");
|
||||
|
||||
join_set.spawn(rwhod_server());
|
||||
} else {
|
||||
tracing::debug!("RWHOD server is disabled in configuration");
|
||||
}
|
||||
|
||||
join_set.spawn(ctrl_c_handler());
|
||||
|
||||
join_set.join_next().await.unwrap()??;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn ctrl_c_handler() -> anyhow::Result<()> {
|
||||
tokio::signal::ctrl_c()
|
||||
.await
|
||||
.map_err(|e| anyhow::anyhow!("Failed to listen for Ctrl-C: {}", e))
|
||||
}
|
||||
|
||||
async fn rwhod_server() -> anyhow::Result<()> {
|
||||
let addr = SocketAddrV4::new(Ipv4Addr::UNSPECIFIED, RWHOD_BROADCAST_PORT);
|
||||
tracing::debug!("Binding RWHOD socket to {}", addr);
|
||||
let socket = tokio::net::UdpSocket::bind(addr)
|
||||
@@ -42,24 +71,9 @@ async fn main() -> anyhow::Result<()> {
|
||||
let client_server_task = rwhod_client_server_task(client_server_socket, status_store.clone());
|
||||
|
||||
tokio::select! {
|
||||
res = sender_task => {
|
||||
if let Err(err) = res {
|
||||
eprintln!("RWHOD sender task error: {}", err);
|
||||
}
|
||||
}
|
||||
res = receiver_task => {
|
||||
if let Err(err) = res {
|
||||
eprintln!("RWHOD receiver task error: {}", err);
|
||||
}
|
||||
}
|
||||
res = client_server_task => {
|
||||
if let Err(err) = res {
|
||||
eprintln!("RWHOD client-server task error: {}", err);
|
||||
}
|
||||
}
|
||||
_ = tokio::signal::ctrl_c() => {
|
||||
println!("Received Ctrl-C, shutting down.");
|
||||
}
|
||||
res = sender_task => res?,
|
||||
res = receiver_task => res?,
|
||||
res = client_server_task => res?,
|
||||
}
|
||||
|
||||
Ok(())
|
||||
|
||||
@@ -1 +1,2 @@
|
||||
pub mod rwhod;
|
||||
pub mod config;
|
||||
|
||||
36
src/server/config.rs
Normal file
36
src/server/config.rs
Normal file
@@ -0,0 +1,36 @@
|
||||
use std::net::SocketAddrV4;
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
pub const DEFAULT_CONFIG_PATH: &str = "/etc/roowho2/config.toml";
|
||||
|
||||
pub const DEFAULT_CLIENT_SOCKET_PATH: &str = "/run/roowho2/server_client.sock";
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
pub struct Config {
|
||||
/// Configuration for the rwhod server.
|
||||
pub rwhod: RwhodConfig,
|
||||
|
||||
/// Path to the Unix domain socket for client-server communication.
|
||||
///
|
||||
/// If left as `None`, the server expects to be served a file descriptor to the socket named 'client'.
|
||||
pub client_socket_path: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
pub struct RwhodConfig {
|
||||
/// Enable or disable the rwhod server functionality.
|
||||
pub enable: bool,
|
||||
|
||||
/// Network interfaces to listen on (e.g., ["eth0", "wlan0"]).
|
||||
///
|
||||
/// If left as `None`, the server will automatically determine relevant interfaces.
|
||||
///
|
||||
/// Note that if `broadcast_addresses` is specified, this field is ignored.
|
||||
pub interfaces: Option<Vec<String>>,
|
||||
|
||||
/// Broadcast addresses to send rwhod packets to.
|
||||
///
|
||||
/// If left as `None`, the server will automatically determine broadcast addresses for the selected interfaces.
|
||||
pub broadcast_addresses: Option<Vec<SocketAddrV4>>,
|
||||
}
|
||||
@@ -75,7 +75,7 @@ pub fn generate_rwhod_status_update() -> anyhow::Result<WhodStatusUpdate> {
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct RwhodSendTarget {
|
||||
/// Name of the network interface.
|
||||
pub name: String,
|
||||
|
||||
Reference in New Issue
Block a user