Skip to content

Commit

Permalink
Remove cors signature, use global middleware to handle preflight request
Browse files Browse the repository at this point in the history
  • Loading branch information
vardius committed Sep 10, 2020
1 parent 33c7df6 commit 8407a4f
Show file tree
Hide file tree
Showing 3 changed files with 0 additions and 32 deletions.
12 changes: 0 additions & 12 deletions fasthttp.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ type fastHTTPRouter struct {
fileServer fasthttp.RequestHandler
notFound fasthttp.RequestHandler
notAllowed fasthttp.RequestHandler
cors fasthttp.RequestHandler
handler fasthttp.RequestHandler
middlewareCounter uint
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}

Expand All @@ -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)
Expand Down
12 changes: 0 additions & 12 deletions nethttp.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ type router struct {
fileServer http.Handler
notFound http.Handler
notAllowed http.Handler
cors http.Handler
handler http.Handler
middlewareCounter uint
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}

Expand All @@ -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)
Expand Down
8 changes: 0 additions & 8 deletions router.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}

0 comments on commit 8407a4f

Please sign in to comment.