veilid/veilid-server/src/main.rs

98 lines
2.7 KiB
Rust
Raw Normal View History

2021-11-22 16:28:30 +00:00
#![forbid(unsafe_code)]
2021-12-08 03:09:45 +00:00
#![deny(clippy::all)]
2021-12-09 21:11:52 +00:00
#![deny(unused_must_use)]
2021-11-22 16:28:30 +00:00
mod client_api;
2022-01-15 23:24:37 +00:00
mod cmdline;
mod server;
2021-11-22 16:28:30 +00:00
mod settings;
2022-01-15 23:24:37 +00:00
#[cfg(unix)]
mod unix;
mod veilid_logs;
#[cfg(windows)]
mod windows;
use async_std::task;
use cfg_if::*;
use server::*;
2022-06-10 21:07:10 +00:00
use tracing::*;
2022-01-15 23:24:37 +00:00
use veilid_logs::*;
2021-11-22 16:28:30 +00:00
2021-11-29 01:08:50 +00:00
#[allow(clippy::all)]
2021-11-22 16:28:30 +00:00
pub mod veilid_client_capnp {
include!(concat!(env!("OUT_DIR"), "/proto/veilid_client_capnp.rs"));
}
2022-06-10 21:07:10 +00:00
#[instrument(err)]
2022-01-15 23:24:37 +00:00
fn main() -> Result<(), String> {
2022-06-08 01:31:05 +00:00
#[cfg(windows)]
let _ = ansi_term::enable_ansi_support();
2022-01-15 23:24:37 +00:00
let (settings, matches) = cmdline::process_command_line()?;
2021-11-22 16:28:30 +00:00
2022-01-15 23:24:37 +00:00
// --- Dump Config ---
if matches.occurrences_of("dump-config") != 0 {
return serde_yaml::to_writer(std::io::stdout(), &*settings.read())
.map_err(|e| e.to_string());
}
2022-01-19 02:21:11 +00:00
// --- Generate DHT Key ---
if matches.occurrences_of("generate-dht-key") != 0 {
2022-01-15 23:24:37 +00:00
let (key, secret) = veilid_core::generate_secret();
println!("Public: {}\nSecret: {}", key.encode(), secret.encode());
return Ok(());
2021-11-22 16:28:30 +00:00
}
2022-05-16 15:52:48 +00:00
// See if we're just running a quick command
let (server_mode, success, failure) = if matches.occurrences_of("set-node-id") != 0 {
(
ServerMode::ShutdownImmediate,
"Node Id and Secret set successfully",
"Failed to set Node Id and Secret",
)
} else if matches.occurrences_of("dump-txt-record") != 0 {
(ServerMode::DumpTXTRecord, "", "Failed to dump txt record")
} else {
(ServerMode::Normal, "", "")
};
// Handle non-normal server modes
if !matches!(server_mode, ServerMode::Normal) {
// Init combined console/file logger
2022-06-08 13:33:41 +00:00
let _logs = VeilidLogs::setup(settings.clone())?;
2022-05-16 15:52:48 +00:00
// run the server to set the node id and quit
2022-06-08 13:33:41 +00:00
return task::block_on(async { run_veilid_server(settings, server_mode).await })
2022-05-16 15:52:48 +00:00
.map(|v| {
println!("{}", success);
v
})
.map_err(|e| {
println!("{}", failure);
e
});
}
2022-01-15 23:24:37 +00:00
// --- Daemon Mode ----
2022-05-16 15:52:48 +00:00
if settings.read().daemon.enabled {
2022-01-15 23:24:37 +00:00
cfg_if! {
if #[cfg(windows)] {
return windows::run_service(settings, matches).map_err(|e| format!("{}", e));
} else if #[cfg(unix)] {
return unix::run_daemon(settings, matches);
}
2021-11-22 16:28:30 +00:00
}
}
2022-01-15 23:24:37 +00:00
// Init combined console/file logger
2022-06-08 13:33:41 +00:00
let _logs = VeilidLogs::setup(settings.clone())?;
2022-01-15 23:24:37 +00:00
// --- Normal Startup ---
ctrlc::set_handler(move || {
shutdown();
})
.expect("Error setting Ctrl-C handler");
// Run the server loop
2022-06-08 13:33:41 +00:00
task::block_on(async { run_veilid_server(settings, server_mode).await })
2021-11-22 16:28:30 +00:00
}