Skip to content

Commit

Permalink
Fixed issue with parsing config path
Browse files Browse the repository at this point in the history
  • Loading branch information
ezrasingh committed Aug 23, 2024
1 parent b12f64d commit b703ca9
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 53 deletions.
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/target
.env*
perf.data
flamegraph.svg
*.bin
1 change: 0 additions & 1 deletion geoprox-server/.gitignore

This file was deleted.

2 changes: 2 additions & 0 deletions geoprox-server/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ pub async fn run(server_config: ServerConfig, shard_config: GeoShardConfig) {
.init();

let socket: std::net::SocketAddr = server_config.clone().into();
dbg!("binding to:", socket);

let listener = tokio::net::TcpListener::bind(socket).await.unwrap();
println!("listening on {}", listener.local_addr().unwrap());

Expand Down
2 changes: 1 addition & 1 deletion geoprox/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ license = "MIT OR Apache-2.0"
[dependencies]
geoprox-core = { path = "../geoprox-core", version = "0.4.2" }
geoprox-server = { path = "../geoprox-server", version = "0.4.2" }
clap = { version = "4.5.4", features = ["derive", "env"] }
clap = { version = "4.5.4", features = ["derive", "env", "string"] }
serde = { version = "1.0.199", features = ["derive", "rc"] }
serde_json = "1.0.116"
config = "0.14.0"
45 changes: 10 additions & 35 deletions geoprox/src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,33 +1,32 @@
use clap::{ArgAction, Parser, Subcommand};
use config::{Config, ConfigError};
use geoprox_core::models::GeoShardConfig;
use geoprox_server::config::{ServerConfig, DEFAULT_CONFIG_PATH};
use geoprox_server::config::ServerConfig;
use serde::Deserialize;
use std::net::IpAddr;
use std::path::PathBuf;

#[derive(Parser)]
#[command(version, about, long_about = None)]
pub struct Cli {
/// Specify a config file (defaults /var/lib/geoprox/geoprox.{toml,yaml,json,ini,ron,json5})
#[arg(short, long, value_name = "CONFIG", env = "GEOPROX_CONFIG")]
config: Option<PathBuf>,

#[command(subcommand)]
command: Option<Commands>,
pub command: Option<Commands>,
}

#[derive(Subcommand)]
pub enum Commands {
/// Start Geoprox server
Run {
/// The address the server will bind to
#[arg(short, long, default_value_t = ServerConfig::DEFAULT_ADDR, env = "GEOPROX_HTTP_ADDR")]
addr: IpAddr,
#[arg(short, long, env = "GEOPROX_HTTP_ADDR")]
addr: Option<IpAddr>,

/// The port the server will listen on
#[arg(short, long, default_value_t = ServerConfig::DEFAULT_PORT, env = "GEOPROX_HTTP_PORT")]
port: u16,
#[arg(short, long, env = "GEOPROX_HTTP_PORT")]
port: Option<u16>,

/// Specify a config file
#[arg(short, long, env = "GEOPROX_CONFIG")]
config_path: Option<PathBuf>,
},

/// Hash latitude/longitude into geohash
Expand Down Expand Up @@ -77,27 +76,3 @@ pub struct GeoproxConfig {
pub server: Option<ServerConfig>,
pub shard: Option<GeoShardConfig>,
}

pub fn runtime() -> Result<(Option<Commands>, GeoproxConfig), ConfigError> {
let cli = Cli::parse();

let settings: GeoproxConfig = match cli.config.as_deref() {
Some(config_path) => Config::builder()
.add_source(config::File::from(config_path))
.build()?
.try_deserialize()?,
None => Config::builder()
.add_source(config::File::with_name(&format!(
// ? look for geoprox.{toml,yaml,json,ini,ron,json5}
// ? config under default config path
"{}/geoprox",
DEFAULT_CONFIG_PATH
)))
.build()
.unwrap_or_default()
.try_deserialize()
.unwrap_or_default(),
};

Ok((cli.command, settings))
}
49 changes: 35 additions & 14 deletions geoprox/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,42 @@
use geoprox_server::config::ServerConfig;
use clap::Parser;
use config::Config;
use std::io::Write;

use geoprox_server::config::ServerConfig;

mod cli;
use cli::{Cli, GeoproxConfig};

fn main() {
let (command, settings) = cli::runtime().unwrap();

match &command {
Some(cli::Commands::Run { addr, port }) => geoprox_server::run(
ServerConfig {
http_addr: Some(*addr),
http_port: Some(*port),
..settings.server.unwrap_or_default()
},
settings.shard.unwrap_or_default(),
),
let cli = Cli::parse();
let cwd = std::env::current_dir().unwrap();
match &cli.command {
Some(cli::Commands::Run {
addr,
port,
config_path,
}) => {
let settings: GeoproxConfig = match config_path.as_deref() {
Some(config_path) => Config::builder()
.add_source(config::File::from(config_path))
.build()
.unwrap()
.try_deserialize()
.unwrap(),
None => GeoproxConfig::default(),
};
let server_config = settings.server.unwrap_or_default();
dbg!("using settings:", &server_config);
geoprox_server::run(
ServerConfig {
http_addr: addr.or(server_config.http_addr),
http_port: port.or(server_config.http_port),
timeout: server_config.timeout,
snapshots: server_config.snapshots,
},
settings.shard.unwrap_or_default(),
)
}

Some(cli::Commands::Encode { lat, lng, depth }) => {
let ghash = geoprox_core::geohash::encode([*lng, *lat].into(), *depth).unwrap();
Expand All @@ -39,7 +61,6 @@ fn main() {
} else {
// ? combine the directory and file name into the final path
let file_path = {
let cwd = std::env::current_dir().unwrap();
let dir = destination
.as_deref() // Convert &Option<PathBuf> to Option<&Path>
.unwrap_or(&cwd);
Expand All @@ -60,7 +81,7 @@ fn main() {
}
}
None => {
println!("Invalid command. Please try '--help' for more information.");
eprintln!("Invalid command. Please try '--help' for more information.");
}
}
}

0 comments on commit b703ca9

Please sign in to comment.