gotham
gotham copied to clipboard
Is there a easy way to handle CORS/OPTIONS?
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.
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?
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 👍.
See this: https://github.com/lawliet89/rocket_cors it's an implementation of CORS for the Rocket framework.
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.
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.
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