Getting Started
This guide assumes you have a basic knowledge of rust. No knowledge of thruster or the web in general is necessary!
Project Setup
Create a new binary rust project using cargo:
Open up the newly generated Cargo.toml
and add some dependencies for thruster, as well as it's preferred async runtime, tokio.
Note that version 1.3.0 is not yet released, but will be in the near future. If you're using this guide before it has been released, then replace the version = "1.3.0"
with git = "https://github.com/thruster-rs/thruster"
.
Here we've added thruster itself (along with a backend server, which we won't cover here) and tokio which is an async runtime for rust that allows thruster to run lots of things efficiently.
Now let's make a super-simple app to make sure everything is working. In src/main.rs
, change the contents to look like this:
Let's break down what we have here (the comments align with the points):
All the imports for thruster, not super important but worth noting that we're using a
TypedHyperContext
here. I'll explain why in a few points.It's often convenient to just alias our context type, because we use it a LOT (literally every middleware function.)
ServerConfig
andRequestConfig
aren't important now, but will be super important later.ServerConfig
will basically hold the shared state of the server, whileRequestConfig
will hold all of the request-specific pieces that you define. Authentication results are commonly stored in theRequestConfig
.This function is the backbone of thruster. It's important that this function be nice and efficient because of that. The
generate_context
function takes a request and turns it into aContext
that's then passed through all of the handling middleware. It also has references to the shared context and path of the request just in case it's useful for the context object. Commonly, this is how references to a database pool are passed to middleware functions.A simple middleware function that sets the body of the response to "Hello, world!". Note that this function doesn't use the
next
function. This means that even if there's other middleware after it, it won't be called. That's whatnext
is -- a convenient wrapper to call the "next" function in the chain of middleware.This is how we create an app. We have to tell the app what the server config looks like, and how to generate a context.
This is bread and butter of thruster. It's middleware chains! the
get
represents the relevant verb, and them!
macro is all of the middleware to call when the route requested.m!
takes a comma separated list of functions.Thruster is a few pieces, but its router is detached from the actual TCP handling. This means that we can use super optimized or highly featured libraries like Hyper or Actix to do heavy lifting for us! This example will use hyper, so we make a
HyperServer
and pass thatapp
to it to serve.
Now run the program!
If you check out http://localhost:4321/hello
you should see our "Hello, world!" message -- nice!
Last updated