2021-12-11 22:43:41 -05:00
|
|
|
//! Server process
|
2022-09-24 09:30:22 -04:00
|
|
|
use log::info;
|
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;
|
2021-12-31 12:51:57 -05:00
|
|
|
use std::env;
|
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;
|
2021-12-05 17:53:26 -05:00
|
|
|
|
2022-09-11 13:44:45 -04:00
|
|
|
use console_subscriber::ConsoleLayer;
|
|
|
|
|
2022-01-05 17:41:12 -05:00
|
|
|
/// Return a requested DB name from command line arguments.
|
2022-09-24 20:28:02 -04:00
|
|
|
fn db_from_args(args: &[String]) -> Option<String> {
|
2021-12-31 16:19:35 -05:00
|
|
|
if args.len() == 3 && args.get(1) == Some(&"--db".to_owned()) {
|
2022-09-24 09:30:22 -04:00
|
|
|
return args.get(2).map(std::clone::Clone::clone);
|
2021-12-31 12:51:57 -05:00
|
|
|
}
|
|
|
|
None
|
|
|
|
}
|
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() {
|
2021-12-29 23:13:02 -05:00
|
|
|
// setup logger
|
2021-12-05 17:53:26 -05:00
|
|
|
let _ = env_logger::try_init();
|
2022-09-06 06:56:04 -04:00
|
|
|
info!("Starting up from main");
|
2022-09-06 07:12:07 -04:00
|
|
|
|
2021-12-31 12:51:57 -05:00
|
|
|
// get database directory from args
|
|
|
|
let args: Vec<String> = env::args().collect();
|
2022-09-24 20:28:02 -04:00
|
|
|
let db_dir: Option<String> = db_from_args(&args);
|
2022-09-06 07:12:07 -04:00
|
|
|
// configure settings from config.toml
|
|
|
|
// replace default settings with those read from config.toml
|
|
|
|
let mut settings = config::Settings::new();
|
2022-09-11 13:44:45 -04:00
|
|
|
|
|
|
|
if settings.diagnostics.tracing {
|
|
|
|
// enable tracing with tokio-console
|
|
|
|
ConsoleLayer::builder().with_default_env().init();
|
|
|
|
}
|
|
|
|
|
2022-09-06 07:12:07 -04:00
|
|
|
// update with database location
|
|
|
|
if let Some(db) = db_dir {
|
|
|
|
settings.database.data_directory = db;
|
2021-12-29 23:13:02 -05:00
|
|
|
}
|
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
|
|
|
|
let handle = thread::spawn(|| {
|
2022-09-06 07:12:07 -04: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.
|
2022-09-24 20:28:02 -04:00
|
|
|
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
|
|
|
}
|