Skip to content

Commit

Permalink
Add clap to parse cli args. Configure server.rs to accept address and…
Browse files Browse the repository at this point in the history
… port arguments. Fixes Make port and server address configurable #23
  • Loading branch information
th3oth3rjak3 committed Mar 11, 2024
1 parent 152c602 commit f7ef519
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 8 deletions.
86 changes: 81 additions & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/web/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ tokio = { version = "*", features = ["full"] }
tower = "*"
utoipa = { workspace = true, features = ["axum_extras", "preserve_order"] }
utoipa-swagger-ui = { version = "*", features = ["axum"] }
clap = { version = "4.5.2", features = ["derive"] }

[dev-dependencies]
http-body-util = "0.1.0"
24 changes: 21 additions & 3 deletions crates/web/src/bin/server.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,35 @@
use clap::Parser;
use std::sync::Arc;
use tokio::net::TcpListener;
use web::axum::router;
use web::web_api::WebApi;

/// Command line arguments for starting the web application.
#[derive(Parser, Debug)]
#[command(version, about, long_about = None)]
struct Args {
/// The web address to host the server at with a default value of "0.0.0.0" when no input is provided.
#[arg(short, long, default_value_t = String::from("0.0.0.0"))]
address: String,

/// The port to host the server at with a default value of "8080" when no input is provided.
#[arg(short, long, default_value_t = String::from("8080"))]
port: String,
}

#[tokio::main]
async fn main() {
let cli_args: Args = Args::parse();
let web_address = format!("{}:{}", cli_args.address, cli_args.port);

println!("You can access the server for example via");
println!(
"http://localhost:8080/move?die1=3&die2=1&p24=2&p19=-5&p17=-3&p13=5&p12=-5&p8=3&p6=5&p1=-2"
"http://{}/move?die1=3&die2=1&p24=2&p19=-5&p17=-3&p13=5&p12=-5&p8=3&p6=5&p1=-2",
&web_address
);
println!("http://localhost:8080/swagger-ui");
println!("http://{}/swagger-ui", &web_address);

let listener = TcpListener::bind("0.0.0.0:8080").await.unwrap();
let listener = TcpListener::bind(web_address).await.unwrap();
let web_api = Arc::new(WebApi::try_default());
let app = router(web_api);
axum::serve(listener, app).await.unwrap();
Expand Down

0 comments on commit f7ef519

Please sign in to comment.