2022-09-17 17:02:57 -04:00
|
|
|
use anyhow::{anyhow, Result};
|
2022-09-17 13:37:49 -04:00
|
|
|
use nostr_rs_relay::config;
|
|
|
|
use nostr_rs_relay::server::start_server;
|
2022-09-17 17:02:57 -04:00
|
|
|
//use http::{Request, Response};
|
|
|
|
use hyper::{Client, StatusCode, Uri};
|
2022-09-17 15:36:05 -04:00
|
|
|
use std::net::TcpListener;
|
2022-09-28 08:55:06 -04:00
|
|
|
use std::sync::atomic::{AtomicU16, Ordering};
|
2022-09-17 13:37:49 -04:00
|
|
|
use std::sync::mpsc as syncmpsc;
|
|
|
|
use std::sync::mpsc::{Receiver as MpscReceiver, Sender as MpscSender};
|
|
|
|
use std::thread;
|
|
|
|
use std::thread::JoinHandle;
|
2022-09-17 17:02:57 -04:00
|
|
|
use std::time::Duration;
|
2022-09-28 08:19:59 -04:00
|
|
|
use tracing::{debug, info};
|
2022-09-17 13:37:49 -04:00
|
|
|
|
|
|
|
pub struct Relay {
|
2022-09-17 15:36:05 -04:00
|
|
|
pub port: u16,
|
2022-09-17 13:37:49 -04:00
|
|
|
pub handle: JoinHandle<()>,
|
|
|
|
pub shutdown_tx: MpscSender<()>,
|
|
|
|
}
|
|
|
|
|
2022-09-17 15:36:05 -04:00
|
|
|
pub fn start_relay() -> Result<Relay> {
|
2022-09-28 08:19:59 -04:00
|
|
|
// setup tracing
|
|
|
|
let _trace_sub = tracing_subscriber::fmt::try_init();
|
2022-09-28 08:55:06 -04:00
|
|
|
info!("Starting a new relay");
|
2022-09-17 13:37:49 -04:00
|
|
|
// replace default settings
|
|
|
|
let mut settings = config::Settings::default();
|
2022-09-17 15:36:05 -04:00
|
|
|
// identify open port
|
2022-09-17 17:02:57 -04:00
|
|
|
info!("Checking for address...");
|
2022-09-17 15:36:05 -04:00
|
|
|
let port = get_available_port().unwrap();
|
2022-09-17 17:02:57 -04:00
|
|
|
info!("Found open port: {}", port);
|
2022-09-17 15:36:05 -04:00
|
|
|
// bind to local interface only
|
|
|
|
settings.network.address = "127.0.0.1".to_owned();
|
2022-09-17 13:37:49 -04:00
|
|
|
settings.network.port = port;
|
2022-09-17 15:36:05 -04:00
|
|
|
// create an in-memory DB with multiple readers
|
|
|
|
settings.database.in_memory = true;
|
|
|
|
settings.database.min_conn = 4;
|
|
|
|
settings.database.max_conn = 8;
|
2022-09-17 13:37:49 -04:00
|
|
|
let (shutdown_tx, shutdown_rx): (MpscSender<()>, MpscReceiver<()>) = syncmpsc::channel();
|
|
|
|
let handle = thread::spawn(|| {
|
2022-09-17 17:02:57 -04:00
|
|
|
// server will block the thread it is run on.
|
2022-09-17 13:37:49 -04:00
|
|
|
let _ = start_server(settings, shutdown_rx);
|
|
|
|
});
|
2022-09-17 17:02:57 -04:00
|
|
|
// how do we know the relay has finished starting up?
|
2022-09-24 09:30:22 -04:00
|
|
|
Ok(Relay {
|
2022-09-17 15:36:05 -04:00
|
|
|
port,
|
2022-09-17 13:37:49 -04:00
|
|
|
handle,
|
|
|
|
shutdown_tx,
|
2022-09-24 09:30:22 -04:00
|
|
|
})
|
2022-09-17 15:36:05 -04:00
|
|
|
}
|
|
|
|
|
2022-09-17 17:02:57 -04:00
|
|
|
// check if the server is healthy via HTTP request
|
|
|
|
async fn server_ready(relay: &Relay) -> Result<bool> {
|
2022-09-24 09:30:22 -04:00
|
|
|
let uri: String = format!("http://127.0.0.1:{}/", relay.port);
|
2022-09-17 17:02:57 -04:00
|
|
|
let client = Client::new();
|
|
|
|
let uri: Uri = uri.parse().unwrap();
|
|
|
|
let res = client.get(uri).await?;
|
|
|
|
Ok(res.status() == StatusCode::OK)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn wait_for_healthy_relay(relay: &Relay) -> Result<()> {
|
|
|
|
// TODO: maximum time to wait for server to become healthy.
|
|
|
|
// give it a little time to start up before we start polling
|
|
|
|
tokio::time::sleep(Duration::from_millis(10)).await;
|
|
|
|
loop {
|
2022-09-24 09:30:22 -04:00
|
|
|
let server_check = server_ready(relay).await;
|
2022-09-17 17:02:57 -04:00
|
|
|
match server_check {
|
|
|
|
Ok(true) => {
|
|
|
|
// server responded with 200-OK.
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
Ok(false) => {
|
|
|
|
// server responded with an error, we're done.
|
|
|
|
return Err(anyhow!("Got non-200-OK from relay"));
|
|
|
|
}
|
|
|
|
Err(_) => {
|
|
|
|
// server is not yet ready, probably connection refused...
|
2022-09-28 08:19:59 -04:00
|
|
|
debug!("Relay not ready, will try again...");
|
2022-09-17 17:02:57 -04:00
|
|
|
tokio::time::sleep(Duration::from_millis(10)).await;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
info!("relay is ready");
|
|
|
|
Ok(())
|
|
|
|
// simple message sent to web browsers
|
|
|
|
//let mut request = Request::builder()
|
|
|
|
// .uri("https://www.rust-lang.org/")
|
|
|
|
// .header("User-Agent", "my-awesome-agent/1.0");
|
|
|
|
}
|
|
|
|
|
2022-09-17 15:36:05 -04:00
|
|
|
// from https://elliotekj.com/posts/2017/07/25/find-available-tcp-port-rust/
|
2022-09-28 08:55:06 -04:00
|
|
|
// This needed some modification; if multiple tasks all ask for open ports, they will tend to get the same one.
|
|
|
|
// instead we should try to try these incrementally/globally.
|
|
|
|
|
|
|
|
static PORT_COUNTER: AtomicU16 = AtomicU16::new(4030);
|
|
|
|
|
2022-09-17 15:36:05 -04:00
|
|
|
fn get_available_port() -> Option<u16> {
|
2022-09-28 08:55:06 -04:00
|
|
|
let startsearch = PORT_COUNTER.fetch_add(10, Ordering::SeqCst);
|
|
|
|
if startsearch >= 20000 {
|
|
|
|
// wrap around
|
|
|
|
PORT_COUNTER.store(4030, Ordering::Relaxed);
|
|
|
|
}
|
|
|
|
(startsearch..20000).find(|port| port_is_available(*port))
|
2022-09-17 15:36:05 -04:00
|
|
|
}
|
2022-09-17 17:02:57 -04:00
|
|
|
pub fn port_is_available(port: u16) -> bool {
|
2022-09-28 08:55:06 -04:00
|
|
|
info!("checking on port {}", port);
|
2022-09-17 15:36:05 -04:00
|
|
|
match TcpListener::bind(("127.0.0.1", port)) {
|
|
|
|
Ok(_) => true,
|
|
|
|
Err(_) => false,
|
|
|
|
}
|
2022-09-17 13:37:49 -04:00
|
|
|
}
|