2021-12-11 22:43:41 -05:00
|
|
|
//! Server process
|
2021-12-05 17:53:26 -05:00
|
|
|
use log::*;
|
2021-12-29 23:13:02 -05:00
|
|
|
use nostr_rs_relay::config;
|
2021-12-05 17:53:26 -05:00
|
|
|
use nostr_rs_relay::error::{Error, Result};
|
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 06:56:04 -04:00
|
|
|
use std::thread;
|
2021-12-05 17:53:26 -05:00
|
|
|
|
2022-01-05 17:41:12 -05:00
|
|
|
/// Return a requested DB name from command line arguments.
|
2021-12-31 12:51:57 -05:00
|
|
|
fn db_from_args(args: Vec<String>) -> Option<String> {
|
2021-12-31 16:19:35 -05:00
|
|
|
if args.len() == 3 && args.get(1) == Some(&"--db".to_owned()) {
|
|
|
|
return args.get(2).map(|x| x.to_owned());
|
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.
|
|
|
|
fn main() -> Result<(), Error> {
|
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");
|
2021-12-31 12:51:57 -05:00
|
|
|
// get database directory from args
|
|
|
|
let args: Vec<String> = env::args().collect();
|
|
|
|
let db_dir: Option<String> = db_from_args(args);
|
2021-12-29 23:13:02 -05:00
|
|
|
{
|
2021-12-29 23:47:31 -05:00
|
|
|
let mut settings = config::SETTINGS.write().unwrap();
|
2021-12-29 23:13:02 -05:00
|
|
|
// replace default settings with those read from config.toml
|
2021-12-31 12:51:57 -05:00
|
|
|
let mut c = config::Settings::new();
|
|
|
|
// update with database location
|
|
|
|
if let Some(db) = db_dir {
|
2021-12-31 16:19:35 -05:00
|
|
|
c.database.data_directory = db;
|
2021-12-31 12:51:57 -05:00
|
|
|
}
|
2021-12-29 23:13:02 -05:00
|
|
|
*settings = c;
|
|
|
|
}
|
2022-09-06 06:56:04 -04:00
|
|
|
// run this in a new thread
|
|
|
|
let handle = thread::spawn(|| {
|
|
|
|
let _ = start_server();
|
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
|
|
|
Ok(())
|
|
|
|
}
|