From 8407a4fdcc389c4a45694b86353653f4de4e15a5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafa=C5=82=20Lorenz?= Date: Fri, 11 Sep 2020 00:09:24 +1000 Subject: [PATCH] Remove cors signature, use global middleware to handle preflight request --- fasthttp.go | 12 ------------ nethttp.go | 12 ------------ router.go | 8 -------- 3 files changed, 32 deletions(-) diff --git a/fasthttp.go b/fasthttp.go index 1e29a88..c016473 100644 --- a/fasthttp.go +++ b/fasthttp.go @@ -32,7 +32,6 @@ type fastHTTPRouter struct { fileServer fasthttp.RequestHandler notFound fasthttp.RequestHandler notAllowed fasthttp.RequestHandler - cors fasthttp.RequestHandler handler fasthttp.RequestHandler middlewareCounter uint } @@ -122,10 +121,6 @@ func (r *fastHTTPRouter) Compile() { } } -func (r *fastHTTPRouter) CORS(cors fasthttp.RequestHandler) { - r.cors = cors -} - func (r *fastHTTPRouter) NotFound(notFound fasthttp.RequestHandler) { r.notFound = notFound } @@ -208,7 +203,6 @@ func (r *fastHTTPRouter) serveHTTP(ctx *fasthttp.RequestCtx) { ctx.Response.Header.Set("Allow", allow) if method == fasthttp.MethodOptions { - r.serveCors(ctx) return } @@ -221,12 +215,6 @@ func (r *fastHTTPRouter) serveHTTP(ctx *fasthttp.RequestCtx) { r.serveNotFound(ctx) } -func (r *fastHTTPRouter) serveCors(ctx *fasthttp.RequestCtx) { - if r.cors != nil { - r.cors(ctx) - } -} - func (r *fastHTTPRouter) serveNotFound(ctx *fasthttp.RequestCtx) { if r.notFound != nil { r.notFound(ctx) diff --git a/nethttp.go b/nethttp.go index 8146581..c919943 100644 --- a/nethttp.go +++ b/nethttp.go @@ -31,7 +31,6 @@ type router struct { fileServer http.Handler notFound http.Handler notAllowed http.Handler - cors http.Handler handler http.Handler middlewareCounter uint } @@ -119,10 +118,6 @@ func (r *router) Compile() { } } -func (r *router) CORS(cors http.Handler) { - r.cors = cors -} - func (r *router) NotFound(notFound http.Handler) { r.notFound = notFound } @@ -207,7 +202,6 @@ func (r *router) serveHTTP(w http.ResponseWriter, req *http.Request) { w.Header().Set("Allow", allow) if req.Method == http.MethodOptions { - r.serveCors(w, req) return } @@ -220,12 +214,6 @@ func (r *router) serveHTTP(w http.ResponseWriter, req *http.Request) { r.serveNotFound(w, req) } -func (r *router) serveCors(w http.ResponseWriter, req *http.Request) { - if r.cors != nil { - r.cors.ServeHTTP(w, req) - } -} - func (r *router) serveNotFound(w http.ResponseWriter, req *http.Request) { if r.notFound != nil { r.notFound.ServeHTTP(w, req) diff --git a/router.go b/router.go index eed2fd0..788c4c4 100644 --- a/router.go +++ b/router.go @@ -82,10 +82,6 @@ type Router interface { // NotFound replies to the request with the // 405 Error code NotAllowed(http.Handler) - - // CORS handler replies to request with cors header and handles preflight request - // it is enhancement to improve middleware usability instead of wrapping every handler - CORS(http.Handler) } // FastHTTPRouter is a fasthttp micro framework, HTTP request router, multiplexer, mux @@ -158,8 +154,4 @@ type FastHTTPRouter interface { // NotFound replies to the request with the // 405 Error code NotAllowed(fasthttp.RequestHandler) - - // CORS handler replies to request with cors header and handles preflight request - // it is enhancement to improve middleware usability instead of wrapping every handler - CORS(fasthttp.RequestHandler) }