Unverified Commit f312de34 authored by Matteo Bigoi's avatar Matteo Bigoi Committed by GitHub
Browse files

Add Pokemon service cmdline arguments using clap. (#1371)



* Add command line arguments for server and port. Default the bind address to localhost
* Update rust-runtime/aws-smithy-http-server/examples/pokemon_service/src/main.rs
* Remove tracing::instrument as we should create the spans inside the code generated code

Co-authored-by: default avatardavid-perez <d@vidp.dev>
parent ae9a5ed5
Loading
Loading
Loading
Loading
+4 −1
Original line number Diff line number Diff line
@@ -3,12 +3,15 @@ name = "pokemon_service"
version = "0.1.0"
edition = "2021"
publish = false
authors = ["Smithy-rs Server Team <smithy-rs-server@amazon.com>"]
description = "A smithy Rust service to retrieve information about Pokémon."

[dependencies]
clap = { version = "3", features = ["derive"] }
hyper = {version = "0.14", features = ["server"] }
tokio = "1"
tower = "0.4"
tower-http = { version = "0.2", features = ["trace"] }
tower-http = { version = "0.3", features = ["trace"] }
tracing = "0.1"
tracing-subscriber = { version = "0.3", features = ["env-filter"] }

+18 −2
Original line number Diff line number Diff line
@@ -4,16 +4,29 @@
 */

// This program is exported as a binary named `pokemon_service`.
use std::sync::Arc;
use std::{net::SocketAddr, sync::Arc};

use aws_smithy_http_server::{AddExtensionLayer, Router};
use clap::Parser;
use pokemon_service::{empty_operation, get_pokemon_species, get_server_statistics, setup_tracing, State};
use pokemon_service_sdk::operation_registry::OperationRegistryBuilder;
use tower::ServiceBuilder;
use tower_http::trace::TraceLayer;

#[derive(Parser, Debug)]
#[clap(author, version, about, long_about = None)]
struct Args {
    /// Hyper server bind address.
    #[clap(short, long, default_value = "127.0.0.1")]
    address: String,
    /// Hyper server bind port.
    #[clap(short, long, default_value = "13734")]
    port: u16,
}

#[tokio::main]
pub async fn main() {
    let args = Args::parse();
    setup_tracing();
    let app: Router = OperationRegistryBuilder::default()
        // Build a registry containing implementations to all the operations in the service. These
@@ -37,7 +50,10 @@ pub async fn main() {
    );

    // Start the [`hyper::Server`].
    let server = hyper::Server::bind(&"0.0.0.0:13734".parse().unwrap()).serve(app.into_make_service());
    let bind: SocketAddr = format!("{}:{}", args.address, args.port)
        .parse()
        .expect("unable to parse the server bind address and port");
    let server = hyper::Server::bind(&bind).serve(app.into_make_service());

    // Run forever-ish...
    if let Err(err) = server.await {