poem-web / poem

A full-featured and easy-to-use web framework with the Rust programming language.
Apache License 2.0
3.5k stars 283 forks source link

How can you add dynamic params to a Middleware when used with OpenAPI? #705

Open danechitoaie opened 9 months ago

danechitoaie commented 9 months ago

Looking at examples/openapi/poem-middleware/src/main.rs I see that we can apply a "transform" function that applies a middleware to the desired API endpoint.

My function is like this:

pub(crate) fn set_size_limit(ep: impl Endpoint) -> impl Endpoint {
    ep.with(SizeLimit::new(1024))
}

How can we make the size param dynamic? So that I don't hardcode it to 1024?

Christoph-AK commented 9 months ago

First thought: Define a global mutexed variable, and use this here. Then the changed value will be used here. See https://doc.rust-lang.org/std/sync/struct.OnceLock.html Init it before starting the server and use set in other functions or another request handler to change the value at runtime. Not entirely sure about the performance implications, but normally mutexes and the likes are unproblematic up to at least 100k requests per second.

danechitoaie commented 9 months ago

For now what I did was to copy over SizeLimit middleware into my src folder and then modify it to read the config from req.data;

    async fn call(&self, req: Request) -> Result<Self::Output> {
        let app_config = req.data::<AppConfig>();
        let size_limit = match app_config {
            Some(app_config) => app_config.size_limit,
            None => 0,
        };
        ...

I don't like that I had to copy it, but it had just few lines of code so I guess it's not that big of a problem. Still not sure if this is the best way, or there's a better one.