Add print-url CLI option
This commit is contained in:
parent
5ed21dcf12
commit
36a3ab427a
|
@ -876,6 +876,12 @@ pub fn compact_poll(
|
|||
return Ok(warp::reply::json(&response).into_response());
|
||||
}
|
||||
|
||||
// Not publicly exposed.
|
||||
pub async fn get_url() -> Result<Response, Rejection> {
|
||||
let url = super::get_url();
|
||||
return Ok(warp::reply::json(&url).into_response());
|
||||
}
|
||||
|
||||
// Utilities
|
||||
|
||||
fn get_pending_tokens(
|
||||
|
|
41
src/main.rs
41
src/main.rs
|
@ -1,6 +1,7 @@
|
|||
use std::collections::HashMap;
|
||||
use std::fs;
|
||||
use std::net::{IpAddr, Ipv4Addr, SocketAddr};
|
||||
use std::sync::Mutex;
|
||||
|
||||
use futures::join;
|
||||
use log::info;
|
||||
|
@ -24,6 +25,13 @@ mod tests;
|
|||
|
||||
const LOCALHOST_PORT: u16 = 3030;
|
||||
|
||||
lazy_static::lazy_static! {
|
||||
|
||||
pub static ref USES_TLS: Mutex<bool> = Mutex::new(false);
|
||||
pub static ref PORT: Mutex<u16> = Mutex::new(0);
|
||||
pub static ref HEX_PUBLIC_KEY: Mutex<String> = Mutex::new("".to_string());
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
// Parse arguments
|
||||
|
@ -36,15 +44,20 @@ async fn main() {
|
|||
// Run in command mode
|
||||
execute_commands(opt).await;
|
||||
} else {
|
||||
// Store the port and TLS mode
|
||||
*PORT.lock().unwrap() = opt.port;
|
||||
*USES_TLS.lock().unwrap() = opt.tls;
|
||||
// Run in server mode
|
||||
logging::init(opt.log_file);
|
||||
let addr = SocketAddr::new(IpAddr::V4(opt.host), opt.port);
|
||||
let localhost = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), LOCALHOST_PORT);
|
||||
*crypto::PRIVATE_KEY_PATH.lock().unwrap() = opt.x25519_private_key;
|
||||
*crypto::PUBLIC_KEY_PATH.lock().unwrap() = opt.x25519_public_key;
|
||||
// Print the server public key
|
||||
// Print the server URL
|
||||
let hex_public_key = hex::encode(crypto::PUBLIC_KEY.as_bytes());
|
||||
info!("The public key of this server is: {}", hex_public_key);
|
||||
*HEX_PUBLIC_KEY.lock().unwrap() = hex_public_key;
|
||||
print!("Users can join rooms on this open group server using the following URL format:");
|
||||
print!("{}", get_url());
|
||||
// Create the main database
|
||||
storage::create_main_database_if_needed();
|
||||
// Create required folders
|
||||
|
@ -61,7 +74,8 @@ async fn main() {
|
|||
let private_routes = routes::create_room()
|
||||
.or(routes::delete_room())
|
||||
.or(routes::add_moderator())
|
||||
.or(routes::delete_moderator());
|
||||
.or(routes::delete_moderator())
|
||||
.or(routes::get_url());
|
||||
if opt.tls {
|
||||
info!("Running on {} with TLS.", addr);
|
||||
let serve_public_routes_future = warp::serve(public_routes)
|
||||
|
@ -126,6 +140,27 @@ async fn execute_commands(opt: options::Opt) {
|
|||
client.post(format!("{}/delete_moderator", localhost)).json(¶ms).send().await.unwrap();
|
||||
info!("Deleted moderator: {} from room with ID: {}", &args[0], &args[1]);
|
||||
}
|
||||
// Print URL
|
||||
if let Some(_) = opt.print_url {
|
||||
let response =
|
||||
client.get(format!("{}/url", localhost)).send().await.unwrap().text().await.unwrap();
|
||||
print!("Users can join rooms on this open group server using the following URL format:");
|
||||
info!("{}", response);
|
||||
}
|
||||
}
|
||||
|
||||
fn get_url() -> String {
|
||||
let uses_tls: bool = *USES_TLS.lock().unwrap();
|
||||
let port: u16 = *PORT.lock().unwrap();
|
||||
let hex_public_key: &str = &*HEX_PUBLIC_KEY.lock().unwrap();
|
||||
let protocol = if uses_tls { "https" } else { "http" };
|
||||
let is_port_implicit = (port == 80 && !uses_tls) || (port == 443 && uses_tls);
|
||||
return format!(
|
||||
"{}://[host_name_or_ip]{}/[room_id]?public_key={}",
|
||||
protocol,
|
||||
if is_port_implicit { "".to_string() } else { format!(":{}", port) },
|
||||
hex_public_key
|
||||
);
|
||||
}
|
||||
|
||||
async fn create_default_rooms() {
|
||||
|
|
|
@ -55,4 +55,8 @@ pub struct Opt {
|
|||
/// Removes moderator permission for the given public key in the room with the given ID.
|
||||
#[structopt(long = "delete-moderator")]
|
||||
pub delete_moderator: Option<Vec<String>>,
|
||||
|
||||
/// Prints the URL format users can use to join rooms on this open group server.
|
||||
#[structopt(long = "print-url")]
|
||||
pub print_url: Option<String>, // FIXME: This is just a dummy argument (the command doesn't actually need any arguments). There's probably a better way to do this.
|
||||
}
|
||||
|
|
|
@ -61,6 +61,13 @@ pub fn delete_moderator() -> impl Filter<Extract = impl warp::Reply, Error = Rej
|
|||
.and_then(handlers::delete_moderator);
|
||||
}
|
||||
|
||||
/// GET /url
|
||||
///
|
||||
/// Not publicly exposed.
|
||||
pub fn get_url() -> impl Filter<Extract = impl warp::Reply, Error = Rejection> + Clone {
|
||||
return warp::get().and(warp::path("url")).and_then(handlers::get_url);
|
||||
}
|
||||
|
||||
pub async fn root_html() -> Result<Response, Rejection> {
|
||||
let body = r#"
|
||||
<html>
|
||||
|
|
Loading…
Reference in New Issue