fabaccess-bffh/bin/bffhd/main.rs

155 lines
5.3 KiB
Rust
Raw Normal View History

2021-10-19 11:16:24 +02:00
use std::{
io,
io::Write,
path::PathBuf,
};
use clap::{App, Arg, crate_version, crate_description, crate_name};
use std::str::FromStr;
2021-10-20 12:58:05 +02:00
use diflouroborane::{config, error::Error};
2021-10-20 18:37:50 +02:00
use diflouroborane::db::{Databases, Dump};
2021-10-20 12:58:05 +02:00
use std::net::ToSocketAddrs;
2021-10-19 11:16:24 +02:00
2021-10-27 14:49:45 +02:00
fn main() -> Result<(), Error> {
tracing_subscriber::fmt::init();
2021-10-19 11:16:24 +02:00
// Argument parsing
// values for the name, description and version are pulled from `Cargo.toml`.
let matches = App::new(crate_name!())
.about(crate_description!())
.version(crate_version!())
.arg(Arg::with_name("config")
.help("Path to the config file to use")
.long("config")
.short("c")
.takes_value(true
))
.arg(Arg::with_name("print default")
.help("Print a default config to stdout instead of running")
.long("print-default")
)
.arg(Arg::with_name("check config")
.help("Check config for validity")
.long("check")
)
.arg(Arg::with_name("dump")
.help("Dump all internal databases")
.long("dump")
.conflicts_with("load")
)
.arg(Arg::with_name("load")
.help("Load values into the internal databases")
.long("load")
.conflicts_with("dump")
)
.get_matches();
// Check for the --print-default option first because we don't need to do anything else in that
// case.
if matches.is_present("print default") {
let config = config::Config::default();
let encoded = serde_dhall::serialize(&config).to_string().unwrap();
// Direct writing to fd 1 is faster but also prevents any print-formatting that could
// invalidate the generated TOML
let stdout = io::stdout();
let mut handle = stdout.lock();
handle.write_all(&encoded.as_bytes()).unwrap();
// Early return to exit.
return Ok(());
} else if matches.is_present("check config") {
let configpath = matches.value_of("config").unwrap_or("/etc/diflouroborane.dhall");
match config::read(&PathBuf::from_str(configpath).unwrap()) {
2021-10-20 12:58:05 +02:00
Ok(_) => {
2021-10-19 11:16:24 +02:00
//TODO: print a normalized version of the supplied config
println!("config is valid");
std::process::exit(0);
}
Err(e) => {
eprintln!("{}", e);
std::process::exit(-1);
}
}
}
// If no `config` option is given use a preset default.
let configpath = matches.value_of("config").unwrap_or("/etc/diflouroborane.dhall");
let config = config::read(&PathBuf::from_str(configpath).unwrap())?;
2021-10-20 12:58:05 +02:00
println!("{:#?}", config);
let mut sockaddrs = Vec::new();
for listen in config.listens {
match listen.to_socket_addrs() {
Ok(addrs) => {
sockaddrs.extend(addrs)
},
Err(e) => {
tracing::error!("Invalid listen \"{}\" {}", listen, e);
}
}
}
println!("Final listens: {:?}", sockaddrs);
2021-10-19 11:16:24 +02:00
2021-10-20 18:37:50 +02:00
let dbs = Databases::create(config.db_path)?;
2021-10-19 11:16:24 +02:00
if matches.is_present("dump") {
2021-10-20 18:37:50 +02:00
let dump = Dump::new(&dbs)?;
let encoded = serde_json::to_vec(&dump).unwrap();
2021-10-19 11:16:24 +02:00
2021-10-20 18:37:50 +02:00
// Direct writing to fd 1 is faster but also prevents any print-formatting that could
// invalidate the generated TOML
let stdout = io::stdout();
let mut handle = stdout.lock();
handle.write_all(&encoded).unwrap();
}
/*
} else if matches.is_present("load") {
let db = db::Databases::new(&log, &config)?;
let mut dir = PathBuf::from(matches.value_of_os("load").unwrap());
dir.push("users.toml");
let map = db::user::load_file(&dir)?;
for (uid,user) in map.iter() {
db.userdb.put_user(uid, user)?;
}
tracing::debug!("Loaded users: {:?}", map);
dir.pop();
2021-10-19 11:16:24 +02:00
2021-10-20 18:37:50 +02:00
Ok(())
} else {
let ex = smol::Executor::new();
let db = db::Databases::new(&log, &config)?;
2021-10-19 11:16:24 +02:00
2021-10-20 18:37:50 +02:00
let machines = machine::load(&config)?;
let (actor_map, actors) = actor::load(&log, &config)?;
let (init_map, initiators) = initiator::load(&log, &config, db.userdb.clone(), db.access.clone())?;
2021-10-19 11:16:24 +02:00
2021-10-20 18:37:50 +02:00
let mut network = network::Network::new(machines, actor_map, init_map);
2021-10-19 11:16:24 +02:00
2021-10-20 18:37:50 +02:00
for (a,b) in config.actor_connections.iter() {
if let Err(e) = network.connect_actor(a,b) {
tracing::error!("{}", e);
}
tracing::info!("[Actor] Connected {} to {}", a, b);
2021-10-19 11:16:24 +02:00
}
2021-10-20 18:37:50 +02:00
for (a,b) in config.init_connections.iter() {
if let Err(e) = network.connect_init(a,b) {
tracing::error!("{}", e);
}
tracing::info!("[Initi] Connected {} to {}", a, b);
2021-10-19 11:16:24 +02:00
}
2021-10-20 18:37:50 +02:00
for actor in actors.into_iter() {
ex.spawn(actor).detach();
}
for init in initiators.into_iter() {
ex.spawn(init).detach();
}
2021-10-19 11:16:24 +02:00
2021-10-20 18:37:50 +02:00
server::serve_api_connections(log.clone(), config, db, network, ex)
}
*/
2021-10-20 12:58:05 +02:00
Ok(())
}