Questions tagged [rust-axum]

80 questions
2
votes
1 answer

Rust Axum framework - is unwrap safe?

I am using Rust Axum as my web framework. I am looking to use do mutable shared state in my route handlers. I came across this question: https://github.com/tokio-rs/axum/discussions/629 which pointed to this…
2
votes
1 answer

Get access to the request headers from Axum IntoResponse

I have a custom IntoResponse type that needs to return different data to the client depending on the value of the Accept header in the request. I would prefer, if possible, to have this be seamless (i.e. I don't have to pass the header value into…
wallefan
  • 354
  • 1
  • 11
2
votes
0 answers

Add custom jwt authentification in Axum rust

I am trying Axum to build a web server and I want to protect some routes with JWT. I build a function which take the token from request, check if it is valide then pass a data to handler function. But I have an error when compiling and I don't…
Kylo_Entro
  • 46
  • 5
2
votes
1 answer

My Axum handler won't compile: the trait IntoResponse is not implemented

Totally new to Rust. I am trying to implement oauth authentication and I am using axum, with no success.. Here is my ugly code: use axum::{ Json, extract::Query, extract::Extension, http::StatusCode, response::IntoResponse }; use…
Francesco
  • 1,742
  • 5
  • 44
  • 78
2
votes
1 answer

Stateful Axum middleware not compiling if wrapped in a Mutex

I'm trying to implement a basic middleware in a Axum project, and I'm facing an issue when my middleware is passed a state wrapped in a Mutex. Disclaimer There is a high chance that the shared state used in this sample is not optimized nor…
Rafi Panoyan
  • 822
  • 9
  • 21
2
votes
0 answers

Testing a database layer in axum with a closure with assertions

I'm writing some integration tests for a web service using axum and postgres and have a Database trait (that is passed to axum through its with_state functionality) to be able to use a mock database for some of the tests: #[async_trait] pub trait…
beta
  • 2,380
  • 21
  • 38
2
votes
1 answer

Axum Query Extractor - Allow for struct with optional fields to be parsed?

Rust newbie here (and first time SO poster)! I am attempting to add pagination to my axum API, and am trying to use an optional Query extractor on a Pagination struct in my request, to allow for users to skip specifying Pagination and allow the…
2
votes
2 answers

Why do I have an CORS error requesting data from an rust back-end?

For a project I am using a rust back-end with the Axum framework. I am using react as a front-end. Atm, I just want to be able to make a simple request to the back-end. Everything works when I use a plug-in to disable CORS, but I don't get it…
Bram Breda
  • 71
  • 7
2
votes
1 answer

How to return both success and error case in the same Axum handler?

Ok so I have an axum handler which looks somewhat like this: #[debug_handler] async fn handler( State(server_state): State>, Query(query_params): Query, ) -> impl IntoResponse { match…
Finlay Weber
  • 2,989
  • 3
  • 17
  • 37
2
votes
2 answers

Axum router rejecting CORS OPTIONS preflight with 405 even with CorsLayer

Before a fetch is sent, the browser will send a request method OPTIONS to confirm that the API will accept the request from a script with a particular origin. Chrome shows that the my Axum server is rejecting my client's request with 405. My router…
Alister Lee
  • 2,236
  • 18
  • 21
2
votes
1 answer

Struggling to use sea-orm and axum to get the sum of two columns, each with unique filters, in a postgres db

I wrote a beginner program in Rust that actually is working pretty well, with axum for the web-app and sea-orm to manage the connection to the postgres backend. My struggle is putting together a query, or multiple queries, that can get me the sum of…
eseacr17
  • 21
  • 2
2
votes
1 answer

Is it possible to run Axum on a single thread?

I know that Axum is built on top of Tokio and Tokio has a multi-threaded scheduler and current-threaded scheduler. Is it possible to set the runtime to make it serve the requests in single thread?
sailing
  • 55
  • 4
2
votes
1 answer

Why is Handler trait not implemented for simple async fn returning status?

I thought Handler should be implemented for an async function returning a StatusCode: use axum::{ headers, http::StatusCode, routing::{delete, get, post, put}, Router, }; pub fn create_routes() -> Router { …
The Mungler
  • 136
  • 1
  • 12
2
votes
1 answer

How to redirect after successful form submission with axum?

I found axum::response::Redirect to redirect a user to a different page, but the example there only shows get methods. I want to know how I can do that with post. The code I'm thinking is like the following: let app = Router::new() .route("/",…
tet
  • 1,287
  • 1
  • 17
  • 36
2
votes
1 answer

Why the error "the trait `Handler<_, _>` is not implemented for `()`" if I'm only returning a fn?

In Go I can return a function from a function, like this: func newHandler(arg string) http.HandlerFunc { return service.Handler(arg) } // in other files handler := newHandler(config.Arg) router.route("/", func(group *router.Group) { …
Fred Hors
  • 3,258
  • 3
  • 25
  • 71