CLI
Because curl is so passé
Awesomesauce. We're almost there to a functioning... thing? App? Yes. A functioning app.
The server is all deployed, let's build the cli. Start by making a new src/cli.rs
file with the following contents:
Now we need to tell Cargo.toml
about the new binary target, so add the following to Cargo.toml
.
Come to think of it, we'll want some command line argument parsing too, so let's throw that dependency in there now:
While we're at it, add reqwest
too. reqwest
is a http request library based on hyper
.
Great! So we'll want to basically have two commands:
Store a secret
Fetch a secret
Let's start by adding some clap
library magic. In the cli.rs
file, add this bit at the top:
This parses command line arguments and basically accepts the flags "--store", "--fetch", and "--code".
Now, the rest of this code isn't very interesting, so I'll drop it below. Basically it checks whether you have added the store or fetch arguments, and makes the request to our server accordingly. When you write this code, remember to update the SERVER_URI
variable!
Now simply run the command to store,
and use the resulting id and code to fetch it
Your results should be "super secret test message". You did it!! Congratulations!!
This is the end of the tutorial, but there is extra credit for you overachievers! You might have noticed we added an expired_at
to the secrets that we store in the database. Well, there's a reason -- to make this a bit more usable and secure, secrets should have expirations associated with them. If you feel up to it, see if you can add a bit to the query that actually checks the expiration time, and then a bit to the CLI that accepts an expiration time. If you're feeling fancy, the expiration could even parse human times (like 5 minutes, 2 days, or tomorrow!)
There are lots of other things we could do with this basic setup too -- for example, we could check the Accept
header and return a fully rendered HTML page if it's text/html
or a JSON blob if it's application/json
. Have fun!
Last updated