nostr-rs-relay

My dev fork of nostr-rs-relay
git clone git://jb55.com/nostr-rs-relay
Log | Files | Refs | README | LICENSE

main.rs (1719B)


      1 //! Server process
      2 
      3 use nostr_rs_relay::config;
      4 use nostr_rs_relay::server::start_server;
      5 use std::env;
      6 use std::sync::mpsc as syncmpsc;
      7 use std::sync::mpsc::{Receiver as MpscReceiver, Sender as MpscSender};
      8 use std::thread;
      9 use tracing::info;
     10 
     11 use console_subscriber::ConsoleLayer;
     12 
     13 /// Return a requested DB name from command line arguments.
     14 fn db_from_args(args: &[String]) -> Option<String> {
     15     if args.len() == 3 && args.get(1) == Some(&"--db".to_owned()) {
     16         return args.get(2).map(std::clone::Clone::clone);
     17     }
     18     None
     19 }
     20 
     21 /// Start running a Nostr relay server.
     22 fn main() {
     23     // setup tracing
     24     let _trace_sub = tracing_subscriber::fmt::try_init();
     25     info!("Starting up from main");
     26     // get database directory from args
     27     let args: Vec<String> = env::args().collect();
     28     let db_dir: Option<String> = db_from_args(&args);
     29     // configure settings from config.toml
     30     // replace default settings with those read from config.toml
     31     let mut settings = config::Settings::new();
     32 
     33     if settings.diagnostics.tracing {
     34         // enable tracing with tokio-console
     35         ConsoleLayer::builder().with_default_env().init();
     36     }
     37     // update with database location
     38     if let Some(db) = db_dir {
     39         settings.database.data_directory = db;
     40     }
     41 
     42     let (_, ctrl_rx): (MpscSender<()>, MpscReceiver<()>) = syncmpsc::channel();
     43     // run this in a new thread
     44     let handle = thread::spawn(|| {
     45         // we should have a 'control plane' channel to monitor and bump the server.
     46         // this will let us do stuff like clear the database, shutdown, etc.
     47         let _svr = start_server(settings, ctrl_rx);
     48     });
     49     // block on nostr thread to finish.
     50     handle.join().unwrap();
     51 }