Skip to content

Easily add extra headers to an endpoint #82

@shepmaster

Description

@shepmaster

I have some endpoints like this clipped example:

impl Index {
    #[get("/")]
    fn index(&self) -> impl Future<Item = FileResponse, Error = io::Error> + Send {}

    #[get("/help")]
    fn help(&self) -> impl Future<Item = FileResponse, Error = io::Error> + Send {}
}

impl Assets {
    #[get("/assets/*asset")]
    fn asset(&self, asset: PathBuf) -> impl Future<Item = FileResponse, Error = io::Error> + Send {}
}

I'd like to set the Cache-Control header to 1 hour for index / help and 1 year for asset.

It'd be nice to have some amount of reusable code for this, instead of manually mucking about with the http::Response in each endpoint.

Some ideas I had:

  1. Add a higher-order service:

    ServiceBuilder::new()
        .resource(Cache::new(one_hour, Index::new(config.root.clone())))
        .resource(Cache::new(one_year, Assets::new(config.root)))
  2. Some kind of sugar (unclear on how to implement this)

    #[get("/")]
    #[web(cache = one_hour)]
    fn index(&self) -> impl Future<Item = FileResponse, Error = io::Error> + Send {}

Ideas and suggested direction would be appreciated.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requestquestionFurther information is requested

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions