Joining Forces: An Integrated Rust Web Server

James Bowen
5 min readAug 10, 2020

We’ve now explored a couple different libraries for some production tasks in Rust. A couple weeks ago, we used Diesel to create an ORM for some database types. And then last week, we used Rocketto make a basic web server to respond to basic requests. This week, we’ll put these two ideas together! We’ll use some more advanced functionality from Rocket to make some CRUD endpoints for our database type. Take a look at the code on Github here!

If you’ve never written any Rust, you should start with the basics though! Take a look at our Rust Beginners Series!

Database State and Instances

Our first order of business is connecting to the database from our handler functions. There are some direct integrations you can check out between Rocket, Diesel, and other libraries. These can provide clever ways to add a connection argument to any handler.

But for now we’re going to keep things simple. We’ll re-generate the PgConnection within each endpoint. We'll maintain a "stateful" connection string to ensure they all use the same database.

Our Rocket server can “manage” different state elements. Suppose we have a function that gives us our database string. We can pass that to our server at initialization time.

fn local_conn_string() ->

--

--