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:

Integrating Django Templates With React For Dynamic Webpages

Integrating Django templates with React for dynamic webpages

Create a dynamic demo blog site using Django and React to demonstrate Django’s server-side functionalities and React’s interactive UI.

Kayode Adeniyi
Apr 18, 2024 ⋅ 7 min read
Using Aoi Js To Build A Bot For Discord

Using aoi.js to build a bot on Discord

Explore how the aoi.js library makes it easy to create Discord bots with useful functionalities for frontend applications.

Rahul Padalkar
Apr 17, 2024 ⋅ 9 min read
Web Components Adoption Guide: Overview, Examples, And Alternatives

Web Components adoption guide: Overview, examples, and alternatives

Evaluate Web Components, a set of standards that allow you to create custom HTML tags for more reusable, manageable code.

Elijah Asaolu
Apr 16, 2024 ⋅ 11 min read
Using Aws Lambda And Aws Cloudfront To Optimize Image Handling

Using AWS Lambda and CloudFront to optimize image handling

Leverage services like AWS Lambda, CloudFront, and S3 to handle images more effectively, optimizing performance and providing a better UX.

Nitish Sharma
Apr 12, 2024 ⋅ 12 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