2023-04-24
3968
#rust
Mario Zupan
17686
Apr 24, 2023 ⋅ 14 min read

Create an async CRUD web service in Rust with warp

Mario Zupan I'm a software developer originally from Graz but living in Vienna, Austria. I previously worked as a full-stack web developer before quitting my job to work as a freelancer and explore open source. Currently, I work at Netconomy.

Recent posts:

Using React Shepherd To Build A Site Tour

Using React Shepherd to build a site tour

React Shepherd stands out as a site tour library due to its elegant UI and out-of-the-box, easy-to-use React Context implementation.

Onuorah Bonaventure
May 1, 2024 ⋅ 14 min read
A Guide To Cookies In Next Js

A guide to cookies in Next.js

Cookies are crucial to web development. This article will explore how to handle cookies in your Next.js applications.

Georgey V B
Apr 30, 2024 ⋅ 10 min read
Handling Dates In JavaScript With Tempo

Handling dates in JavaScript with Tempo

Use the Tempo library to format dates and times in JavaScript while accounting for time zones, daylight saying time, and date internationalization.

Amazing Enyichi Agu
Apr 30, 2024 ⋅ 8 min read
A Guide To Deno.cron

A guide to Deno.cron

This guide explores how to use the cron package in Deno, `Deno.cron`, to handle scheduling tasks with specific commands.

Rosario De Chiara
Apr 29, 2024 ⋅ 5 min read
View all posts

9 Replies to "Create an async CRUD web service in Rust with warp"

  1. I did not know about warp and I really liked. It seems more streamlined than Actix. I will give it a try. Thanks for sharing.

  2. Hello!
    Thanks for your article.
    To be able to use the API consumed by axios in a recent browser, I had to reconfigure the CORS as follows:

    // CORS
    let cors = warp::cors()
    .allow_methods(&[Method::GET, Method::POST, Method::DELETE])
    .allow_headers(vec![header::CONTENT_TYPE, header::AUTHORIZATION])
    .allow_any_origin();

    The listing of allowed methods & headers is mandatory.
    Moreover, I had to put the line .with(cors) after the line .recover(handle_rejection), otherwise, you will have a CORS issue after a rejection is raised.

    Hope it helps 🙂

  3. Hey!

    Thanks for the feedback and for the heads up about the order of CORS, that definitely makes more sense that way. 🙂

  4. Hello Mario! Nice article, but I just performance tested this code (with my own DB) – it comes down to about 17k req/sec, but Rocket achieves 65k… of course I just stumbled upon warp, but there must be a limiting factor somewhere in the pg connector which I wasn’t able to pinpoint.

  5. Hey Bernd!

    That’s interesting for sure. There could be several reasons for this. It could be related to the connection pool, but also Warp-inherent. I’m not aware of any up2date benchmarks comparing the two frameworks.

    Also, since this post has been published over a year ago, it still uses Tokio pre 1.x, so using fully upgraded dependencies might improve this as well.

    Generally, keep in mind that this example was just a very basic tutorial on how to build a simple DB-backed web-app with Warp. It’s entirely unoptimized, since this simply wasn’t a relevant factor in the tutorial.

  6. Hello there Mario!

    Thanks a lot for this awesome guide, this was pretty much exactly what I was looking for 🙂
    Even someone like me, who is just dipping his toes into the rust waters, was able to get it up and running while (hopefully) understanding at least some parts of the whole thing 😀

    I had some issues though as it seems that since the tutorial was written rust/some dependencies changed a few things around. So I went ahead and updated all dependencies (while adding cargo-husky and itconfig as I simply love those two ^^).

    The result can be seen here: https://github.com/ItsNothingPersonal/warp-postgres-example

    If there is any problem with me hosting it there, just tell me and I’ll delete it 🙂

    Again, thanks for your work!

  7. Hey Sebastian!

    That’s great – I’m very happy the article was useful to you and big props on “making it your own”, which is the ultimate goal a tutorial such as this can have – to enable readers to extend and build something on top of it. Great work!

    The issues with dependencies are an annoying reality in any not-yet-fully-stable field such as Rust’s async-web, but I’m glad you were able to resolve them. 🙂

    Thanks for your feedback. 🙂

  8. Hi Mario, thanks for this tutorial. I’m in the process of learning rust and found this useful, picked up a few tips but must admit some of it I still don’t grasp.

    I managed to get it working and even satisfied cargo clippy by ensuring all linting rules were adhered to. There seems to be no reason for many of the closures and just supplying the actual function as the argument was sufficient e.g. .map_err(reject::custom)?; as suggested by clippy.

    I’ve had a little dabble with axum and it seems simpler than warp but it’s very early days for me, anyway I’ve got 2 more of your blogs to work through now, Build a Rust + WebAssembly frontend web app with Yew and Full-stack Rust: A complete tutorial with examples. I can’t wait to get stuck in, thanks again.

Leave a Reply