gotham icon indicating copy to clipboard operation
gotham copied to clipboard

Is there a easy way to handle CORS/OPTIONS?

Open ghost opened this issue 7 years ago • 7 comments

ghost avatar Jan 23 '18 12:01 ghost

Hi @krrkcc - At this time there isn't.

It is something we're interested in seeing come to Gotham however so I am going to leave this ticket open as a means to track the feature request.

If you happen to work on this in the interim we'd be keen to see what you come up with.

bradleybeddoes avatar Jan 24 '18 21:01 bradleybeddoes

This would be perfect for a middleware package. Maybe as part of the Gotham ecosystem. Or maybe a middleware shipped with Gotham out of the box.

Thoughts?

thedodd avatar Jan 26 '18 14:01 thedodd

I'd certainly like to see this functionality ship from the core Gotham repository and be opt in when folks need it.

At this stage I'm not sure on best implementation approach as I've not yet properly considered the problem space (for what it's worth my initial thought around this was a middleware based solution as well).

If other folks have some time to study the relevant spec and are able to make some implementation recommendations that would be brilliant 👍.

bradleybeddoes avatar Jan 26 '18 23:01 bradleybeddoes

See this: https://github.com/lawliet89/rocket_cors it's an implementation of CORS for the Rocket framework.

StyMaar avatar Jan 27 '18 01:01 StyMaar

Thanks @StyMaar, had a very brief look will spend some more time in the future.

From what I saw it seems that the Gotham Router / RouterBuilder may also be a point of extension to consider for implementing this.

bradleybeddoes avatar Jan 27 '18 06:01 bradleybeddoes

Rocket didn't initially have a good way to handle CORS, and the implementations provided by others were sort of clumsy. I'd just say that if you happen to have your app behind a reverse proxy such as nginx or apache, CORS can be handled there, albeit in a bit of a blanket fashion.

onelson avatar Mar 27 '18 03:03 onelson

i dont see a way to handle options without doing it individually per route.

  • a middleware cannot handle a METHOD for which there is no route
  • its not possible to install an route.option("/*") handler, the other routes will be checked first and option is rejected (method not allowed).
  • scopes have no effect on that

i'm working around this by writing my own middleware stack that i call at the top of every function like so

pub fn list_devices(state: State) -> (State, Response<Body>) {
    if let Some(v) = cors(&state) {
        return (state, v);
    }
    if let Some(v) = auth(&state) {
        return (state, v);
    }

effectively i'm not actually using gotham

aep avatar Oct 04 '18 12:10 aep