Merge pull request #705 from bradrydzewski/master
ability to handle options requests
This commit is contained in:
commit
49a9c33dc9
2 changed files with 25 additions and 0 deletions
|
@ -131,6 +131,7 @@ func main() {
|
||||||
|
|
||||||
// create the router and add middleware
|
// create the router and add middleware
|
||||||
mux := router.New()
|
mux := router.New()
|
||||||
|
mux.Use(middleware.Options)
|
||||||
mux.Use(ContextMiddleware)
|
mux.Use(ContextMiddleware)
|
||||||
mux.Use(middleware.SetHeaders)
|
mux.Use(middleware.SetHeaders)
|
||||||
mux.Use(middleware.SetUser)
|
mux.Use(middleware.SetUser)
|
||||||
|
|
24
server/middleware/options.go
Normal file
24
server/middleware/options.go
Normal file
|
@ -0,0 +1,24 @@
|
||||||
|
package middleware
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/zenazn/goji/web"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Options automatically return an appropriate "Allow" header when the
|
||||||
|
// request method is OPTIONS and the request would have otherwise been 404'd.
|
||||||
|
func Options(c *web.C, h http.Handler) http.Handler {
|
||||||
|
fn := func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if r.Method == "OPTIONS" {
|
||||||
|
w.Header().Set("Access-Control-Allow-Origin", "*")
|
||||||
|
w.Header().Set("Access-Control-Allow-Methods", "GET,POST,PUT,DELETE,OPTIONS")
|
||||||
|
w.Header().Set("Access-Control-Allow-Headers", "Authorization")
|
||||||
|
w.Header().Set("Allow", "HEAD,GET,POST,PUT,DELETE,OPTIONS")
|
||||||
|
w.WriteHeader(http.StatusOK)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
h.ServeHTTP(w, r)
|
||||||
|
}
|
||||||
|
return http.HandlerFunc(fn)
|
||||||
|
}
|
Loading…
Reference in a new issue