Web service with Rust, Rocket and Diesel

A hands-on walk-trough on how to create a REST-API with Rust, Rocket and Diesel

Marco Amann
Feb 14 · 12 min read
Image for post
Image for post
Rocket, ready to launch. Photo by Felipe Simo on Unsplash

What and why

Creating the API

Ecosystem overview

Starting the Rocket

Rocket is a web framework for Rust that makes it simple to write fast, secure web applications without sacrificing flexibility, usability, or type safety.

[dependencies.rocket]
version = "0.4.2"
features = ["private-cookies"]
[dependencies.rocket_contrib]
default-features = false
features = ["json", "diesel_postgres_pool", "serve"]
#[get("/hello/<name>")]
fn hello(name: String) -> String {
format!("hello {}", name)
}
#[get(“/whatever/<id>”)]
fn super_secure_function(id: i32, user: AuthUser) -> ... {
user.do_something();
...
}
#[post("/stay", data = "<new_stay>")]
fn stay_new(new_stay: Json<NewStay>, con: DbCon) -> ... {

Connecting the Database

Image for post
Image for post
A rusty barrel full of diesel? (also resembling the shape of a DB in a flowchart?) Photo by Aleks Dorohovich on Unsplash

Diesel basics

rocket::ignite()
.attach(DbCon::fairing())
...
#[get(“/stay/<id>”)]
fn stay_get_by_id(con: DbCon, id: i32) -> ...{

Rocket-Internals

Image for post
Image for post
Photo by Leonel Fernandez on Unsplash

Shortcomings

Conclusion

Digital Frontiers — Das Blog

Dies ist das Blog der Digital Frontiers GmbH & Co.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store