uptrace/bunrouter

Why is this function called so many times?

frederikhors opened this issue · 2 comments

Using the example CORS if I put a breakpoint on line: https://github.com/uptrace/bunrouter/blob/master/example/cors/main.go#L67 my debugger stops many many times there when I start the app.

Can I ask you why so many times?

Shorter code:

package main

import (
	"fmt"
	"log"
	"net/http"

	"github.com/rs/cors"
	"github.com/uptrace/bunrouter"
)

func main() {
	router := bunrouter.New(
		bunrouter.WithMiddleware(newCorsMiddleware([]string{"http://localhost:9999"})),
	)

	router.GET("/", func(w http.ResponseWriter, req bunrouter.Request) error {
		fmt.Println(req.Method, req.Route(), req.Params().Map())
		
		return nil
	})

	log.Println(http.ListenAndServe(":9999", router))
}

func newCorsMiddleware(allowedOrigins []string) bunrouter.MiddlewareFunc {
	return func(next bunrouter.HandlerFunc) bunrouter.HandlerFunc {
		corsHandler := cors.New(cors.Options{ // put a breakpoint on this line
			AllowedOrigins:   allowedOrigins,
			AllowCredentials: true,
		}).Handler(next)

		return bunrouter.HTTPHandler(corsHandler)
	}
}

I guess this is fixed by #36?

Yes. It works now. Sorry.