Parsing arguments

Since our command supports three subcommands, we have to add them to a clap::App instance:

let matches = App::new(crate_name!())
.version(crate_version!())
.author(crate_authors!())
.about(crate_description!())
.setting(AppSettings::SubcommandRequired)
.arg(
Arg::with_name("database")
.short("d")
.long("db")
.value_name("ADDR")
.help("Sets an address of db connection")
.takes_value(true),
)
.subcommand(SubCommand::with_name(CMD_ADD).about("add a session")
.arg(Arg::with_name("TOKEN")
.help("Sets the token of a user")
.required(true)
.index(1))
.arg(Arg::with_name("UID")
.help("Sets the uid of a user")
.required(true)
.index(2)))
.subcommand(SubCommand::with_name(CMD_REMOVE).about("remove a session")
.arg(Arg::with_name("TOKEN")
.help("Sets the token of a user")
.required(true)
.index(1)))
.subcommand(SubCommand::with_name(CMD_LIST).about("print list of sessions"))
.get_matches();

As in previous examples, this can also use the --database argument with a link to a Redis connection. It supports two subcommands. The add subcommand expects a session TOKEN and the UID of the user. The remove command expects a session TOKEN only to remove it from a map. The list command doesn't expect any parameters and prints a list of sessions.

Imagine the structure of data in this example as a cache for sessions that holds associations between token and uid. After authorization, we can send the token as a secure cookie and extract the user's uid for the provided token for every microservice to achieve loose coupling between microservices. We will explore this concept in detail later.

Now, we are ready to connect to Redis with r2d2::Pool.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.235.62