I'm trying to implement a web API using Iron as a practical exercise.
My session is the following struct, which will be encoded as a JWT. Every time I receive requests from clients, some handlers will need to access user_id
;
#[derive(Serialize, Deserialize)]
struct Session {
session_id: i32,
user_id: Option<i32>,
expiration: u64,
}
There are several ways to do this, but I don't know the most idiomatic and least verbose. I was thinking something like:
fn handle(&self, req: &mut Request) -> IronResult<Response> {
let session = match get_session(req) {
Err(err) => return Ok(Response::with((status::BadRequest, err.description()))),
Ok(session) => session,
};
Ok(Response::with((status::Ok, err.description())))
}
But this way I'll need this snippet on several endpoints.
I could use a middleware, but I don't know how to catch the struct value between middlewares.