gotham-rs / gotham

A flexible web framework that promotes stability, safety, security and speed.

Home Page:https://gotham.rs

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Accept customer parameter for router handle

efancier opened this issue · comments

In the multi-thread application, if gotham can implement customer parameter for route.get("xxx").to(handle, customer parameter), it seems be helpful, here is an example of the use case:
in main, global parameter defined as
let global = Arc::new(Mutex::new(0));
let gclone1 = global.clone();
let gclone2 = global.clone();

and gclone1 pass to router as

gotham::start_with_tls(addr, router(gclone1 ), build_config()?);

in router, pass gclone1 to handle

route.post("/xxx").to( userHandle, gclone1);

then the user handle functin signature will look like this:

fn userHandle(mut state: State, gclone: std::sync::Arc<std::sync::Mutex>) -> Box{

}

commented

I don't understand your suggestion, why don't you use global.clone() in your handler, or use a middleware to inject your gclone1 into the handler?

msrd0, thanks for you comments. we need pass gclone1 to handle because gclone1 and global are defined in main() function, which is out of the handle functioin's scope.
use middleware is a good idea but make the design more complex

commented

I don't think we need another way to inject stuff into handlers. Unless there is a specific reason you can't use middlewares and/or global variables, I'm going to close this.