Questions tagged [rust-warp]

rust warp is a super-easy, composable, web server framework for warp speeds. The main concept in warp is the Filter.

warp is a super-easy, composable, web server framework for warp speeds. Thanks to its Filter system, warp provides these out of the box:

  • Path routing and parameter extraction
  • Header requirements and extraction
  • Query string deserialization
  • JSON and Form bodies
  • Static Files and Directories
  • Websockets
  • Access logging

The main concept in warp is the Filter, which allows composition to describe various endpoints in your web service. Besides this powerful trait, warp comes with several built in filters, which can be combined for your specific needs.

https://docs.rs/warp/

92 questions
10
votes
2 answers

How do I handle errors in Warp using both Rejection and the question-mark operator?

Using warp.rs 0.2.2, let's consider a basic web service with one route for GET /: #[tokio::main] async fn main() -> Result<(), anyhow::Error> { let getRoot = warp::get().and(warp::path::end()).and_then(routes::getRoot); …
Nicolas Marshall
  • 4,186
  • 9
  • 36
  • 54
9
votes
1 answer

Is there a way to do validation as part of a filter in Warp?

I have a route and an endpoint function defined. I've also injected some dependencies. pub fn route1() -> BoxedFilter<(String, ParamType)> { warp::get() .and(warp::path::param()) .and(warp::filters::query::query()) …
CyanRook
  • 8,664
  • 4
  • 21
  • 20
8
votes
0 answers

How can I reduce noise when tracing with Rust Warp?

When using warp with tracing enabled, I find the tracing output to be noisy. This is because a set of tracing events is emitted by every route, i.e., filter, that is attempted on each request. So following the tracing example from the warp repo, if…
the_morrok
  • 83
  • 5
7
votes
2 answers

Serve on multiple ports(http,https) simultaneously using warp framework in Rust

I want to serve multiple connections using warp so that I can redirect every http request to https. Here is what I am up to right now. #[tokio::main] async fn main() { let http_routes = warp::path("http").map(|| { println!("This is http…
kanudo
  • 2,119
  • 1
  • 17
  • 33
7
votes
1 answer

How to check the authorization header using Warp?

I'm building a graphql api with Rust and Warp. I've looked through the docs, but I have still not figured out how to chain the filters, especially for checking the authorization in request header. let context_extractor = warp::any() // this code…
mununki
  • 350
  • 4
  • 16
4
votes
1 answer

API key validation in warp (Rust)

I'm trying get started with warp and testing an api key validation. The following code works but it's not nice. The validation function extracts the key from the header. After a successful validation the key is no longer used but "handle_request"…
Jimmy Foobar
  • 191
  • 1
  • 8
4
votes
1 answer

Rust (warp) How to discard unauthorized requests?

Let's say I have a function that checks if an authorization header is valid and if the authentication is correct. How do I make a warp filter that discards all requests with invalid header or false credentials?
4
votes
1 answer

How do I make a string outlive a closure body?

I have this code: .and_then(move |key: Option| async { let pool = pool.clone(); let key = key.as_ref().map(|s| &**s); match pool.get() { Ok(conn) => Ok(Session::from_key(conn, key)), Err(e) => { …
Zane Hitchcox
  • 936
  • 1
  • 9
  • 23
3
votes
1 answer

How do I tackle this issue "expected a closure that implements the `Fn` trait, but this closure only implements `FnOnce`"

I am trying to get rate limiting to work with Warp using Governor crate. However, when I try using rate_limiter instance wrapped in Arc part of a closure, I keep getting "expected a closure that implements the Fn trait, but this closure only…
Bhargav
  • 31
  • 4
3
votes
1 answer

When I use warp,it just work on localhost, but not public network

I wrote a Web application based on rust-warp,it works well.But it can just access by localhost:3030(127.0.0.1:3030).Now I want to access it by public network(101.35.56.79)/Local Area Network(192.168.1.18),but it didn't work.I can't find the relevant…
hacktor
  • 47
  • 4
3
votes
1 answer

How to properly handle errors from Warp route

I'm trying to structure a non-trivial warp REST application into modules while handling errors and rejections elegantly. It's not working out the way I expect. Consider this simple application: main.rs use warp_sample::routes; #[tokio::main] async…
DungeonTiger
  • 627
  • 1
  • 9
  • 21
3
votes
1 answer

How can I achieve shared application state with Warp async routes?

I have a Rust application using warp. It implements a RESTful CRUD API. I need each route handler (i.e., the function that ends up being ultimately called by the warp filters) to have access to, and (in most cases) mutate shared application…
sporejack
  • 71
  • 1
  • 6
3
votes
1 answer

How to write a simple warp handler returning json or html?

I have the following: use warp::Filter; pub struct Router {} impl Router { pub async fn handle( &self, ) -> std::result::Result { let uri = "/path"; match uri { …
Patryk
  • 22,602
  • 44
  • 128
  • 244
3
votes
1 answer

Is there a way to pass a custom file path to warp::fs::file?

I'd like to build a custom path, then download a file at that path. Eg, warp::path!("files" / u32) .map(|fileId| { format!("{}.txt", *FILES_PATH, fileId) }) .and(warp::fs::file) But I get…
MrBigglesworth
  • 350
  • 1
  • 8
3
votes
1 answer

Dependency Injection in Rust Warp

How do I inject dependencies into my route handlers in Warp? A trivial example is as follows. I have a route that I want to serve a static value that is determined at startup time, but the filter is what passes values into the final handler. How…
CyanRook
  • 8,664
  • 4
  • 21
  • 20
1
2 3 4 5 6 7