Who's Awesome?

You are.

Someone recently posted this on reddit, and I think that it's awesome. I challenged myself to make this into a web service in under five minutes, because let's face it -- who has time to pick a thing four times?

Want to skip all the code and just get to the result? Here's some feel good messaging.

The Setup

I'm setting up shuttle.rs because it's super fast to set up, and thruster as the web framework because I wrote it, so I better know how to use it.

mkdir pep-generator
cargo shuttle init --thruster

I'm adding a few quick deps, basically the shuttle stuff, thruster stuff, tokio, and askama for templating.

askama = "0.11.1" # For templating
chrono = "0.4.22" # For time stuff
log = "0.4.17" # For teh logz
rand = "0.8.5" # Random!
shuttle-aws-rds = { version = "0.7.2", features = ["postgres"] } # For storing data
shuttle-service = { version = "0.7.2", features = ["web-thruster"] } # For deploying easily
sqlx = { version = "0.6.2", features = ["runtime-tokio-native-tls", "postgres", "chrono", "uuid"] } # For dealing with postgres
thruster = { version = "1.3.0", features = ["hyper_server"] } # For http framework stuff
tokio = { version = "1.20.1", features = ["macros"] } # For an async runtime

The Schema

Schemas are simple, the ideas behind twitter are simple, we'll probably never need to make a change to this, so here's the schema; throw it in schema.sql.

The Code

We have the basic code in lib.rs, first let's populate the DB. If you aren't using an IDE that lets you auto import, then you should get one at this point.

I also need to add a pool for the middleware to make calls to, so I need to have a singleton on the server. So I used TypedHyperContext along with some structs to hold it. I had to delete some of the default imports that these override.

Oops -- we have to update the thruster function type signature now to include the server config too;

Okay, so we're making that DB, but we have no page except stupid "Hello, world!" I'm going to take that function and reuse it for our / route.

Okay, let's randomly grab them phrases from the database now!

Last step -- let's fill in templates/home.html.

Time to run this sucka!

This runs it locally, so let's see how it looks:

Now I'll ship it to prod, easy as setting up the shuttle project remotely:

And then deploying the code

Now, follow the link in the resulting command and get a nice lil' message to brighten your day.

All the code can be found here.

Last updated

Was this helpful?