From cc2a453c97f9a1c05152dc5fa31a33cab5ada759 Mon Sep 17 00:00:00 2001 From: EliasSchriefer Date: Sat, 23 Oct 2021 17:39:05 +0200 Subject: [PATCH] Add config/options for IP and port --- src/cli.rs | 15 +++++++++++++-- src/config.rs | 9 ++++++++- src/main.rs | 25 ++++++++++++++++++++++++- src/net.rs | 13 ++++--------- 4 files changed, 49 insertions(+), 13 deletions(-) diff --git a/src/cli.rs b/src/cli.rs index c853a90..6dcb5cb 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -1,10 +1,13 @@ use structopt::StructOpt; -use std::path::PathBuf; +use std::{ + net::IpAddr, + path::PathBuf, +}; #[derive(Debug, StructOpt)] pub struct Options { /// Path to config - #[structopt(short, long, value_name = "path", default_value = "/etc/noise-server/config.toml")] + #[structopt(short, long, value_name = "path", default_value = crate::DEFAULT_CONFIG_PATH)] pub config: PathBuf, /// Don't use config @@ -18,4 +21,12 @@ pub struct Options { /// Prints default config #[structopt(long)] pub generate_config: bool, + + /// IP address to bind to + #[structopt(short = "a", long, value_name = "ip", default_value = crate::DEFAULT_IP_ADDRESS_STR)] + pub ip_address: IpAddr, + + /// Port to bind to + #[structopt(short, long, value_name = "port", default_value = crate::DEFAULT_PORT_STR)] + pub port: u16, } \ No newline at end of file diff --git a/src/config.rs b/src/config.rs index d378d44..6be34ad 100644 --- a/src/config.rs +++ b/src/config.rs @@ -2,7 +2,10 @@ use serde::{ Deserialize, Serialize, }; -use std::path::PathBuf; +use std::{ + net::IpAddr, + path::PathBuf, +}; use tokio::{ fs::File, io::AsyncReadExt, @@ -13,6 +16,8 @@ use crate::cli::Options; #[serde(default, rename_all = "kebab-case")] pub struct Config { pub database_path: PathBuf, + pub ip_address: IpAddr, + pub port: u16, } impl Config { @@ -38,6 +43,8 @@ impl Default for Config { fn default() -> Self { Config { database_path: crate::DEFAULT_DATABASE_PATH.into(), + ip_address: crate::DEFAULT_IP_ADDRESS, + port: crate::DEFAULT_PORT, } } } \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index acfde77..5401765 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,6 +2,10 @@ use structopt::StructOpt; use std::{ boxed::Box, error::Error, + net::{ + IpAddr, + Ipv4Addr, + }, }; use crate::{cli::Options, config::Config}; @@ -12,7 +16,18 @@ mod net; mod sqlite; mod graphql; +const DEFAULT_CONFIG_PATH: &'static str = "/etc/noise-server/config.toml"; const DEFAULT_DATABASE_PATH: &'static str = "/var/lib/noise-server/noise-server.sqlite"; +const DEFAULT_IP_ADDRESS: IpAddr = IpAddr::V4(Ipv4Addr::UNSPECIFIED); +const DEFAULT_IP_ADDRESS_STR: &'static str = "0.0.0.0"; +#[cfg(debug_assertions)] +const DEFAULT_PORT: u16 = 8080; +#[cfg(debug_assertions)] +const DEFAULT_PORT_STR: &'static str = "8080"; +#[cfg(not(debug_assertions))] +const DEFAULT_PORT: u16 = 80; +#[cfg(not(debug_assertions))] +const DEFAULT_PORT_STR: &'static str = "80"; #[tokio::main] async fn main() -> Result<(), Box> { @@ -30,6 +45,14 @@ async fn main() -> Result<(), Box> { config.database_path = options.database_path.clone(); } + if clap_matches.occurrences_of("ip-address") > 0 { + config.ip_address = options.ip_address; + } + + if clap_matches.occurrences_of("port") > 0 { + config.port = options.port; + } + dbg!(&config); #[cfg(feature = "sqlite")] @@ -38,7 +61,7 @@ async fn main() -> Result<(), Box> { #[cfg(feature = "sqlite")] sqlite::load_template(&sqlite_connection).await?; - net::web(sqlite_connection).await; + net::web(config.ip_address, config.port, sqlite_connection).await; Ok(()) } \ No newline at end of file diff --git a/src/net.rs b/src/net.rs index ba26dac..026705d 100644 --- a/src/net.rs +++ b/src/net.rs @@ -1,16 +1,13 @@ +use std::net::IpAddr; use sqlx::SqlitePool; -use warp::{ - Filter, -}; +use warp::Filter; use crate::graphql::{ Context, schema, }; - -pub async fn web(db: SqlitePool){ - +pub async fn web(ip_address: IpAddr, port: u16, db: SqlitePool) { std::env::set_var("RUST_LOG", "warp_async"); let state = warp::any().map(move || Context { db: db.clone() }); let graphql_filter = juniper_warp::make_graphql_filter(schema(), state.boxed()); @@ -20,7 +17,5 @@ pub async fn web(db: SqlitePool){ .and(warp::path("graphiql")) .and(juniper_warp::graphiql_filter("/graphql", None)) .or(warp::path("graphql").and(graphql_filter)), - ) - .run(([0, 0, 0, 0], 8080)) - .await; + ).run((ip_address, port)).await; } \ No newline at end of file