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:

Understanding The Css Revert Layer Keyword, Part Of Css Cascade Layers

Understanding the CSS revert-layer keyword

In this article, we’ll explore CSS cascade layers — and, specifically, the revert-layer keyword — to help you refine your styling strategy.

Chimezie Innocent
Apr 24, 2024 ⋅ 6 min read
Exploring Nushell, A Rust Powered, Cross Platform Shell

Exploring Nushell, a Rust-powered, cross-platform shell

Nushell is a modern, performant, extensible shell built with Rust. Explore its pros, cons, and how to install and get started with it.

Oduah Chigozie
Apr 23, 2024 ⋅ 6 min read
Exploring Zed, A Newly Open Source Code Editor Written In Rust

Exploring Zed, an open source code editor written in Rust

The Zed code editor sets itself apart with its lightning-fast performance and cutting-edge collaborative features.

Nefe Emadamerho-Atori
Apr 22, 2024 ⋅ 7 min read
Implementing Infinite Scroll In Next Js With Server Actions

Implementing infinite scroll in Next.js with Server Actions

Infinite scrolling in Next.js no longer requires external libraries — Server Actions let us fetch initial data directly on the server.

Rahul Chhodde
Apr 19, 2024 ⋅ 10 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