2021-12-11 22:43:41 -05:00
|
|
|
//! Server process
|
2023-01-05 08:47:34 -05:00
|
|
|
use clap::Parser;
|
2023-01-22 10:49:49 -05:00
|
|
|
use nostr_rs_relay::cli::CLIArgs;
|
2021-12-29 23:13:02 -05:00
|
|
|
use nostr_rs_relay::config;
|
2022-09-06 06:56:04 -04:00
|
|
|
use nostr_rs_relay::server::start_server;
|
2022-09-06 07:12:07 -04:00
|
|
|
use std::sync::mpsc as syncmpsc;
|
|
|
|
use std::sync::mpsc::{Receiver as MpscReceiver, Sender as MpscSender};
|
2022-09-06 06:56:04 -04:00
|
|
|
use std::thread;
|
2022-09-28 08:19:59 -04:00
|
|
|
use tracing::info;
|
2023-01-05 08:47:34 -05:00
|
|
|
use console_subscriber::ConsoleLayer;
|
2022-01-05 17:41:12 -05:00
|
|
|
|
2021-12-05 17:53:26 -05:00
|
|
|
/// Start running a Nostr relay server.
|
2022-09-24 09:39:41 -04:00
|
|
|
fn main() {
|
2023-02-06 23:35:08 -05:00
|
|
|
let args = CLIArgs::parse();
|
|
|
|
|
|
|
|
// get config file name from args
|
|
|
|
let config_file_arg = args.config;
|
|
|
|
|
|
|
|
// configure settings from the config file (defaults to config.toml)
|
|
|
|
// replace default settings with those read from the config file
|
|
|
|
let mut settings = config::Settings::new(&config_file_arg);
|
2022-09-11 13:44:45 -04:00
|
|
|
|
2023-01-14 10:47:01 -05:00
|
|
|
// setup tracing
|
2022-09-11 13:44:45 -04:00
|
|
|
if settings.diagnostics.tracing {
|
|
|
|
// enable tracing with tokio-console
|
|
|
|
ConsoleLayer::builder().with_default_env().init();
|
2023-01-14 10:47:01 -05:00
|
|
|
} else {
|
2023-01-22 11:06:44 -05:00
|
|
|
// standard logging
|
|
|
|
tracing_subscriber::fmt::try_init().unwrap();
|
2022-09-11 13:44:45 -04:00
|
|
|
}
|
2023-01-14 10:47:01 -05:00
|
|
|
info!("Starting up from main");
|
|
|
|
|
|
|
|
// get database directory from args
|
2023-01-16 18:21:12 -05:00
|
|
|
let db_dir_arg = args.db;
|
2023-01-14 10:47:01 -05:00
|
|
|
|
2023-01-16 18:21:12 -05:00
|
|
|
// update with database location from args, if provided
|
|
|
|
if let Some(db_dir) = db_dir_arg {
|
2023-01-05 08:47:34 -05:00
|
|
|
settings.database.data_directory = db_dir;
|
2021-12-29 23:13:02 -05:00
|
|
|
}
|
2023-01-22 10:49:49 -05:00
|
|
|
// we should have a 'control plane' channel to monitor and bump
|
|
|
|
// the server. this will let us do stuff like clear the database,
|
|
|
|
// shutdown, etc.; for now all this does is initiate shutdown if
|
|
|
|
// `()` is sent. This will change in the future, this is just a
|
|
|
|
// stopgap to shutdown the relay when it is used as a library.
|
2022-09-06 07:12:07 -04:00
|
|
|
let (_, ctrl_rx): (MpscSender<()>, MpscReceiver<()>) = syncmpsc::channel();
|
2022-09-06 06:56:04 -04:00
|
|
|
// run this in a new thread
|
2023-01-22 10:49:49 -05:00
|
|
|
let handle = thread::spawn(move || {
|
|
|
|
let _svr = start_server(&settings, ctrl_rx);
|
2021-12-05 17:53:26 -05:00
|
|
|
});
|
2022-09-06 06:56:04 -04:00
|
|
|
// block on nostr thread to finish.
|
|
|
|
handle.join().unwrap();
|
2021-12-05 17:53:26 -05:00
|
|
|
}
|