Skip to content

Commit

Permalink
CLI can now generate spec files
Browse files Browse the repository at this point in the history
  • Loading branch information
ezrasingh committed Aug 8, 2024
1 parent f207465 commit 745b604
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 8 deletions.
8 changes: 8 additions & 0 deletions geoprox-server/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,14 @@ pub mod docs {
Router::new()
.merge(SwaggerUi::new("/swagger-ui").url("/api-spec/openapi.json", ApiDoc::openapi()))
}

pub fn openapi_spec(pretty_print: bool) -> Result<String, serde_json::Error> {
if pretty_print {
ApiDoc::openapi().to_pretty_json()
} else {
ApiDoc::openapi().to_json()
}
}
}

#[cfg(test)]
Expand Down
35 changes: 27 additions & 8 deletions geoprox/src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use clap::{Parser, Subcommand};
use clap::{ArgAction, Parser, Subcommand};
use config::{Config, ConfigError};
use geoprox_core::models::GeoShardConfig;
use geoprox_server::config::ServerConfig;
Expand All @@ -19,34 +19,53 @@ pub struct Cli {

#[derive(Subcommand)]
pub enum Commands {
/// start geoprox server
/// Start Geoprox server
Run {
/// <addr>:<port> (default 127.0.0.1:5000)
/// <addr>:<port> (defaults to 0.0.0.0:5000)
#[arg(short, long)]
bind: Option<SocketAddr>,
},

/// hash latitude/longitude into geohash
/// Hash latitude/longitude into geohash
Encode {
/// latitude
/// Latitude
#[arg(long)]
lat: f64,

/// longitude
/// Longitude
#[arg(long)]
lng: f64,

/// geohash length (default 6)
/// Geohash length (defaults to 6)
#[arg(short, long)]
depth: Option<usize>,
},

/// decode geohash into approximate longitude/latitude
/// Decode geohash into approximate longitude/latitude
Decode {
/// geohash
#[arg(short, long)]
ghash: String,
},

/// Generate an OpenAPI specification file
Spec {
/// Directory path to store the specification file (defaults to the current working directory)
#[arg(short, long)]
destination: Option<PathBuf>,

/// Name of the output file (defaults to openapi.json)
#[arg(short, long)]
filename: Option<String>,

/// Output the API specification to standard output (stdout)
#[arg(short, long, action=ArgAction::SetTrue)]
stdout: bool,

/// Format output JSON
#[arg(short, long, action=ArgAction::SetTrue)]
pretty: bool,
},
}

#[derive(Debug, Default, Deserialize)]
Expand Down
36 changes: 36 additions & 0 deletions geoprox/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::io::Write;

use geoprox_core::models::GeoShardConfig;
use geoprox_server::config::ServerConfig;

Expand Down Expand Up @@ -39,6 +41,40 @@ fn main() {
println!("latitude: {} +/- {}", position.y, lat_err);
println!("longitude:{} +/- {}", position.x, lng_err);
}
Some(cli::Commands::Spec {
destination,
filename,
stdout,
pretty,
}) => {
let spec_json = geoprox_server::api::docs::openapi_spec(*pretty).unwrap();
if *stdout {
// ? print the spec JSON to stdout, if `stdout` is `true` or `Some(true)`,
println!("{}", spec_json);
} 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);

let file_name = filename.as_deref().unwrap_or("openapi.json");
dir.join(file_name)
};
// ? create the file and write the spec JSON to it
match std::fs::File::create(&file_path) {
Ok(mut file) => {
if let Err(e) = file.write_all(spec_json.as_bytes()) {
eprintln!("Failed to write to file: {}", e);
} else {
println!("OpenAPI specification saved to: {:?}", file_path);
}
}
Err(e) => eprintln!("Failed to create file: {}", e),
}
}
}
None => {
println!("Invalid command. Please try '--help' for more information.");
}
Expand Down

0 comments on commit 745b604

Please sign in to comment.